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:org.glom.web.server.Utils.java

static public Object deepCopy(final Object oldObj) {
    ObjectOutputStream oos = null;
    ObjectInputStream ois = null;

    try {/*from   w ww  .  j  a  va  2 s .co m*/
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        // serialize and pass the object
        oos.writeObject(oldObj);
        oos.flush();
        final ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
        ois = new ObjectInputStream(bin);

        // return the new object
        return ois.readObject();
    } catch (final Exception e) {
        System.out.println("Exception in deepCopy:" + e);
        return null;
    } finally {
        try {
            oos.close();
            ois.close();
        } catch (final IOException e) {
            System.out.println("Exception in deepCopy during finally: " + e);
            return null;
        }
    }
}

From source file:com.exalttech.trex.util.Util.java

/**
 * De-serialize string to object/*from   www.java 2  s  .  com*/
 *
 * @param serializedStriing
 * @return
 */
public static Object deserializeStringToObject(String serializedStriing) {
    try {
        byte[] data = Base64.getDecoder().decode(serializedStriing);
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
        Object o = ois.readObject();
        ois.close();
        return o;
    } catch (IOException | ClassNotFoundException ex) {
        LOG.error("Error deserializing string to object", ex);
        return null;
    }
}

From source file:MSUmpire.DIA.TargetMatchScoring.java

public static TargetMatchScoring LibraryMatchReadJS(String Filename, String LibID)
        throws FileNotFoundException {

    if (!new File(FilenameUtils.getFullPath(Filename) + FilenameUtils.getBaseName(Filename) + "_" + LibID
            + "_LibMatch.serFS").exists()) {
        return null;
    }//from www  .j av a  2 s .  com
    TargetMatchScoring match = null;
    try {
        Logger.getRootLogger()
                .info("Loading Target library match results to file:" + FilenameUtils.getFullPath(Filename)
                        + FilenameUtils.getBaseName(Filename) + "_" + LibID + "_LibMatch.ser...");
        FileInputStream fileIn = new FileInputStream(FilenameUtils.getFullPath(Filename)
                + FilenameUtils.getBaseName(Filename) + "_" + LibID + "_LibMatch.ser");
        ObjectInputStream in = new ObjectInputStream(fileIn);
        match = (TargetMatchScoring) in.readObject();
        in.close();
        fileIn.close();
    } catch (Exception ex) {
        Logger.getRootLogger().error(ExceptionUtils.getStackTrace(ex));
        return null;
    }
    return match;
}

From source file:backtype.storm.utils.Utils.java

/**
 * Deserialized with ClassLoader/*w  ww  .  j  av a2  s  . c  om*/
 * 
 * @param serialized
 * @param loader
 * @return
 */
public static Object javaDeserializeWithCL(byte[] serialized, URLClassLoader loader) {
    try {
        ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
        Object ret = null;
        if (loader != null) {
            ClassLoaderObjectInputStream cis = new ClassLoaderObjectInputStream(loader, bis);
            ret = cis.readObject();
            cis.close();
        } else {
            ObjectInputStream ois = new ObjectInputStream(bis);
            ret = ois.readObject();
            ois.close();
        }
        return ret;
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}

From source file:de.dal33t.powerfolder.util.ByteSerializer.java

/**
 * Deserializer method with flag indicating if the base array is zip
 * compressed//w w  w  .  j  av a2  s . c o m
 *
 * @param base
 * @param compressed
 * @return the dezerialized object
 * @throws IOException
 * @throws ClassNotFoundException
 */
private static Object deserialize0(byte[] base, boolean compressed) throws IOException, ClassNotFoundException {
    long start = System.currentTimeMillis();
    ObjectInputStream in = null;
    Object result = null;
    try {
        InputStream targetIn;
        // deserialize from the array.......u
        ByteArrayInputStream bin = new ByteArrayInputStream(base);
        if (compressed) {
            GZIPInputStream zipIn = new GZIPInputStream(bin);
            targetIn = zipIn;
        } else {
            targetIn = bin;
        }
        in = new ObjectInputStream(targetIn);
        result = in.readUnshared();
        return result;
    } finally {
        if (in != null) {
            in.close();
        }
        if (BENCHMARK && result != null) {
            totalObjects++;
            totalTime += System.currentTimeMillis() - start;

            int count = 0;
            if (CLASS_STATS.containsKey(result.getClass())) {
                count = CLASS_STATS.get(result.getClass());
            }
            count++;
            CLASS_STATS.put(result.getClass(), count);
        }
    }
}

From source file:de.mpg.escidoc.services.aa.crypto.RSAEncoder.java

public static Key readKeyFromFile(String keyFileName, boolean publ) throws Exception {
    InputStream in = ResourceUtil.getResourceAsStream(keyFileName, RSAEncoder.class.getClassLoader());
    ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in));
    try {// ww w.j  av  a 2 s. c o  m
        BigInteger m = (BigInteger) oin.readObject();
        BigInteger e = (BigInteger) oin.readObject();
        KeySpec keySpec;
        if (publ) {
            keySpec = new RSAPublicKeySpec(m, e);
        } else {
            keySpec = new RSAPrivateKeySpec(m, e);
        }
        KeyFactory fact = KeyFactory.getInstance("RSA");
        if (publ) {
            PublicKey pubKey = fact.generatePublic(keySpec);
            return pubKey;
        } else {
            PrivateKey privKey = fact.generatePrivate(keySpec);
            return privKey;
        }
    } catch (Exception e) {
        throw new RuntimeException("Error reading key from file", e);
    } finally {
        oin.close();
    }
}

From source file:com.ebay.erl.mobius.util.SerializableUtil.java

public static Object deserializeFromBase64(String base64String, Configuration conf) throws IOException {
    ObjectInputStream ois = null;
    try {/*from  www.j av  a2s.  c  o m*/
        byte[] objBinary = Base64.decodeBase64(base64String.getBytes());

        ois = new ObjectInputStream(new ByteArrayInputStream(objBinary));

        Object object = ois.readObject();

        if (conf != null) {
            if (object instanceof Configurable) {
                ((Configurable) object).setConf(conf);
            } else if (object instanceof Configurable[]) {
                Configurable[] confArray = (Configurable[]) object;
                for (Configurable aConfigurable : confArray) {
                    aConfigurable.setConf(conf);
                }
            }
        }

        return object;
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } finally {
        if (ois != null) {
            try {
                ois.close();
            } catch (Throwable e) {
            }
        }
    }
}

From source file:me.StevenLawson.TotalFreedomMod.TFM_Util.java

@SuppressWarnings("unchecked")
public static Map<String, Boolean> getSavedFlags() {
    Map<String, Boolean> flags = null;

    File input = new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.SAVED_FLAGS_FILENAME);
    if (input.exists()) {
        try {//from   w ww. j av  a 2  s  .co m
            FileInputStream fis = new FileInputStream(input);
            ObjectInputStream ois = new ObjectInputStream(fis);
            flags = (HashMap<String, Boolean>) ois.readObject();
            ois.close();
            fis.close();
        } catch (Exception ex) {
            TFM_Log.severe(ex);
        }
    }

    return flags;
}

From source file:com.gwtquickstarter.server.Deferred.java

/**
 * Deserialize an object from a byte array. Does not throw any exceptions;
 * instead, exceptions are logged and null is returned.
 *
 * @param bytesIn A byte array containing a previously serialized object.
 * @return An object instance, or null if an exception occurred.
 *///www .  j  av  a2  s .  co  m
private static Object deserialize(byte[] bytesIn) {
    ObjectInputStream objectIn = null;
    try {
        if (isDevelopment()) { // workaround for issue #2097
            bytesIn = decodeBase64(bytesIn);
        }
        objectIn = new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(bytesIn)));
        return objectIn.readObject();
    } catch (Exception e) {
        log.log(Level.SEVERE, "Error deserializing task", e);
        return null; // don't retry task
    } finally {
        try {
            if (objectIn != null) {
                objectIn.close();
            }
        } catch (IOException ignore) {
        }
    }
}

From source file:com.newatlanta.appengine.taskqueue.Deferred.java

/**
 * Deserialize an object from a byte array. Does not throw any exceptions;
 * instead, exceptions are logged and null is returned.
 *
 * @param bytesIn A byte array containing a previously serialized object.
 * @return An object instance, or null if an exception occurred.
 *///from w ww .  java  2  s .co m
private static Object deserialize(byte[] bytesIn) {
    ObjectInputStream objectIn = null;
    try {
        //             if ( isDevelopment() ) { // workaround for issue #2097
        bytesIn = decodeBase64(bytesIn);
        //             }
        objectIn = new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(bytesIn)));
        return objectIn.readObject();
    } catch (Exception e) {
        log.log(Level.SEVERE, "Error deserializing task", e);
        return null; // don't retry task
    } finally {
        try {
            if (objectIn != null) {
                objectIn.close();
            }
        } catch (IOException ignore) {
        }
    }
}