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.googlecode.osde.internal.utils.MapUtil.java

public static String toString(Map<String, String> data) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(baos);
    out.writeObject(data == null ? new HashMap<String, String>() : data);
    out.flush();//from   w  w w  .j a  v a  2s.  c  o  m
    byte[] bytes = baos.toByteArray();
    byte[] encoded = Base64.encodeBase64(bytes);
    return new String(encoded, "UTF-8");
}

From source file:com.ibm.watson.developer_cloud.professor_languo.ingestion.indexing.StackExchangeThreadSerializer.java

/**
 * Serialize a java Object into a byte array
 * //w  w w .  j a  v a  2s .com
 * @param objToSerialize - Java Object to be serialized
 * @return a byte array serialized from the java Object
 * @throws IngestionException
 */
public static byte[] serializeObjToBinArr(Object objToSerialize) throws IngestionException {
    ByteArrayOutputStream binOut = new ByteArrayOutputStream();
    try {
        ObjectOutputStream out = new ObjectOutputStream(binOut);
        out.writeObject(objToSerialize);
        out.close();
        binOut.close();
    } catch (IOException e) {
        throw new IngestionException(e);
    }
    return binOut.toByteArray();
}

From source file:com.facebook.TestUtils.java

public static <T extends Serializable> T serializeAndUnserialize(final T t) {
    try {/*from ww  w . jav  a 2s.  co  m*/
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        new ObjectOutputStream(os).writeObject(t);
        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());

        @SuppressWarnings("unchecked")
        T ret = (T) (new ObjectInputStream(is)).readObject();

        return ret;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}

From source file:Serialization.java

/**
 * Gets an array of bytes corresponding to the given object.
 * @param object the object to serialize
 * @return an array of bytes./*from  ww w  .  j a  v a2  s  . c  o m*/
 * @throws IOException if the object can't be turned into an array of bytes.
 */
public static byte[] storeObject(final Serializable object) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(baos);
        oos.writeObject(object);
        oos.flush();
        return baos.toByteArray();
    } finally {
        if (oos != null) {
            oos.close();
        }
        if (baos != null) {
            baos.close();
        }
    }
}

From source file:com.thoughtworks.go.websocket.MessageEncoding.java

public static String encodeWork(Work work) {
    try {/*from  ww  w .j  a v a  2  s  .co m*/
        try (ByteArrayOutputStream binaryOutput = new ByteArrayOutputStream()) {
            try (ObjectOutputStream objectStream = new ObjectOutputStream(binaryOutput)) {
                objectStream.writeObject(work);
            }
            return Base64.getEncoder().encodeToString(binaryOutput.toByteArray());
        }
    } catch (IOException e) {
        throw bomb(e);
    }
}

From source file:Models.UserModel.java

public Boolean SignUp(User user, String password) {
    try {/*from  www  .j  a v  a 2s  .c om*/
        FileOutputStream file = new FileOutputStream("src/Data/users.dat");
        try (ObjectOutputStream oStream = new ObjectOutputStream(file)) {
            for (int i = 0; i < users.size(); ++i) {
                if (users.get(i).getEmail().equals(user.getEmail())) {
                    JOptionPane.showMessageDialog(null, "? c ng?i s dng email ny", "Li", 2);
                    return false;
                }
            }
            user.setAuth(HMAC_SHA256(user.getEmail(), password));
            users.add(user);
            oStream.writeObject(users);
            oStream.close();
            return true;

        }
    } catch (IOException e) {
        System.out.println(e.getMessage());
        JOptionPane.showMessageDialog(null, "Error write file", "ERROR", 2);
        return false;
    }
}

From source file:com.enonic.cms.core.portal.instruction.PostProcessInstruction.java

public final String serialize() throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ObjectOutputStream dout = new ObjectOutputStream(out);

    writeExternal(dout);//  w w  w . ja v  a2 s.c o m

    dout.close();
    out.close();

    return new String(Base64.encodeBase64(out.toByteArray()));
}

From source file:gnomezgrave.gsyncj.auth.Profile.java

public static void saveSettings(Profile profile, String fileName) throws IOException, ClassNotFoundException {
    ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(fileName));
    oo.writeObject(profile);/* w  w w .  j a v  a 2 s  . c  o  m*/
    oo.close();
}

From source file:com.oprisnik.semdroid.utils.FileUtils.java

public static void writeObjectToStream(Object object, OutputStream output) throws IOException {
    ObjectOutputStream out = null;
    try {/*from   ww w. j a  v a  2  s .  c  om*/
        out = new ObjectOutputStream(new BufferedOutputStream(output));
        out.writeObject(object);
        out.flush();
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:asia.stampy.common.serialization.SerializationUtils.java

/**
 * Serialize base64.// w w  w.j  a v  a  2s  .  c o m
 * 
 * @param o
 *          the o
 * @return the string
 * @throws IOException
 *           Signals that an I/O exception has occurred.
 */
public static String serializeBase64(Object o) throws IOException {
    SERIALIZE_LOCK.lock();
    try {
        if (o instanceof byte[])
            return Base64.encodeBase64String((byte[]) o);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);

        oos.writeObject(o);

        return Base64.encodeBase64String(baos.toByteArray());
    } finally {
        SERIALIZE_LOCK.unlock();
    }
}