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:io.github.azige.whitespace.Cli.java

static void execute(InputStream input, String path) throws IOException, ClassNotFoundException {
    WhitespaceVM vm = new DefaultWhitespaceVM();
    Program program;/*from  ww w .  j  av a  2s.c  o m*/
    if (path.endsWith(WHITESPACE_BINARY_FILE_SUFFIX)) {
        ObjectInputStream oinput = new ObjectInputStream(new GZIPInputStream(input));
        program = (Program) oinput.readObject();
    } else {
        Interpreter interpreter = createInterpreter();
        program = interpreter.interpret(createReader(input));
    }
    vm.getProcessor().loadProgram(program);
    vm.getProcessor().executeAll(true);
}

From source file:com.cj.restspecs.mojo.Util.java

@SuppressWarnings("unchecked")
public static <T> T readObject(File path) {
    try {//from  w  w w  .j a va2 s .c o m
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(path));
        try {
            return (T) in.readObject();
        } finally {
            in.close();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Object convertByteArrayToObject(byte[] bytes) throws IOException, ClassNotFoundException {
    ObjectInputStream is = null;
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    is = new ObjectInputStream(new BufferedInputStream(byteArrayInputStream));
    Object obj = is.readObject();
    is.close();//from w  w w.j ava 2 s. co m
    return obj;

}

From source file:Main.java

public static synchronized ArrayList<double[]> readArrayListFromFile(String filename) {
    ObjectInputStream input;
    ArrayList<double[]> x = null;
    try {//from   w  ww .ja  v a  2s.com
        input = new ObjectInputStream(new FileInputStream(new File(filename)));
        Object obj = input.readObject();
        if (obj != null)
            x = (ArrayList<double[]>) input.readObject();
        input.close();
        return x;
    } catch (StreamCorruptedException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    return x;
}

From source file:Main.java

@SuppressWarnings("unchecked")
static <T extends Serializable> T fromBlob(Class<T> clazz, byte[] blob)
        throws IOException, ClassNotFoundException {
    T result = null;//from   www.j a  v  a2s.  c om
    ObjectInputStream ois = null;
    try {
        ByteArrayInputStream bos = new ByteArrayInputStream(blob);
        ois = new ObjectInputStream(bos);
        result = (T) ois.readObject();
    } finally {
        if (ois != null) {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

From source file:com.plexobject.testplayer.dao.jdbc.GenericDaoJdbc.java

protected static Object toObject(Blob blob) {
    try {/*from   w  w w . j  av a  2  s .  c o m*/
        if (blob == null)
            return null;
        InputStream in = blob.getBinaryStream();
        if (in == null)
            return null;
        int c;
        ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
        while ((c = in.read()) != -1) {
            out.write(c);
        }
        byte[] b = out.toByteArray();
        if (b.length == 0)
            return null;
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(b));
        Object object = ois.readObject();
        ois.close();
        return object;
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new DaoException("Failed to deserialize", e);
    }
}

From source file:io.smartspaces.scheduling.quartz.orientdb.internal.util.SerialUtils.java

public static <T> T deserialize(byte[] serialized, Class<T> clazz) throws JobPersistenceException {
    // ToDO(keith): Serialize better than Java serialization.
    ByteArrayInputStream byteStream = new ByteArrayInputStream(serialized);
    try {//from w ww  . ja  v  a2  s . com
        ObjectInputStream objectStream = new ObjectInputStream(byteStream);
        Object deserialized = objectStream.readObject();
        objectStream.close();
        if (clazz.isInstance(deserialized)) {
            @SuppressWarnings("unchecked")
            T obj = (T) deserialized;
            return obj;
        }

        throw new JobPersistenceException("Deserialized object is not of the desired type");
    } catch (IOException | ClassNotFoundException e) {
        throw new JobPersistenceException("Could not deserialize.", e);
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T readObjectFromFile(File file) {
    ObjectInputStream ois = null;
    T object = null;/*w  w  w . j av  a2  s. c o  m*/
    try {
        FileInputStream fis = new FileInputStream(file);
        ois = new ObjectInputStream(fis);
        object = (T) ois.readObject();
    } catch (ClassNotFoundException | IOException e) {
        e.printStackTrace();
    } finally {
        if (ois != null) {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return object;
}

From source file:com.googlecode.osde.internal.utils.MapUtil.java

public static Map<String, String> toMap(String data) throws IOException, ClassNotFoundException {

    if (StringUtils.isNotBlank(data)) {
        byte[] bytes = data.getBytes("UTF-8");
        byte[] decoded = Base64.decodeBase64(bytes);
        ByteArrayInputStream bais = new ByteArrayInputStream(decoded);
        ObjectInputStream in = new ObjectInputStream(bais);

        @SuppressWarnings("unchecked")
        Map<String, String> result = (Map<String, String>) in.readObject();
        return result;
    }/*w ww  .j  a  va  2  s  . c o  m*/

    return new HashMap<String, String>();
}

From source file:com.comichentai.serialize.SerializeUtil.java

public static <T> T deserialize(byte[] bytes, Class<T> clazz) {
    ByteArrayInputStream bais = null;
    ObjectInputStream ois = null;
    try {/*from  w w  w  . jav  a 2 s  .  c  o m*/
        bais = new ByteArrayInputStream(bytes);
        ois = new ObjectInputStream(bais);
        Object object = ois.readObject();
        return clazz.cast(object);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        closeQuietly(bais);
        closeQuietly(ois);
    }
}