Example usage for java.io ObjectInputStream ObjectInputStream

List of usage examples for java.io ObjectInputStream ObjectInputStream

Introduction

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

Prototype

public ObjectInputStream(InputStream in) throws IOException 

Source Link

Document

Creates an ObjectInputStream that reads from the specified InputStream.

Usage

From source file:com.thoughtworks.go.util.ObjectUtil.java

public static Object readObject(File file) throws ClassNotFoundException, IOException {
    InputStream inputStream = null;
    try {/*from  w w w .j av  a  2 s  .  c  om*/
        inputStream = new FileInputStream(file);
        return new ObjectInputStream(inputStream).readObject();
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:Main.java

/**
 * Decompresses the given byte[] and returns the Object.
 * /*from w ww.  j a v a  2s  .c  o  m*/
 * @param <T> Type of expected Object
 * @param bytes compressed byte[]
 * @param bufferSize size of buffer to be used (in bytes)
 * 
 * @return decompressed Object
 * 
 * @throws IOException if failed to decompress
 * @throws ClassCastException if cannot cast to specified type
 */
@SuppressWarnings("unchecked")
/* Ignore Unchecked Cast Warning */
public static <T> T decompress(byte[] bytes, int bufferSize) throws IOException, ClassCastException {

    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(bufferSize);
        Inflater inflater = new Inflater();
        inflater.setInput(bytes);

        // Decompress
        byte[] buf = new byte[bufferSize];
        while (!inflater.finished()) {
            int count = inflater.inflate(buf);
            bos.write(buf, 0, count);
        }
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));

        return (T) ois.readObject();
    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:com.test.todolist.ObjectSerializer.java

public static Object deserialize(String str) throws IOException {
    if (str == null || str.length() == 0)
        return null;
    try {//from   w  ww . jav a  2s . c  om
        ByteArrayInputStream serialObj = new ByteArrayInputStream(decodeBytes(str));
        ObjectInputStream objStream = new ObjectInputStream(serialObj);
        return objStream.readObject();
    } catch (Exception e) {
        return "exception";
    }
}

From source file:io.kodokojo.service.redis.RedisUtils.java

/**
 * Read a serialized Object in a Redis value
 * @param pool Jedis pool to connect to Redis
 * @param key The key where find the expected Object
 * @return The serialized Object.//from ww w.j av  a2s .  c  o m
 */
public static Object readFromRedis(JedisPool pool, byte[] key) {
    try (Jedis jedis = pool.getResource()) {
        if (jedis.exists(key)) {
            byte[] buffer = jedis.get(key);
            ByteArrayInputStream input = new ByteArrayInputStream(buffer);
            try (ObjectInputStream in = new ObjectInputStream(input)) {
                return in.readObject();
            } catch (IOException e) {
                throw new RuntimeException("Unable to create Object input stream", e);
            } catch (ClassNotFoundException e) {
                throw new IllegalStateException("Unable to found class Object ?", e);
            }
        }
    }
    return null;
}

From source file:Test.java

private static void serverStart() {
    try {/* w ww  .j av a 2  s  .c o m*/
        InetSocketAddress hostAddress = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 2583);
        AsynchronousServerSocketChannel serverSocketChannel = AsynchronousServerSocketChannel.open()
                .bind(hostAddress);
        Future<AsynchronousSocketChannel> serverFuture = serverSocketChannel.accept();
        final AsynchronousSocketChannel clientSocket = serverFuture.get();
        if ((clientSocket != null) && (clientSocket.isOpen())) {
            InputStream connectionInputStream = Channels.newInputStream(clientSocket);
            ObjectInputStream ois = null;
            ois = new ObjectInputStream(connectionInputStream);
            while (true) {
                Object object = ois.readObject();
                if (object.equals("EOF")) {
                    clientSocket.close();
                    break;
                }
                System.out.println("Received :" + object);
            }
            ois.close();
            connectionInputStream.close();
        }

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

From source file:homemade.apps.framework.homerlibs.utils.ObjectSerializer.java

public static Object deserialize(String str) {
    if (str == null || str.length() == 0)
        return null;
    try {//from ww w . j a  va  2 s  . co  m
        ByteArrayInputStream serialObj = new ByteArrayInputStream(decodeBytes(str));
        ObjectInputStream objStream = new ObjectInputStream(serialObj);
        return objStream.readObject();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mErrorReturn;
}

From source file:Main.java

public static Object clone(Object object) throws Exception {
    Object copy = null;/* ww w  .j  a  va 2  s.co 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:com.openteach.diamond.network.waverider.slave.SlaveState.java

public static SlaveState fromByteBuffer(ByteBuffer buffer) {
    ByteArrayInputStream bin = null;
    ObjectInputStream oin = null;
    try {/*from w  w w  . j av  a  2 s  .  c  om*/
        bin = new ByteArrayInputStream(buffer.array(), Packet.getHeaderSize() + Command.getHeaderSize(),
                buffer.remaining());
        oin = new ObjectInputStream(bin);
        return (SlaveState) oin.readObject();
    } catch (IOException e) {
        logger.error(e);
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        logger.error(e);
        throw new RuntimeException(e);
    } finally {
        if (oin != null) {
            try {
                oin.close();
            } catch (IOException e) {
                logger.error(e);
            }
        }
    }
}

From source file:bencoding.securely.Converters.java

public static Object deserializeObjectFromString(String objectString) throws Exception {

    ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(
            Base64.decode(objectString, Base64.DEFAULT));
    GZIPInputStream gzipInputStream = new GZIPInputStream(arrayInputStream);
    ObjectInputStream objectInputStream = new ObjectInputStream(gzipInputStream);

    Object object = objectInputStream.readObject();

    objectInputStream.close();/*from w ww.  j  a  va  2 s  .  c  om*/
    gzipInputStream.close();
    arrayInputStream.close();

    return object;
}

From source file:com.eviware.loadui.impl.statistics.db.util.TypeConverter.java

public static Object base64ToObject(String s) {
    byte[] data = Base64.decodeBase64(s);
    try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data))) {
        Object o = ois.readObject();
        return o;
    } catch (IOException e) {
        return null;
    } catch (ClassNotFoundException e) {
        return null;
    }/*from www  .j a v a  2s .  co  m*/
}