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.marintek.isis.wicket.popupbox.applib.PopupWicketBoxSemanticsProvider.java

static String toString(Serializable serializable) {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {/*from   w ww.  j a  va 2s.c o  m*/
        oos = new ObjectOutputStream(baos);
        oos.writeObject(serializable);
        return new String(Base64.encodeBase64(baos.toByteArray()));
    } catch (IOException e) {
        throw new Base64Serializer.Exception(e);
    } finally {
        try {
            if (oos != null) {
                oos.close();
            }
        } catch (IOException e) {
            throw new Base64Serializer.Exception(e);
        }
    }
}

From source file:android.databinding.tool.util.GenerationalClassUtil.java

public static void writeIntermediateFile(ProcessingEnvironment processingEnv, String packageName,
        String fileName, Serializable object) {
    ObjectOutputStream oos = null;
    try {/*from  ww w. j  a v a  2s  .  c  o m*/
        FileObject intermediate = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT,
                packageName, fileName);
        OutputStream ios = intermediate.openOutputStream();
        oos = new ObjectOutputStream(ios);
        oos.writeObject(object);
        oos.close();
        L.d("wrote intermediate bindable file %s %s", packageName, fileName);
    } catch (IOException e) {
        L.e(e, "Could not write to intermediate file: %s", fileName);
    } finally {
        IOUtils.closeQuietly(oos);
    }
}

From source file:Main.java

public static boolean saveObject(@NonNull Context context, Object obj, String fileName) {
    if (obj == null || TextUtils.isEmpty(fileName)) {
        return false;
    }//from  ww  w . j a va 2  s.  c  om

    FileOutputStream fos = null;
    ObjectOutputStream oos = null;
    try {
        fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
        oos.flush();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            if (fos != null)
                fos.close();
            if (oos != null)
                oos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:SerializationUtils.java

/**
 * <p>Serializes an <code>Object</code> to the specified stream.</p>
 *
 * <p>The stream will be closed once the object is written.
 * This avoids the need for a finally clause, and maybe also exception
 * handling, in the application code.</p>
 * //from  www . j  a  v  a  2s  .  c o m
 * <p>The stream passed in is not buffered internally within this method.
 * This is the responsibility of your application if desired.</p>
 *
 * @param obj  the object to serialize to bytes, may be null
 * @param outputStream  the stream to write to, must not be null
 * @throws IllegalArgumentException if <code>outputStream</code> is <code>null</code>
 * @throws SerializationException (runtime) if the serialization fails
 */
public static void serialize(Serializable obj, OutputStream outputStream) {
    if (outputStream == null) {
        throw new IllegalArgumentException("The OutputStream must not be null");
    }
    ObjectOutputStream out = null;
    try {
        // stream closed in the finally
        out = new ObjectOutputStream(outputStream);
        out.writeObject(obj);

    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException ex) {
            // ignore close exception
        }
    }
}

From source file:com.mnt.base.util.SerializeUtil.java

public static byte[] serialize(Object obj) {
    byte[] result;

    if (obj == null) {
        result = null;/*  w w  w . j  a va 2  s.  c  o  m*/
    } else {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(baos);
            oos.writeObject(obj);

        } catch (IOException e) {
            log.error("error while open byte array output stream.", e);
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    log.error("error while close byte array output stream.", e);
                }
            }
        }

        result = baos.toByteArray();
    }

    return result;
}

From source file:io.bitsquare.common.util.Utilities.java

public static Object copy(Serializable orig) throws IOException, ClassNotFoundException {
    try {// w w  w  .java2  s. c  om
        // Write the object out to a byte array
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bos);
        out.writeObject(orig);
        out.flush();
        out.close();

        // Make an input stream from the byte array and read
        // a copy of the object back in.
        ObjectInputStream in = new LookAheadObjectInputStream(new ByteArrayInputStream(bos.toByteArray()),
                true);
        Object obj = in.readObject();
        return obj;
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
        throw e;
    }
}

From source file:gov.va.chir.tagline.dao.FileDao.java

private static void saveFeatures(final File file, final Collection<Feature> features) throws IOException {
    final ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));

    for (Feature feature : features) {
        out.writeObject(feature);//from ww w .j ava2 s  . co m
    }

    out.close();
}

From source file:de.alpharogroup.io.SerializedObjectExtensions.java

/**
 * Copys the given Object and returns the copy from the object or null if the object can't be
 * serialized.//from w  w  w  .j  a  va  2 s.  c o  m
 *
 * @param <T>
 *            the generic type of the given object
 * @param orig
 *            The object to copy.
 * @return Returns a copy from the original object.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws ClassNotFoundException
 *             is thrown when a class is not found in the classloader or no definition for the
 *             class with the specified name could be found.
 */
@SuppressWarnings("unchecked")
public static <T extends Serializable> T copySerializedObject(final T orig)
        throws IOException, ClassNotFoundException {
    T object = null;
    ByteArrayOutputStream byteArrayOutputStream = null;
    ObjectOutputStream objectOutputStream = null;
    try {
        byteArrayOutputStream = new ByteArrayOutputStream();
        objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(orig);
        objectOutputStream.flush();
        objectOutputStream.close();
        final ByteArrayInputStream bis = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        final ObjectInputStream ois = new ObjectInputStream(bis);
        object = (T) ois.readObject();
    } finally {
        StreamExtensions.closeOutputStream(byteArrayOutputStream);
        StreamExtensions.closeOutputStream(objectOutputStream);
    }
    return object;
}

From source file:de.alpharogroup.io.SerializedObjectUtils.java

/**
 * Copys the given Object and returns the copy from the object or null if the object can't be
 * serialized.//from  w  w w  .  jav a  2s.co m
 *
 * @param <T>
 *            the generic type of the given object
 * @param orig
 *            The object to copy.
 * @return Returns a copy from the original object.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws ClassNotFoundException
 *             is thrown when a class is not found in the classloader or no definition for the
 *             class with the specified name could be found.
 */
@SuppressWarnings("unchecked")
public static <T extends Serializable> T copySerializedObject(final T orig)
        throws IOException, ClassNotFoundException {
    T object = null;
    ByteArrayOutputStream byteArrayOutputStream = null;
    ObjectOutputStream objectOutputStream = null;
    try {
        byteArrayOutputStream = new ByteArrayOutputStream();
        objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(orig);
        objectOutputStream.flush();
        objectOutputStream.close();
        final ByteArrayInputStream bis = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        final ObjectInputStream ois = new ObjectInputStream(bis);
        object = (T) ois.readObject();
    } finally {
        StreamUtils.closeOutputStream(byteArrayOutputStream);
        StreamUtils.closeOutputStream(objectOutputStream);
    }
    return object;
}

From source file:Main.java

public static Object clone(Object object) throws Exception {
    Object copy = null;//from   w  ww  .ja v a 2s .  c  o  m

    ObjectOutputStream oos = null;
    ObjectInputStream ois = null;

    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        oos.writeObject(object);
        oos.flush();

        ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
        ois = new ObjectInputStream(bin);
        copy = ois.readObject();
    } finally {
        if (ois != null)
            ois.close();
        if (oos != null)
            oos.close();
    }
    return copy;
}