Example usage for java.io ObjectOutputStream writeObject

List of usage examples for java.io ObjectOutputStream writeObject

Introduction

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

Prototype

public final void writeObject(Object obj) throws IOException 

Source Link

Document

Write the specified object to the ObjectOutputStream.

Usage

From source file:berlin.iconn.persistence.InOutOperations.java

public static void saveSimpleWeights(float[][] weights, Date date, String suffix) throws IOException {
    mkdir(simpleWeightsFolder);/*from ww w  .  j a  va2 s  .  c  o  m*/
    File file = new File(simpleWeightsFolder + "/" + getFileNameByDate(date, suffix, "dat"));
    ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(file.toPath()));
    oos.writeObject(weights);
    oos.close();
}

From source file:net.darkmist.alib.io.Serializer.java

/** Write one object serilized to a output stream.
 * @param obj The Object to serilize./*from w w  w .  j  a  va2  s . co  m*/
 * @param out The output stream to write it to. This will be closed!
 */
public static <T extends Serializable> void serializeToStream(T obj, OutputStream out) throws IOException {
    ObjectOutputStream objOut;

    if (out instanceof ObjectOutputStream)
        objOut = (ObjectOutputStream) out;
    else
        objOut = new ObjectOutputStream(out);
    objOut.writeObject(obj);
    objOut.close();
}

From source file:SerialIntList.java

/**
 * Serialize the object o (and any Serializable objects it refers to) and
 * store its serialized state in File f.
 *//* ww w  .  j a  v a2 s . co m*/
static void store(Serializable o, File f) throws IOException {
    ObjectOutputStream out = // The class for serialization
            new ObjectOutputStream(new FileOutputStream(f));
    out.writeObject(o); // This method serializes an object graph
    out.close();
}

From source file:org.alfresco.rad.test.AlfrescoTestRunner.java

public static String serializableToString(Serializable serializable) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(serializable);
    oos.close();// ww  w .j a v a 2  s.  c  om

    String string = Base64.encodeBase64URLSafeString(baos.toByteArray());

    return string;
}

From source file:it.polito.elite.dog.core.library.model.DeviceStatus.java

/**
 * Serialize a DeviceStatus into a String using a byte encoding
 * @param object/*from   w w w  .  j av  a2  s.  co  m*/
 *          a DeviceStatus ready for serialization
 * @return a byte encoded String of a DeviceStatus
 * @throws IOException 
 */
public static String serializeToString(DeviceStatus object) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream encoder = new ObjectOutputStream(baos);
    //serialize the DeviceStatus
    encoder.writeObject(object);
    //flush and close everything
    encoder.flush();
    baos.flush();
    encoder.close();
    baos.close();
    return new String(Base64.encodeBase64(baos.toByteArray()));
}

From source file:com.taobao.tair.etc.TranscoderUtil.java

public static byte[] serialize(Object o) {
    if (o == null) {
        throw new NullPointerException("Can't serialize null");
    }//w ww .  j  av  a  2s  . c om

    byte[] rv = null;

    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(bos);

        os.writeObject(o);
        os.close();
        bos.close();
        rv = bos.toByteArray();
    } catch (IOException e) {
        throw new IllegalArgumentException("Non-serializable object", e);
    }

    return rv;
}

From source file:bencoding.securely.Converters.java

public static String serializeObjectToString(Object object) throws Exception {

    ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(arrayOutputStream);
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(gzipOutputStream);

    objectOutputStream.writeObject(object);

    objectOutputStream.flush();/*from  w  ww.j  a  va2 s. co m*/
    objectOutputStream.close();
    gzipOutputStream.close();
    arrayOutputStream.close();

    String objectString = new String(Base64.encodeToString(arrayOutputStream.toByteArray(), Base64.DEFAULT));

    return objectString;
}

From source file:info.usbo.skypetwitter.Run.java

public static void save_file() {
    try {/*from   w w w  .  jav a 2s.  c o m*/
        FileOutputStream fos = new FileOutputStream(work_dir + "\\twitter_ids.data");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(twitter_ids);
        oos.close();
        fos.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

    try {
        FileOutputStream fos = new FileOutputStream(work_dir + "\\vk_ids.data");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(vk_ids);
        oos.close();
        fos.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

From source file:org.terracotta.management.model.cluster.ClusterTest.java

@SuppressWarnings("unchecked")
private static <T> T copy(T o) throws IOException, ClassNotFoundException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(o);
    oos.close();/*  w  w  w  . j  av  a  2  s  .  c o  m*/
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    return (T) in.readObject();
}

From source file:com.mirth.connect.connectors.jms.JmsMessageUtils.java

/**
 * @param message//from ww  w.jav  a2  s.  co  m
 *            the message to receive the bytes from. Note this only works
 *            for TextMessge, ObjectMessage, StreamMessage and BytesMessage.
 * @return a byte array corresponding with the message payload
 * @throws JMSException
 *             if the message can't be read or if the message passed is a
 *             MapMessage
 * @throws java.io.IOException
 *             if a failiare occurs while stream and converting the message
 *             data
 */
public static byte[] getBytesFromMessage(Message message) throws JMSException, IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024 * 2];
    int len;

    if (message instanceof BytesMessage) {
        BytesMessage bMsg = (BytesMessage) message;
        // put message in read-only mode
        bMsg.reset();
        while ((len = bMsg.readBytes(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
    } else if (message instanceof StreamMessage) {
        StreamMessage sMsg = (StreamMessage) message;
        sMsg.reset();
        while ((len = sMsg.readBytes(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
    } else if (message instanceof ObjectMessage) {
        ObjectMessage oMsg = (ObjectMessage) message;
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(bs);
        os.writeObject(oMsg.getObject());
        os.flush();
        baos.write(bs.toByteArray());
        os.close();
        bs.close();
    } else if (message instanceof TextMessage) {
        TextMessage tMsg = (TextMessage) message;
        baos.write(tMsg.getText().getBytes());
    } else {
        throw new JMSException("Cannot get bytes from Map Message");
    }

    baos.flush();
    byte[] bytes = baos.toByteArray();
    baos.close();
    return bytes;
}