Example usage for java.io ObjectOutputStream close

List of usage examples for java.io ObjectOutputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the stream.

Usage

From source file:com.db4o.sync4o.SyncKey.java

static public String toEncodedString(SyncKey key) throws Exception {

    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(bs);
    os.writeObject(key);// w  w  w.  ja  v  a 2s  . c  o m
    os.flush();
    os.close();

    return new String(Base64.encodeBase64(bs.toByteArray()));

}

From source file:de.kbs.acavis.service.SerializationHelper.java

public static String serializePublicationIdentifier(PublicationIdentifier identifier) throws IOException {
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    ObjectOutputStream so = new ObjectOutputStream(bo);

    so.writeObject(identifier);/*from   www.  j  a  v  a 2s .  c o m*/
    so.flush();

    String serialization = bo.toString();

    so.close();
    bo.close();

    return serialization;
}

From source file:de.kbs.acavis.service.SerializationHelper.java

public static String serializeAuthorIdentifier(AuthorIdentifier identifier) throws IOException {
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    ObjectOutputStream so = new ObjectOutputStream(bo);

    so.writeObject(identifier);//from   w  ww .ja  v a  2 s .c om
    so.flush();

    String serialization = bo.toString();

    so.close();
    bo.close();

    return serialization;
}

From source file:com.cloudera.kitten.util.LocalDataHelper.java

public static <T> String serialize(Map<String, T> mapping) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*from  w  w  w. j a v  a 2  s  .c o  m*/
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(mapping);
        oos.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return Base64.encodeBase64String(baos.toByteArray());
}

From source file:de.tudarmstadt.ukp.dkpro.core.frequency.tfidf.util.TfidfUtils.java

public static void serialize(Object object, String fileName) throws Exception {
    File file = new File(fileName);
    if (!file.exists())
        FileUtils.touch(file);/*from  ww w.  ja va 2  s  .c om*/
    if (file.isDirectory()) {
        throw new IOException("A directory with that name exists!");
    }
    ObjectOutputStream objOut;
    objOut = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
    objOut.writeObject(object);
    objOut.flush();
    objOut.close();

}

From source file:it.restrung.rest.utils.IOUtils.java

/**
 * Serializes ans saves a Serializable object to a file
 *
 * @param object the source object/*  w  w  w. j a  v  a 2s . c  o m*/
 * @param file   the target file
 */
static public void saveSerializableObjectToDisk(Object object, File file) {
    try {
        file.delete();
        file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);
        GZIPOutputStream gzos = new GZIPOutputStream(fos);
        ObjectOutputStream out = new ObjectOutputStream(gzos);
        out.writeObject(object);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        Log.d(IOUtils.class.getName(), "Error, file not found for save serializable object to disk");
        throw new RuntimeException(e);
    } catch (IOException e) {
        Log.d(IOUtils.class.getName(), "Error on save serializable object");
        throw new RuntimeException(e);
    }
}

From source file:de.tudarmstadt.ukp.dkpro.keyphrases.bookindexing.util.SerializationUtils.java

/**
 * @param object/*  w w  w  . j  a va 2 s  .  co m*/
 *          object to be serialized
 * @param filePath
 *          the object will be written at this location, directories will be
 *          created according to path
 * @throws Exception exception
 */
public static void serializeToDisk(Serializable object, String filePath) throws Exception {
    File dir = new File(FilenameUtils.getFullPathNoEndSeparator(filePath));
    if (!dir.exists()) {
        FileUtils.forceMkdir(dir);
    } else {
        if (dir.isFile()) {
            throw new IOException("Path to dir is a file!");
        }
    }
    ObjectOutputStream objOut = null;
    objOut = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(filePath)));
    objOut.writeObject(object);
    objOut.flush();
    objOut.close();
}

From source file:brooklyn.util.javalang.Serializers.java

@SuppressWarnings("unchecked")
public static <T> T reconstitute(T object, ClassLoader classLoader, final ObjectReplacer replacer)
        throws IOException, ClassNotFoundException {
    if (object == null)
        return null;

    class ReconstitutingObjectOutputStream extends ObjectOutputStream {
        public ReconstitutingObjectOutputStream(OutputStream outputStream) throws IOException {
            super(outputStream);
            enableReplaceObject(true);//from   w  w w .j  a v a  2s .com
        }

        @Override
        protected Object replaceObject(Object obj) throws IOException {
            return replacer.replace(obj);
        }
    }
    ;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ReconstitutingObjectOutputStream(baos);
    oos.writeObject(object);
    oos.close();

    class ReconstitutingObjectInputStream extends ClassLoaderObjectInputStream {
        public ReconstitutingObjectInputStream(InputStream inputStream, ClassLoader classLoader)
                throws IOException {
            super(inputStream, classLoader);
            super.enableResolveObject(true);
        }

        @Override
        protected Object resolveObject(Object obj) throws IOException {
            return replacer.resolve(obj);
        }
    }
    ;
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ReconstitutingObjectInputStream ois = new ReconstitutingObjectInputStream(bais, classLoader);
    try {
        return (T) ois.readObject();
    } finally {
        Streams.closeQuietly(ois);
    }
}

From source file:com.code.savemarks.utils.Utils.java

/**
 * Serialize an object into a byte array.
 * //from ww w . j ava 2s . c o  m
 * @param obj An object to be serialized.
 * @return A byte array containing the serialized object
 * @throws QueueFailureException If an I/O error occurs during the
 * serialization process.
 */
public static byte[] serialize(Object obj) {
    try {
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        ObjectOutputStream objectOut = new ObjectOutputStream(new BufferedOutputStream(bytesOut));
        objectOut.writeObject(obj);
        objectOut.close();
        return encodeBase64(bytesOut.toByteArray());
    } catch (IOException e) {
        throw new QueueFailureException(e);
    }
}

From source file:Test.java

private static void clientStart() {
    try {//from   ww  w .jav a  2  s.co  m
        InetSocketAddress hostAddress = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 2583);
        AsynchronousSocketChannel clientSocketChannel = AsynchronousSocketChannel.open();
        Future<Void> connectFuture = clientSocketChannel.connect(hostAddress);
        connectFuture.get(); // Wait until connection is done.
        OutputStream os = Channels.newOutputStream(clientSocketChannel);
        ObjectOutputStream oos = new ObjectOutputStream(os);
        for (int i = 0; i < 5; i++) {
            oos.writeObject("Look at me " + i);
            Thread.sleep(1000);
        }
        oos.writeObject("EOF");
        oos.close();
        clientSocketChannel.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}