Example usage for java.io ObjectOutputStream ObjectOutputStream

List of usage examples for java.io ObjectOutputStream ObjectOutputStream

Introduction

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

Prototype

public ObjectOutputStream(OutputStream out) throws IOException 

Source Link

Document

Creates an ObjectOutputStream that writes to the specified OutputStream.

Usage

From source file:com.feilong.commons.core.lang.ObjectUtil.java

/**
 * ?.//from  w  w w .j  a v a2 s. c o  m
 *
 * @param serializable
 *            the object
 * @return the int
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @see ByteArrayOutputStream#size()
 * @since 1.0.7
 */
//XXX ?check,?
public static int size(Object serializable) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
    objectOutputStream.writeObject(serializable);
    objectOutputStream.close();
    return byteArrayOutputStream.size();
}

From source file:com.topekalabs.bigmachine.lib.app.ContinuationSerializationTest.java

@Test
public void simpleSerializationTest() throws Exception {
    MutableInt context = new MutableInt();
    Continuation c = Continuation.startWith(new SimpleContinuationRunnable(), context);
    logger.debug("Is serializable: {}", c.isSerializable());

    Assert.assertEquals("The value of the context was not set properly", SimpleContinuationRunnable.FIRST_VALUE,
            context.getValue());/*from  w  ww  .ja v  a2 s . c o  m*/

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(c);
    oos.close();

    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
    c = (Continuation) ois.readObject();
    ois.close();

    Continuation.continueWith(c, context);

    Assert.assertEquals("The value of the context was not set properly",
            SimpleContinuationRunnable.SECOND_VALUE, context.getValue());
}

From source file:com.conwet.silbops.model.JSONvsRMIPerfT.java

public static void externalizeSeveralObjects(Externalizable[] objects, ByteArrayOutputStream baos) {

    long start = 0, end = 0;

    try {/*from  w  ww  . j a v a 2  s  .  co  m*/
        ObjectOutputStream out = new ObjectOutputStream(baos);

        //Measure the elapsed time
        start = System.nanoTime();

        for (Externalizable object : objects) {
            out.writeObject(object);
        }

        end = System.nanoTime();

        out.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    long elapsed = (end - start);
    long elapsedPerMessage = elapsed / objects.length;
    System.out.println("   Elapsed Time " + objects.length + " messages - Externalize: " + elapsed
            + " nanoseconds (" + elapsedPerMessage + " nanoseconds/msg)");
}

From source file:marshalsec.gadgets.C3P0WrapperConnPool.java

public static String makeC3P0UserOverridesString(String codebase, String clazz)
        throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException,
        InvocationTargetException, IOException {

    ByteArrayOutputStream b = new ByteArrayOutputStream();
    try (ObjectOutputStream oos = new ObjectOutputStream(b)) {
        Class<?> refclz = Class.forName("com.mchange.v2.naming.ReferenceIndirector$ReferenceSerialized"); //$NON-NLS-1$
        Constructor<?> con = refclz.getDeclaredConstructor(Reference.class, Name.class, Name.class,
                Hashtable.class);
        con.setAccessible(true);//from   www  .j  ava  2 s .c  o m
        Reference jndiref = new Reference("Foo", clazz, codebase);
        Object ref = con.newInstance(jndiref, null, null, null);
        oos.writeObject(ref);
    }

    return "HexAsciiSerializedMap:" + Hex.encodeHexString(b.toByteArray()) + ";"; //$NON-NLS-1$
}

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

public static void writeObject(Object o, File path) {
    try {//from w w  w. j a  v  a  2  s  .  c om
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(path));
        try {
            out.writeObject(o);
        } finally {
            out.close();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.hp.saas.agm.rest.client.SessionContext.java

public void save(File targetFile) {
    ObjectOutput out = null;/*w  w w.j a va  2 s  . c  o  m*/
    try {
        out = new ObjectOutputStream(new FileOutputStream(targetFile));
        out.writeObject(this);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                logger.log(Level.SEVERE, "IOException thrown while closing stream.", e);
            }
        }
    }

}

From source file:com.vnomicscorp.spring.security.cas.authentication.redis.DefaultCasAuthenticationTokenSerializer.java

@Override
public String serialize(CasAuthenticationToken token) throws CasAuthenticationTokenSerializerException {
    if (token == null) {
        throw new NullPointerException("Expected given token to be non-null");
    }/*  ww w .  java2s . co  m*/
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(token);
        oos.flush();
        return new String(Base64.encode(baos.toByteArray()), charset);
    } catch (IOException e) {
        throw new CasAuthenticationTokenSerializerException(e);
    }
}

From source file:com.linkedin.pinot.common.utils.DataTableJavaSerDe.java

/**
 * Helper method to serialize an object using Java ser/de.
 *
 * @param object  to serialize//from w  w w  .j  a va  2  s  . com
 * @return Serialized byte[] for the object.
 */
public static byte[] serializeJavaObject(@Nonnull Object object) {
    byte[] bytes;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;

    try {
        try {
            out = new ObjectOutputStream(bos);
            out.writeObject(object);
        } catch (IOException e) {
            LOGGER.error("Caught exception while serializing object for data table.", e);
            Utils.rethrowException(e);
        }

        bytes = bos.toByteArray();
    } finally {
        IOUtils.closeQuietly((Closeable) out);
        IOUtils.closeQuietly(bos);
    }

    return bytes;
}

From source file:SerialCloneTest.java

public Object clone() {
    try {/*  w  w w  .  j a  v  a 2s .c o m*/
        // save the object to a byte array
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bout);
        out.writeObject(this);
        out.close();

        // read a clone of the object from the byte array
        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
        ObjectInputStream in = new ObjectInputStream(bin);
        Object ret = in.readObject();
        in.close();

        return ret;
    } catch (Exception e) {
        return null;
    }
}

From source file:natalia.dymnikova.cluster.UnitTestTcpTransportAddressStealerExtensionIdTest.java

@Test
public void shouldSerializeProps() throws Exception {
    final AkkaExtension extension = new AkkaExtension();

    new ObjectOutputStream(new ByteArrayOutputStream()).writeObject(extension.props(TestActor.class));
}