Example usage for java.io ObjectInputStream readObject

List of usage examples for java.io ObjectInputStream readObject

Introduction

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

Prototype

public final Object readObject() throws IOException, ClassNotFoundException 

Source Link

Document

Read an object from the ObjectInputStream.

Usage

From source file:com.linkedin.pinot.common.utils.DataTableJavaSerDe.java

/**
 * Helper method to de-serialize an object using Java ser/de.
 * @param bytes Java serialized byte-array of the object.
 * @param <T> Type of the object//w  w  w . ja v  a 2 s  . c  om
 * @return De-serialized object
 */
@SuppressWarnings("unchecked")
public static <T extends Serializable> T deserializeJavaObject(@Nonnull byte[] bytes) {

    final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    ObjectInputStream objectInputStream = null;
    Object readObject;

    try {
        try {
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            readObject = objectInputStream.readObject();
        } catch (final Exception e) {
            throw new RuntimeException("Caught exception while deserializing DataTable: " + e);
        }
    } finally {
        IOUtils.closeQuietly(objectInputStream);
        IOUtils.closeQuietly(byteArrayInputStream);
    }

    return (T) readObject;
}

From source file:info.usbo.skypetwitter.Run.java

public static int load_file() {
    // TWITTER/*ww w  .j  a  v a2  s  .com*/
    try {
        FileInputStream fis = new FileInputStream(work_dir + "\\twitter_ids.data");
        ObjectInputStream ois = new ObjectInputStream(fis);
        twitter_ids = (ArrayList) ois.readObject();
        ois.close();
        fis.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
        return 0;
    } catch (ClassNotFoundException c) {
        System.out.println("Class not found");
        c.printStackTrace();
        return 0;
    }
    // VK
    try {
        FileInputStream fis = new FileInputStream(work_dir + "\\vk_ids.data");
        ObjectInputStream ois = new ObjectInputStream(fis);
        vk_ids = (ArrayList) ois.readObject();
        ois.close();
        fis.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
        return 0;
    } catch (ClassNotFoundException c) {
        System.out.println("Class not found");
        c.printStackTrace();
        return 0;
    }
    return 1;
}

From source file:sawtooth.examples.jvmsc.JvmScHandler.java

/**
 * the method that returns an object from a byte[].
 *//*from   w  w w.j av  a  2s  . c o  m*/
public static Object dataFromByteArray(byte[] data) throws IOException, ClassNotFoundException {
    ByteArrayInputStream input = new ByteArrayInputStream(data);
    ObjectInputStream output = new ObjectInputStream(input);
    return output.readObject();
}

From source file:com.joliciel.talismane.parser.ParsingConstrainerImpl.java

public static ParsingConstrainer loadFromFile(File inFile) {
    try {//  ww w  .j av  a 2  s . c o m
        ZipInputStream zis = new ZipInputStream(new FileInputStream(inFile));
        zis.getNextEntry();
        ObjectInputStream in = new ObjectInputStream(zis);
        ParsingConstrainer parsingConstrainer = null;
        try {
            parsingConstrainer = (ParsingConstrainer) in.readObject();
        } catch (ClassNotFoundException e) {
            LogUtils.logError(LOG, e);
            throw new RuntimeException(e);
        }
        return parsingConstrainer;
    } catch (IOException ioe) {
        LogUtils.logError(LOG, ioe);
        throw new RuntimeException(ioe);
    }
}

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 .  ja va 2s  . 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:corner.util.crypto.Cryptor.java

/**
 * ?IO?.IO?,.//from  w w  w.j a v a  2  s .co  m
 * @param inputFileName ????.
 * @param keyFile ??.
 * @return ?IO?.
 */
public static InputStream dencryptFileIO(String inputFileName, String keyFile) {
    if (keyFile == null) {
        try {
            return new FileInputStream(new File(inputFileName));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    SecretKey key = null;

    ObjectInputStream keyis;
    try {
        keyis = new ObjectInputStream(new FileInputStream(keyFile));
        key = (SecretKey) keyis.readObject();

        keyis.close();
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }

    //keyCipher
    Cipher cipher = null;

    try {
        //,
        cipher = Cipher.getInstance("DES");
        // ?
        cipher.init(Cipher.DECRYPT_MODE, key);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    }

    File file = new File(inputFileName);

    try {

        //?
        CipherInputStream in = new CipherInputStream(new BufferedInputStream(new FileInputStream(file)),
                cipher);
        return in;

    } catch (Exception e) {
        throw new RuntimeException(e);

    }

}

From source file:com.pongasoft.util.io.IOUtils.java

@SuppressWarnings("unchecked")
public static <T> T deserialize(InputStream inputStream) throws IOException, ClassNotFoundException {
    if (inputStream == null)
        return null;

    ObjectInputStream ois = new ObjectInputStream(inputStream);
    return (T) ois.readObject();
}

From source file:corner.util.crypto.Cryptor.java

/**
 * IO?,IO??,.//from w w w. j  a va  2  s.c  o m
 * @param outFileName ??. 
 * @param keyFile ??.
 * @return ??.
 */
public static OutputStream encryptFileIO(String outFileName, String keyFile) {
    if (keyFile == null) {
        try {
            return new FileOutputStream(outFileName);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    SecretKey key = null;

    //?
    ObjectInputStream keyis;
    try {
        keyis = new ObjectInputStream(new FileInputStream(keyFile));
        key = (SecretKey) keyis.readObject();
        keyis.close();
    } catch (FileNotFoundException e) {
        log.error("file not found!", e);
        throw new RuntimeException("file not found", e);
    } catch (IOException e) {
        log.error("io occour exception", e);
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        log.error("Class Not Found  exception", e);
        throw new RuntimeException(e);
    }

    //keyCipher
    Cipher cipher = null;
    //?Cipher?

    try {
        cipher = Cipher.getInstance("DES");
        //?

        cipher.init(Cipher.ENCRYPT_MODE, key);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    }

    //???

    //
    CipherOutputStream out = null;
    try {
        out = new CipherOutputStream(new BufferedOutputStream(new FileOutputStream(outFileName)), cipher);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }

    return out;

}

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

/**
 * Deserialize an object from a byte array. Does not throw any exceptions;
 * instead, exceptions are logged and null is returned.
 * /*from w  w w .ja  v a  2 s  .  c om*/
 * @param bytesIn A byte array containing a previously serialized object.
 * @return An object instance, or null if an exception occurred.
 */
public static Object deserialize(byte[] bytesIn) {
    ObjectInputStream objectIn = null;
    try {
        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:org.terracotta.management.model.cluster.ClusterTest.java

@SuppressWarnings("unchecked")
private static <T> T copy(T o) throws IOException, ClassNotFoundException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(o);/*from  ww  w. ja  v  a2s.c o  m*/
    oos.close();
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    return (T) in.readObject();
}