Example usage for java.io ObjectInputStream readObject

List of usage examples for java.io ObjectInputStream readObject

Introduction

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

Prototype

public final Object readObject() throws IOException, ClassNotFoundException 

Source Link

Document

Read an object from the ObjectInputStream.

Usage

From source file:com.earldouglas.xjdl.io.LicenseLoader.java

protected License decryptLicense(String encodedLicense)
        throws BadPaddingException, UnsupportedEncodingException, Exception {
    byte[] encryptedLicense = Base64.decodeBase64(encodedLicense);

    SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
    byte[] serializedLicense = cipher.doFinal(encryptedLicense);

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(serializedLicense);
    ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
    License license = (License) objectInputStream.readObject();
    objectInputStream.close();// w  w  w  .j a  va2 s  .c o m
    return license;
}

From source file:org.commonjava.maven.atlas.graph.spi.neo4j.io.Conversions.java

public static ViewParams retrieveView(final Node viewNode, final GraphAdmin maint) {
    if (!viewNode.hasProperty(VIEW_DATA)) {
        return null;
    }/* ww  w. j  a va  2 s .c  o  m*/

    final byte[] data = (byte[]) viewNode.getProperty(VIEW_DATA);

    ObjectInputStream ois = null;
    try {
        ois = new ObjectInputStream(new ByteArrayInputStream(data));
        final ViewParams view = (ViewParams) ois.readObject();

        return view;
    } catch (final IOException e) {
        throw new IllegalStateException(
                "Cannot construct ObjectInputStream to wrap ByteArrayInputStream containing " + data.length
                        + " bytes!",
                e);
    } catch (final ClassNotFoundException e) {
        throw new IllegalStateException("Cannot read ViewParams. A class was missing: " + e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(ois);
    }
}

From source file:ddf.catalog.source.solr.DynamicSchemaResolverTest.java

private MetacardType deserializeMetacardType(byte[] serializedMetacardType)
        throws ClassNotFoundException, IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream((byte[]) serializedMetacardType);
    ObjectInputStream in = new ObjectInputStream(bais);
    MetacardType metacardType = (MetacardType) in.readObject();
    IOUtils.closeQuietly(bais);//from w  w w .j  a v a 2 s .  com
    IOUtils.closeQuietly(in);
    return metacardType;
}

From source file:net.vleu.par.android.rpc.SerializableCookie.java

private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
    final String name = (String) in.readObject();
    final String value = (String) in.readObject();
    this.clientCookie = new BasicClientCookie(name, value);
    this.clientCookie.setComment((String) in.readObject());
    this.clientCookie.setDomain((String) in.readObject());
    this.clientCookie.setExpiryDate((Date) in.readObject());
    this.clientCookie.setPath((String) in.readObject());
    this.clientCookie.setVersion(in.readInt());
    this.clientCookie.setSecure(in.readBoolean());
}

From source file:net.sf.ehcache.distribution.EventMessageTest.java

/**
 * test serialization and deserialization of EventMessage.
 *//* w  ww . j  a  va  2 s  . co  m*/
public void testSerialization() throws IOException, ClassNotFoundException {

    EventMessage eventMessage = new EventMessage(EventMessage.PUT, "key", new Element("key", "element"));

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bout);
    oos.writeObject(eventMessage);
    byte[] serializedValue = bout.toByteArray();
    oos.close();
    EventMessage eventMessage2 = null;
    ByteArrayInputStream bin = new ByteArrayInputStream(serializedValue);
    ObjectInputStream ois = new ObjectInputStream(bin);
    eventMessage2 = (EventMessage) ois.readObject();
    ois.close();

    //Check after Serialization
    assertEquals("key", eventMessage2.getSerializableKey());
    assertEquals("element", eventMessage2.getElement().getObjectValue());
    assertEquals(EventMessage.PUT, eventMessage2.getEvent());
    assertTrue(eventMessage2.isValid());

}

From source file:com.healthmarketscience.rmiio.RemoteStreamServerTest.java

/**
 * Returns the result of serializing and deserializing the given, exported
 * RemoteStub.//from  www.j  av a2 s  .  c  o m
 * <p>
 * Note, i'm leaving the paragraph below because i believe it is related to
 * the problem, however, adding the serialization cycle did <b>not</b> solve
 * the hard ref problem (hence that code is still in place).
 * <p>
 * Evidently, something special happens to a RemoteStub when it is
 * serialized.  There were previously issues during testing where
 * RemoteStubs were throwing NoSuchObjectException on the first remote call.
 * In these cases, the server objects were being garbage collected before
 * ever being used.  Eventually, I realized that this was because the
 * RemoteStubs were not being serialized in the test code (because both the
 * client and server are in the same VM).  There was initially a hack in the
 * RemoteStreamServer to get around this problem by temporarily maintaining
 * a hard reference to the server object until the client makes the first
 * successful call (which could cause leaks if the client dies before making
 * the first call).  After determining that the issue was due to
 * serialization, I was able to make the problem disappear by forcing a
 * serialize/deserialize cycle on the RemoteStub in the test code before
 * handing it to the client thread.
 *
 * @param obj RMI stub to force into "remote" mode
 */
@SuppressWarnings("unchecked")
public static <T> T simulateRemote(T obj) throws IOException {
    PipeBuffer.InputStreamAdapter istream = new PipeBuffer.InputStreamAdapter();
    PipeBuffer.OutputStreamAdapter ostream = new PipeBuffer.OutputStreamAdapter();
    istream.connect(ostream);
    ObjectOutputStream objOstream = new ObjectOutputStream(ostream);
    objOstream.writeObject(obj);
    objOstream.close();

    ObjectInputStream objIstream = new ObjectInputStream(istream);
    try {
        obj = (T) objIstream.readObject();
    } catch (ClassNotFoundException e) {
        throw (IOException) ((new IOException()).initCause(e));
    }
    objIstream.close();

    return obj;
}

From source file:org.sakaiproject.orm.ibatis.support.BlobSerializableTypeHandler.java

protected Object getResultInternal(ResultSet rs, int index, LobHandler lobHandler)
        throws SQLException, IOException {

    InputStream is = lobHandler.getBlobAsBinaryStream(rs, index);
    if (is != null) {
        ObjectInputStream ois = new ObjectInputStream(is);
        try {/*  w w w. ja  v  a 2s  . co  m*/
            return ois.readObject();
        } catch (ClassNotFoundException ex) {
            throw new SQLException("Could not deserialize BLOB contents: " + ex.getMessage());
        } finally {
            ois.close();
        }
    } else {
        return null;
    }
}

From source file:com.ab.http.SerializableCookie.java

/**
 * Read object./*w w  w. j a  v a2  s . c o  m*/
 *
 * @param in the in
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws ClassNotFoundException the class not found exception
 */
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    String name = (String) in.readObject();
    String value = (String) in.readObject();
    clientCookie = new BasicClientCookie(name, value);
    clientCookie.setComment((String) in.readObject());
    clientCookie.setDomain((String) in.readObject());
    clientCookie.setExpiryDate((Date) in.readObject());
    clientCookie.setPath((String) in.readObject());
    clientCookie.setVersion(in.readInt());
    clientCookie.setSecure(in.readBoolean());
}

From source file:com.brianjmelton.apcs.api.BasicCookieStoreSerializer.java

@SuppressWarnings("unchecked")
@Override//from  www .jav  a2 s .c  o  m
public Map<URI, List<SerializableCookie>> restore() throws PersistenceException {
    try {
        Map<URI, List<SerializableCookie>> map = null;
        if (cookieFile.exists()) {
            InputStream in = new FileInputStream(cookieFile);
            ObjectInputStream ois = new ObjectInputStream(in);
            map = (Map<URI, List<SerializableCookie>>) ois.readObject();
            IOUtils.closeQuietly(ois);
        }
        return map;
    } catch (Throwable t) {
        throw new PersistenceException(t);
    }
}

From source file:de.ailis.oneinstance.OneInstanceClient.java

/**
 * @see java.lang.Runnable#run()//  ww  w  .j a v a2s.  c o  m
 */
@Override
public void run() {
    try {
        try {
            // Send the application ID.
            PrintWriter out = new PrintWriter(new OutputStreamWriter(this.socket.getOutputStream(), "UTF-8"));
            out.println(this.appId);
            out.flush();

            // Read the data from the client
            InputStream in = this.socket.getInputStream();
            ObjectInputStream objIn = new ObjectInputStream(in);
            File workingDir = (File) objIn.readObject();
            String[] args = (String[]) objIn.readObject();

            // Call event handler
            boolean result = OneInstance.getInstance().fireNewInstance(workingDir, args);

            // Send the result
            out.println(result ? "start" : "exit");
            out.flush();

            // Wait for client disconnect.
            in.read();
        } finally {
            this.socket.close();
        }
    } catch (IOException e) {
        LOG.error(e.toString(), e);
    } catch (ClassNotFoundException e) {
        LOG.error(e.toString(), e);
    }
}