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:edu.umd.umiacs.clip.tools.io.SerializationTools.java

public static Object deserialize(String path) {

    try (FileInputStream is = new FileInputStream(path);
            ObjectInputStream in = new ObjectInputStream(
                    path.endsWith(".bz2") ? new BZip2CompressorInputStream(is)
                            : path.endsWith(".gz") ? new GzipCompressorInputStream(is) : is)) {
        return in.readObject();
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();//from   w w w  .j  a  v  a 2 s  .  c  o  m
    }
    return null;
}

From source file:grails.plugin.cache.web.filter.redis.GrailsDeserializer.java

public Object deserialize(InputStream inputStream) throws IOException {
    ObjectInputStream ois = new ObjectInputStream(inputStream) {
        @Override/*w ww  .  ja v a  2  s. c  om*/
        protected Class<?> resolveClass(ObjectStreamClass osc) throws IOException, ClassNotFoundException {
            try {
                return Thread.currentThread().getContextClassLoader().loadClass(osc.getName());
            } catch (Exception e) {
                return super.resolveClass(osc);
            }
        }
    };

    try {
        return ois.readObject();
    } catch (ClassNotFoundException e) {
        throw new NestedIOException("Failed to deserialize object type", e);
    }
}

From source file:com.trin.nilmobile.serializer.ObjectSerializer.java

public static Object deserialize(String str) throws IOException {
    if (str == null || str.length() == 0)
        return null;
    try {/*from   ww  w. j av  a  2  s .  com*/
        ByteArrayInputStream serialObj = new ByteArrayInputStream(decodeBytes(str));
        ObjectInputStream objStream = new ObjectInputStream(serialObj);
        return objStream.readObject();
    } catch (Exception e) {
        throw new IOException("Deserialization error: " + e.getMessage(), e);
    }
}

From source file:edu.virginia.jtd5qe.twitter.ObjectSerializer.java

public static Object deserialize(String str) throws IOException {
    if (str == null || str.length() == 0)
        return null;
    try {/*from  w  w w  . j  av a 2  s .c  o  m*/
        ByteArrayInputStream serialObj = new ByteArrayInputStream(decodeBytes(str));
        ObjectInputStream objStream = new ObjectInputStream(serialObj);
        return objStream.readObject();
    } catch (Exception e) {
        throw WrappedIOException.wrap("Deserialization error: " + e.getMessage(), e);
    }
}

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.
 *///w  ww . ja v a2s.c  om
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;
}

From source file:org.terracotta.management.cli.rest.HttpServices.java

private static Object load(String filename) throws IOException, ClassNotFoundException {
    File path = new File(System.getProperty("user.home"), ".tc/mgmt");
    File file = new File(path, filename);

    if (!file.exists()) {
        return null;
    }//  w  w  w. j  a v a2 s . c om

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
    try {
        return ois.readObject();
    } finally {
        ois.close();
    }
}

From source file:cn.lynx.emi.license.ViewLicense.java

public static final LicenseBean retrieveLicense(String license) throws UnsupportedEncodingException {
    String decryptedLicense = _decrypt(license);
    try {/* w ww.  ja v  a  2 s.  c  om*/
        ObjectInputStream ois = new ObjectInputStream(
                new ByteArrayInputStream(Base64.decodeBase64(decryptedLicense)));
        LicenseBean bean = (LicenseBean) ois.readObject();
        return bean;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.facebook.TestUtils.java

public static <T extends Serializable> T serializeAndUnserialize(final T t) {
    try {//ww w  .jav  a2 s  .  c  o  m
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        new ObjectOutputStream(os).writeObject(t);
        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());

        @SuppressWarnings("unchecked")
        T ret = (T) (new ObjectInputStream(is)).readObject();

        return ret;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.ecyrd.jspwiki.util.Serializer.java

/**
 * Deserializes a Base64-encoded String into a HashMap. Both the keys and values
 * must implement {@link java.io.Serializable}.
 * @param rawString the String contents containing the map to be deserialized
 * @return the attributes, parsed into a Map
 * @throws IOException if the contents cannot be parsed for any reason
 *//*  www . j  a  v a 2  s .  c o  m*/
@SuppressWarnings("unchecked")
public static Map<String, ? extends Serializable> deserializeFromBase64(String rawString) throws IOException {
    // Decode from Base64-encoded String to byte array
    byte[] decodedBytes = Base64.decodeBase64(rawString.getBytes("UTF-8"));

    // Deserialize from the input stream to the Map
    InputStream bytesIn = new ByteArrayInputStream(decodedBytes);
    ObjectInputStream in = new ObjectInputStream(bytesIn);
    HashMap<String, Serializable> attributes;
    try {
        attributes = (HashMap<String, Serializable>) in.readObject();
    } catch (ClassNotFoundException e) {
        throw new IOException("Could not deserialiaze user profile attributes. Reason: " + e.getMessage());
    } finally {
        in.close();
    }
    return attributes;
}

From source file:com.lhy.commons.encrypt.service.EncryptService.java

@Override
public License getLicense(File licenseFile, String ipAddress) {
    License licFile = null;/*from  w ww  .  jav  a2 s .  c om*/
    try {
        ObjectInput in = new ObjectInputStream(new FileInputStream(licenseFile));
        licFile = (License) in.readObject();
        if (licFile.getIpAddress().equals(DigestUtils.sha512Hex(ipAddress))
                && licFile.getLicenseType().equals(LicenseType.user)) {
            licFile.setLicenseType(LicenseType.user);
        } else {
            licFile.setLicenseType(LicenseType.developer);
        }
        in.close();
    } catch (FileNotFoundException e) {
        log.error(e.getMessage());
    } catch (IOException e) {
        log.error(e.getMessage());
    } catch (ClassNotFoundException e) {
        log.error(e.getMessage());
    }
    return licFile;
}