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.ery.ertc.estorm.util.ToolUtil.java

public static String serialObject(Object obj, boolean isGzip, boolean urlEnCode) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
    objectOutputStream.writeObject(obj);
    String serStr = null;/*from   ww  w . ja v a  2s  . c  o  m*/
    byte[] bts = null;
    if (isGzip) {
        bts = GZIPUtils.zip(byteArrayOutputStream.toByteArray());
    } else {
        bts = byteArrayOutputStream.toByteArray();
    }
    if (urlEnCode) {
        serStr = new String(org.apache.commons.codec.binary.Base64.encodeBase64(bts), "ISO-8859-1");
    } else {
        serStr = new String(bts, "ISO-8859-1");
    }
    objectOutputStream.close();
    byteArrayOutputStream.close();
    return serStr;
}

From source file:com.weibo.api.motan.codec.AbstractCodec.java

public ObjectOutput createOutput(OutputStream outputStream) {
    try {/* w w w . j  av a 2 s.  com*/
        return new ObjectOutputStream(outputStream);
    } catch (Exception e) {
        throw new MotanFrameworkException(this.getClass().getSimpleName() + " createOutput error", e,
                MotanErrorMsgConstant.FRAMEWORK_ENCODE_ERROR);
    }
}

From source file:net.sf.profiler4j.console.client.Client.java

public synchronized void connect(String host, int port) throws ClientException {
    try {/*  w w  w . j  a v a2  s .  c o m*/
        if (isConnected()) {
            throw new ClientException("Client already connected");
        }
        s = new Socket(host, port);
        out = new ObjectOutputStream(new BufferedOutputStream(s.getOutputStream()));
        in = new ObjectInputStream(new BufferedInputStream(s.getInputStream()));
        String serverVersion = in.readUTF();
        if (!serverVersion.equals(AgentConstants.VERSION)) {
            s.close();
            s = null;
            out = null;
            in = null;
            throw new ClientException("Version of remote agent is incompatible: console is '"
                    + AgentConstants.VERSION + "' but agent is '" + serverVersion + "'");
        }
        log.info("Client connected to " + host + ":" + port);
    } catch (Exception e) {
        handleException(e);
    }
    // fireClientEvent(new ClientEvent(this, ClientEvent.CONNECTED, null));
}

From source file:com.hurence.logisland.processor.hbase.util.ObjectSerDe.java

@Override
public void serialize(OutputStream output, Object value) throws SerializationException, IOException {
    try (final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
            final ObjectOutputStream objOut = new ObjectOutputStream(bOut)) {
        objOut.writeObject(value);/*w  ww  . jav  a2s .  co m*/
        output.write(bOut.toByteArray());
    }
}

From source file:misc.TestUtils.java

@SuppressWarnings("unchecked")
public static <T extends Serializable> T cloneSerializable(T obj) {
    ObjectOutputStream out = null;
    ObjectInputStream in = null;/*from  www. java  2  s.  com*/

    try {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        out = new ObjectOutputStream(bout);

        out.writeObject(obj);
        out.close();

        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
        in = new ObjectInputStream(bin);
        Object copy = in.readObject();

        in.close();

        return (T) copy;
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }

            if (in != null) {
                in.close();
            }
        } catch (IOException ignore) {
        }
    }

    return null;
}

From source file:com.servioticy.queueclient.SimpleQueueClient.java

void writeQueue(LinkedList<Object> queue) throws IOException {
    File file = new File(filePath);
    file.delete();/*from w w w.  j  a va 2  s.  co  m*/
    file.createNewFile();
    FileOutputStream fileOut = new FileOutputStream(file);
    ObjectOutputStream out = new ObjectOutputStream(fileOut);
    out.writeObject(queue);
    out.close();
    fileOut.close();
}

From source file:com.mongodb.hadoop.mapred.input.MongoInputSplit.java

/**
 * Serialize the Split instance//from  w w  w  . ja va2 s . c o m
 */

public void write(DataOutput out) throws IOException {
    final ObjectOutputStream objOut = new ObjectOutputStream((OutputStream) out);
    // TODO - Use object outputstream instead of going to <-> from string?
    out.writeUTF(_mongoURI.toString());

    out.writeUTF(JSON.serialize(_querySpec));
    out.writeUTF(JSON.serialize(_fieldSpec));
    out.writeUTF(JSON.serialize(_sortSpec));
    out.writeInt(_limit);
    out.writeInt(_skip);
    objOut.close();
}

From source file:edu.tufts.vue.util.Encryption.java

private static synchronized Key getKey() {
    try {//from  ww  w.  j  ava 2s  . co  m
        if (key == null) {
            File keyFile = new File(
                    VueUtil.getDefaultUserFolder().getAbsolutePath() + File.separator + KEY_FILE);
            if (keyFile.exists()) {
                ObjectInputStream is = new ObjectInputStream(new FileInputStream(keyFile));
                key = (Key) is.readObject();
                is.close();
            } else {
                key = KeyGenerator.getInstance(algorithm).generateKey();
                ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(keyFile));
                os.writeObject(key);
                os.close();
            }
            return key;
        } else {
            return key;
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }

}

From source file:jedi.util.serialization.Pickle.java

/**
 * Serializes a object on the main memory and returns a sequence of bytes as a String.
 * //  ww w .j a  va  2 s  . co m
 * @param o Object to be serialized.
 * @return String
 */

public static String dumps(Object o) {
    String s = "";

    try {
        // Serializing.

        // Reference to a sequence of bytes on the memory.
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(o);
        oos.close();

        // Converts a array of bytes into a String.
        // The default conversion doesn't works on the other hand Base64 works fine.
        s = new String(Base64.encodeBase64(baos.toByteArray()));

        baos.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return s;
}

From source file:org.versly.rest.wsdoc.RestDocumentation.java

public void toStream(OutputStream out) throws IOException {
    ObjectOutputStream oos = new ObjectOutputStream(out);
    oos.writeObject(this);
    oos.flush();//from   w  ww  .  j  a  v  a  2 s.c o m
}