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:Main.java

public static byte[] objectToByte(Object obj) throws Exception {
    ObjectOutputStream oos = null;
    try {/*  w w w  .j a  v  a  2  s  . c  o  m*/
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        return bos.toByteArray();
    } finally {
        if (oos != null)
            oos.close();
    }
}

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

/**
 * Serialize a StackExchangeThread into a byte array
 * //  ww  w  . j a va2 s . com
 * @param threadToSerialize - StackExchangeThread to be serialized
 * @return a byte array serialized from the StackExchangeThread
 * @throws IngestionException
 */
public static byte[] serializeThreadToBinArr(StackExchangeThread threadToSerialize) throws IngestionException {
    ByteArrayOutputStream binOut = new ByteArrayOutputStream();
    try {
        ObjectOutputStream out = new ObjectOutputStream(binOut);
        out.writeObject(threadToSerialize);
        out.close();
        binOut.close();
    } catch (IOException e) {
        throw new IngestionException(e);
    }
    return binOut.toByteArray();
}

From source file:com.talis.storage.s3.cache.RedisChunkHandler.java

public static byte[] serialize(S3Object chunk) throws IOException {
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    ObjectOutputStream objStream = new ObjectOutputStream(byteStream);
    objStream.writeObject(chunk);//from  w ww  .java2  s . c o  m
    objStream.flush();
    objStream.close();
    return byteStream.toByteArray();
}

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

/**
 * Serialize a StackExchangeThread into a binary file
 * //from  w w w.j  a v  a 2s  .c o  m
 * @param threadToSerialize - The StackExchangeThread to be serialized
 * @return the path(relative to the resource folder) of the serialized binary file
 * @throws IngestionException
 */
public static String serializeThreadToBinFile(StackExchangeThread threadToSerialize, String dirPath)
        throws IngestionException {
    String binFileName = threadToSerialize.getId() + StackExchangeConstants.BIN_FILE_SUFFIX;
    try {
        String binFilePath = dirPath + binFileName;
        File serFile = new File(binFilePath);
        if (serFile.getParentFile() != null)
            serFile.getParentFile().mkdirs();
        if (!serFile.exists())
            serFile.createNewFile();
        OutputStream binOut = new FileOutputStream(serFile);
        ObjectOutputStream out = new ObjectOutputStream(binOut);
        out.writeObject(threadToSerialize);
        out.close();
        binOut.close();
    } catch (IOException e) {
        throw new IngestionException(e);
    }
    return binFileName;
}

From source file:de.bps.webservices.clients.onyxreporter.HashMapWrapper.java

/**
 * Serializes the given object o and encodes the result as non-chunked base64.
 * // w w w.  j  a  v  a  2  s.c  om
 * @param objectToSerializeAndEncode
 * @return
 * @throws OnyxReporterException
 */
private static final String serialize(final Object objectToSerializeAndEncode) throws OnyxReporterException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(baos);
        oos.writeObject(objectToSerializeAndEncode);
        oos.close();
        oos = null;
        return Base64.encodeBase64String(baos.toByteArray());
    } catch (final IOException e) {
        throw new OnyxReporterException("Could not serialize object!", e);
    } finally {
        try {
            if (oos != null) {
                oos.close();
            }
        } catch (final IOException 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.//  w w w. j a  v  a  2  s  .c  om
 * @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:cpd3314.buildit12.CPD3314BuildIt12.java

/**
 * Build a sample method that saves a handful of car instances as Serialized
 * objects, and as JSON objects./*from ww  w .j  a  v a2s .  c o  m*/
 */
public static void doBuildIt2Output() {
    Car[] cars = { new Car("Honda", "Civic", 2001), new Car("Buick", "LeSabre", 2003),
            new Car("Toyota", "Camry", 2005) };

    // Saves as Serialized Objects
    try {
        FileOutputStream objFile = new FileOutputStream("cars.obj");
        ObjectOutputStream objStream = new ObjectOutputStream(objFile);

        for (Car car : cars) {
            objStream.writeObject(car);
        }

        objStream.close();
        objFile.close();
    } catch (IOException ex) {
        Logger.getLogger(CPD3314BuildIt12.class.getName()).log(Level.SEVERE, null, ex);
    }

    // Saves as JSONObject
    try {
        PrintWriter jsonStream = new PrintWriter("cars.json");

        JSONArray arr = new JSONArray();
        for (Car car : cars) {
            arr.add(car.toJSON());
        }

        JSONObject json = new JSONObject();
        json.put("cars", arr);

        jsonStream.println(json.toJSONString());

        jsonStream.close();
    } catch (IOException ex) {
        Logger.getLogger(CPD3314BuildIt12.class.getName()).log(Level.SEVERE, null, ex);
    }
}

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

/**
 * Serializes a object into a file.//  ww  w . ja v  a  2 s.  co  m
 * 
 * @param o Object to be serialized.
 * @param f File where the serialization will occurs.
 * @param append defines if the write will occur in append mode.
 */
public static void dump(Object o, File f, boolean append) {

    if (o != null && f != null) {

        try {
            // File where the data will be written.
            FileOutputStream fos = new FileOutputStream(f, append);

            // Object that writes (writer) the data on the file.
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.flush();
            oos.writeObject(o); // write the data.
            oos.close();

            fos.close();

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

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

/** Write one object serilized to a output stream.
 * @param obj The Object to serilize./*  w  ww . j a  v  a2  s.  com*/
 * @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:berlin.iconn.persistence.InOutOperations.java

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