Example usage for java.io ObjectInputStream ObjectInputStream

List of usage examples for java.io ObjectInputStream ObjectInputStream

Introduction

In this page you can find the example usage for java.io ObjectInputStream ObjectInputStream.

Prototype

public ObjectInputStream(InputStream in) throws IOException 

Source Link

Document

Creates an ObjectInputStream that reads from the specified InputStream.

Usage

From source file:duthientan.mmanm.com.CipherRSA.java

@Override
public void setKey(String keyString) {
    try {//from   w ww.j a v  a2s. c o m
        ObjectInputStream inputStream = null;
        inputStream = new ObjectInputStream(new FileInputStream(keyString + "/public.key"));
        publicKey = (PublicKey) inputStream.readObject();
        inputStream = new ObjectInputStream(new FileInputStream(keyString + "/private.key"));
        privateKey = (PrivateKey) inputStream.readObject();
    } catch (IOException ex) {
        Logger.getLogger(CipherRSA.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(CipherRSA.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:jp.xet.baseunits.tests.SerializationTester.java

/**
 * ????????/* w ww  .ja  v a2  s.c om*/
 * 
 * @param serializable 
 * @throws AssertionError ????
 */
public static void assertCanBeSerialized(Object serializable) {
    if (Serializable.class.isInstance(serializable) == false) {
        fail("Object doesn't implement java.io.Serializable interface: " + serializable.getClass());
    }

    ObjectOutputStream out = null;
    ObjectInputStream in = null;
    ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
    ByteArrayInputStream byteArrayIn = null;
    try {
        out = new ObjectOutputStream(byteArrayOut);
        out.writeObject(serializable);

        byteArrayIn = new ByteArrayInputStream(byteArrayOut.toByteArray());
        in = new ObjectInputStream(byteArrayIn);
        Object deserialized = in.readObject();
        if (serializable.equals(deserialized) == false) {
            fail("Reconstituted object is expected to be equal to serialized");
        }
    } catch (IOException e) {
        fail(e.getMessage());
    } catch (ClassNotFoundException e) {
        fail(e.getMessage());
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
    }
}

From source file:com.btoddb.trellis.common.serialization.JavaSerializer.java

@Override
public Serializable deserialize(final ByteBuffer bb) {
    try {//from   www. j ava 2  s.  com
        ObjectInputStream ois;
        ois = new ObjectInputStream(new InputStream() {
            @Override
            public int read() throws IOException {
                return bb.get();
            }
        });
        Object obj = ois.readObject();
        return (Serializable) obj;
    } catch (IOException e) {
        throw new TrellisException("exception while deserializing Java Serializable object", e);
    } catch (ClassNotFoundException e) {
        throw new TrellisException("exception while deserializing Java Serializable object", e);
    }
}

From source file:com.cj.restspecs.mojo.Util.java

@SuppressWarnings("unchecked")
public static <T> T readObject(File path) {
    try {// w w  w . ja  v a2 s.  c  om
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(path));
        try {
            return (T) in.readObject();
        } finally {
            in.close();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:de.tudarmstadt.ukp.dkpro.keyphrases.bookindexing.util.SerializationUtils.java

/**
 * Deserializes an object from disk./*from   ww  w.  java2s .c  o m*/
 *
 * @param filePath the file path
 * @return an object. Clients have to cast the object to the expected type.
 * @throws Exception an exception
 */
public static Object deserializeFromDisk(String filePath) throws Exception {
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(filePath)));

    return in.readObject();
}

From source file:com.pcms.common.Common.java

public <T extends Serializable> T clone(T obj) {
    T clonedObj = null;/*from w ww.ja  va2s .  c o m*/
    try {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(stream);

        out.writeObject(obj);
        out.close();
        ByteArrayInputStream bais = new ByteArrayInputStream(stream.toByteArray());
        ObjectInputStream os = new ObjectInputStream(bais);
        clonedObj = (T) os.readObject();
        stream.close();
    } catch (IOException e) {
        _log.error(e.getMessage());
    } catch (ClassNotFoundException e) {
        _log.error(e.getMessage());
    }
    return clonedObj;
}

From source file:DateServer.java

public void run() {
    while (true) {
        try {//from   www  .j  av a  2s  . co m
            Socket s = ss.accept();

            ObjectInputStream ois;
            ois = new ObjectInputStream(s.getInputStream());
            Locale l = (Locale) ois.readObject();

            PrintWriter pw;
            pw = new PrintWriter(s.getOutputStream());

            MessageFormat mf;
            mf = new MessageFormat("The date is {0, date, long}", l);

            Object[] args = { new Date() };

            pw.println(mf.format(args));

            pw.close();
        } catch (Exception e) {
            System.err.println(e);
        }
    }
}

From source file:com.db4o.sync4o.SyncKey.java

static public SyncKey fromEncodedString(String s) throws Exception {

    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(Base64.decodeBase64(s.getBytes())));
    SyncKey key = (SyncKey) ois.readObject();
    ois.close();/*from www  . ja  va 2 s .  co  m*/

    return key;

}

From source file:io.cloudslang.orchestrator.services.ExecutionSerializationUtil.java

public Execution objFromBytes(byte[] bytes) {
    ObjectInputStream ois = null;
    try {/*w  ww.  ja v a2 s. c o  m*/
        //2 Buffers are added to increase performance
        ByteArrayInputStream is = new ByteArrayInputStream(bytes);
        BufferedInputStream bis = new BufferedInputStream(is);
        ois = new ObjectInputStream(bis);
        //noinspection unchecked
        return (Execution) ois.readObject();
    } catch (IOException | ClassNotFoundException ex) {
        throw new RuntimeException("Failed to read execution from byte[]. Error: ", ex);
    } finally {
        IOUtils.closeQuietly(ois);
    }

}

From source file:TcpClient.java

/** Default constructor. */
public TcpClient()
{
    try/*from  w  w w.j a va2  s . c o m*/
    {
        this.socket = new Socket(SERVER_HOSTNAME, COMM_PORT);
        InputStream iStream = this.socket.getInputStream();
        ObjectInputStream oiStream = new ObjectInputStream(iStream);
        this.payload = (TcpPayload) oiStream.readObject();
    }
    catch (UnknownHostException uhe)
    {
        System.out.println("Don't know about host: " + SERVER_HOSTNAME);
        System.exit(1);
    }
    catch (IOException ioe)
    {
        System.out.println("Couldn't get I/O for the connection to: " +
            SERVER_HOSTNAME + ":" + COMM_PORT);
        System.exit(1);
    }
    catch(ClassNotFoundException cne)
    {
        System.out.println("Wanted class TcpPayload, but got class " + cne);
    }
    System.out.println("Received payload:");
    System.out.println(this.payload.toString());
}