Example usage for java.io ObjectInputStream close

List of usage examples for java.io ObjectInputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the input stream.

Usage

From source file:com.facebook.infrastructure.utils.FBUtilities.java

public static Object deserializeFromStream(byte[] bytes) {
    Object o = null;//from w w w.  jav a  2 s .co m
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    try {
        ObjectInputStream ois = new ObjectInputStream(bis);
        try {
            o = ois.readObject();
        } catch (ClassNotFoundException e) {
        }
        ois.close();
        bis.close();
    } catch (IOException e) {
        LogUtil.getLogger(FBUtilities.class.getName()).info(LogUtil.throwableToString(e));
    }
    return o;
}

From source file:atg.tools.dynunit.test.util.FileUtil.java

/**
 * @see org.apache.commons.lang3.SerializationUtils#deserialize(java.io.InputStream)
 *//*from  w w  w.  ja v a 2  s  .c  om*/
@Nullable
@Deprecated
@SuppressWarnings("unchecked")
public static Map<String, Long> deserialize(@NotNull final File file, final long serialTtl) {

    if (file.exists() && file.lastModified() < System.currentTimeMillis() - serialTtl) {
        logger.debug("Deleting previous serial {} because it's older than {} ms", file.getPath(), serialTtl);
        file.delete();
    }

    Map<String, Long> o = null;
    try {
        if (file.exists()) {
            final ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
            try {
                o = (Map<String, Long>) in.readObject();
            } finally {
                in.close();
            }
        }
    } catch (Exception e) {
        logger.catching(e);
    }
    if (o == null) {
        o = new HashMap<String, Long>();
    }
    return o;
}

From source file:Main.java

public static Object clone(Object object) throws Exception {
    Object copy = null;//from  w w w.ja v  a 2  s  .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;
}

From source file:org.pgptool.gui.config.impl.ConfigRepositoryImpl.java

@SuppressWarnings("unchecked")
public static <T extends DtoBase> T readObject(String sourceFile) {
    ObjectInputStream ois = null;
    try {//from w ww.j a  v a 2  s.  c  om
        File file = new File(sourceFile);
        if (!file.exists()) {
            return null;
        }

        FileInputStream fis = new FileInputStream(file);
        ois = new ObjectInputStream(fis);
        T ret = (T) ois.readObject();
        ois.close();
        return ret;
    } catch (Throwable t) {
        log.warn("Failed to read " + sourceFile, t);
        return null;
    } finally {
        safeClose(ois);
    }
}

From source file:edu.harvard.i2b2.Icd9.BinResourceFromIcd9Data.java

public static HashMap<String, String> deSerializeIcd9CodeToNameMap() throws IOException {

    HashMap<String, String> map = null;
    ObjectInputStream ois = null;
    InputStream fis = null;//from   w  w w  .j a v  a 2s .co  m

    try {
        fis = BinResourceFromIcd9Data.class.getResourceAsStream("/icd9/Icd9CodeToNameMap.bin");
        ois = new ObjectInputStream(fis);
        map = (HashMap<String, String>) ois.readObject();

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        ois.close();
        fis.close();
    }

    return map;
}

From source file:Main.java

public static Object readObject(File file) {
    Object obj = null;//from  ww w.j a  v  a2 s  . com
    if (file != null && file.exists()) {
        FileInputStream fileInputStream = null;
        ObjectInputStream objectInputStream = null;

        try {
            fileInputStream = new FileInputStream(file);
            objectInputStream = new ObjectInputStream(fileInputStream);

            obj = objectInputStream.readObject();
        } catch (IOException | ClassNotFoundException e) {
            Log.d("readObject", e.getMessage());
        } finally {
            try {
                if (objectInputStream != null) {
                    objectInputStream.close();
                }
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
            } catch (IOException e) {
                Log.d("readObject", e.getMessage());
            }

        }
    }
    return obj;
}

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

/**
 * The Method toObject() converts the given byte array into an Object.
 *
 * @param byteArray/*  www.j a va2 s  . co m*/
 *            The byte array to convert into an Object.
 * @return The Object the was converted from the byte array.
 * @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.
 */
public static Object toObject(final byte[] byteArray) throws IOException, ClassNotFoundException {
    Object object = null;
    ByteArrayInputStream byteArrayInputStream = null;
    ObjectInputStream objectInputStream = null;
    try {
        byteArrayInputStream = new ByteArrayInputStream(byteArray);
        objectInputStream = new ObjectInputStream(byteArrayInputStream);
        object = objectInputStream.readObject();
        objectInputStream.close();
    } finally {
        StreamExtensions.closeInputStream(byteArrayInputStream);
        StreamExtensions.closeInputStream(objectInputStream);
    }
    return object;
}

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

/**
 * The Method toObject() converts the given byte array into an Object.
 *
 * @param byteArray// w  w w.ja  va2s.  co m
 *            The byte array to convert into an Object.
 * @return The Object the was converted from the byte array.
 * @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.
 */
public static Object toObject(final byte[] byteArray) throws IOException, ClassNotFoundException {
    Object object = null;
    ByteArrayInputStream byteArrayInputStream = null;
    ObjectInputStream objectInputStream = null;
    try {
        byteArrayInputStream = new ByteArrayInputStream(byteArray);
        objectInputStream = new ObjectInputStream(byteArrayInputStream);
        object = objectInputStream.readObject();
        objectInputStream.close();
    } finally {
        StreamUtils.closeInputStream(byteArrayInputStream);
        StreamUtils.closeInputStream(objectInputStream);
    }
    return object;
}

From source file:com.vmware.identity.openidconnect.sample.RelyingPartyInstaller.java

static Object readObject(String file) throws IOException, ClassNotFoundException {
    FileInputStream fis = null;// ww  w  . j a va2 s . c om
    ObjectInputStream ois = null;
    try {
        fis = new FileInputStream(file);
        ois = new ObjectInputStream(fis);
        Object object = ois.readObject();
        return object;
    } finally {
        if (ois != null) {
            ois.close();
        }
    }
}

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

/** Read one serialized object from a input stream.
 * @param in InputStream to read from. This will be closed!
 * @param type The type that is to be read.
 * @return The serialized object cast as type.
 *//*  ww w. j av  a  2  s. co  m*/
public static <T extends Serializable> T deserializeFromStream(InputStream in, Class<T> type)
        throws ClassNotFoundException, IOException {
    ObjectInputStream objIn;
    T obj;

    if (in instanceof ObjectInputStream)
        objIn = (ObjectInputStream) in;
    else
        objIn = new ObjectInputStream(in);

    obj = type.cast(objIn.readObject());
    // to close or not to close... That is the question...
    // we close... It doesn't make too much sense to have a stream with more afterwards...There would be two
    // stream headers...
    objIn.close();
    return obj;
}