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:glluch.com.ontotaxoseeker.TestsGen.java

/**
 * Deserialize an object/*w  w  w. j a  v a 2  s  . c  o m*/
 * @param filename The file name where the object was serialized.
 * @return An object. Most likely a posterior cast will be needed.
 * @throws FileNotFoundException if the ser file was not found
 * @throws IOException ?
 * @throws ClassNotFoundException Very unlikely to appear. The object class is always there.
 */
public static Object load(String filename) throws FileNotFoundException, IOException, ClassNotFoundException {

    FileInputStream fileIn = new FileInputStream(filename);
    ObjectInputStream in = new ObjectInputStream(fileIn);
    Object o = in.readObject();

    return o;
}

From source file:com.weibo.api.motan.protocol.restful.support.RestfulUtil.java

public static Exception getCause(BuiltResponse resp) {
    if (resp == null || resp.getStatus() != Status.EXPECTATION_FAILED.getStatusCode())
        return null;

    String exceptionClass = resp.getHeaderString(EXCEPTION_HEADER);
    if (!StringUtils.isBlank(exceptionClass)) {
        String body = resp.readEntity(String.class);
        resp.close();/*from w  w w  .j  a  v  a2  s. c  om*/

        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(
                    Base64.decode(body.getBytes(MotanConstants.DEFAULT_CHARACTER)));
            ObjectInputStream ois = new ObjectInputStream(bais);
            return (Exception) ois.readObject();
        } catch (Exception e) {
            LoggerUtil.error("deserialize " + exceptionClass + " error", e);
        }
    }

    return null;
}

From source file:gnomezgrave.gsyncj.auth.ProfileSettings.java

public static ProfileSettings loadProfileSettings(String fileName) throws IOException, ClassNotFoundException {
    ObjectInputStream oi = new ObjectInputStream(new FileInputStream(fileName));
    ProfileSettings profileSettings = (ProfileSettings) oi.readObject();
    oi.close();/*from   w ww .  j a  va2 s . c  o  m*/
    return profileSettings;
}

From source file:com.cloudera.recordservice.hcatalog.common.HCatRSUtil.java

public static Object deserialize(String str) throws IOException {
    if (str == null || str.length() == 0) {
        return null;
    }//  w ww  .ja  v a2 s .c  om
    try {
        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:de.tudarmstadt.ukp.dkpro.argumentation.sequence.feature.meta.AbstractSequenceMetaDataFeatureGenerator.java

@SuppressWarnings("unchecked")
public static List<String> decodeFromString(String featureValue) throws TextClassificationException {
    if (featureValue == null || featureValue.isEmpty()) {
        throw new TextClassificationException(
                "MetaData feature value is empty. Maybe " + "you forgot to add the feature generator "
                        + AbstractSequenceMetaDataFeatureGenerator.class.getName());
    }// ww w . j av  a 2s  . c  o m

    try {
        byte[] bytes = Base64.decodeBase64(featureValue);
        ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(bytes));

        return (List<String>) objectInputStream.readObject();

    } catch (IOException | ClassNotFoundException e) {
        throw new TextClassificationException(e);
    }
}

From source file:edu.tufts.vue.util.Encryption.java

private static synchronized Key getKey() {
    try {/*from   w  w w .  j ava  2 s . c om*/
        if (key == null) {
            File keyFile = new File(
                    VueUtil.getDefaultUserFolder().getAbsolutePath() + File.separator + KEY_FILE);
            if (keyFile.exists()) {
                ObjectInputStream is = new ObjectInputStream(new FileInputStream(keyFile));
                key = (Key) is.readObject();
                is.close();
            } else {
                key = KeyGenerator.getInstance(algorithm).generateKey();
                ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(keyFile));
                os.writeObject(key);
                os.close();
            }
            return key;
        } else {
            return key;
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }

}

From source file:de.kbs.acavis.service.SerializationHelper.java

public static PublicationIdentifier deserializePublicationIdentifier(String serialization)
        throws IOException, ClassNotFoundException {
    byte b[] = serialization.getBytes();

    ByteArrayInputStream bi = new ByteArrayInputStream(b);
    ObjectInputStream si = new ObjectInputStream(bi);

    PublicationIdentifier identifier = (PublicationIdentifier) si.readObject();

    si.close();/*from w  w w .  ja  v a 2  s.com*/
    bi.close();

    return identifier;
}

From source file:BytesUtil.java

public static Object toObject(byte[] bytes) throws IOException, ClassNotFoundException {
    Object obj = null;//from  w ww.j  a  va2 s.  c o  m
    ByteArrayInputStream bis = null;
    ObjectInputStream ois = null;
    try {
        bis = new ByteArrayInputStream(bytes);
        ois = new ObjectInputStream(bis);
        obj = ois.readObject();
    } finally {
        if (bis != null) {
            bis.close();
        }
        if (ois != null) {
            ois.close();
        }
    }
    return obj;
}

From source file:de.tudarmstadt.ukp.dkpro.core.frequency.tfidf.util.TfidfUtils.java

@SuppressWarnings("unchecked")
public static <T> T deserialize(String filePath) throws Exception {
    ObjectInputStream in = null;
    try {//from ww w . jav a 2s .c o  m
        in = new ObjectInputStream(new FileInputStream(new File(filePath)));
        return (T) in.readObject();
    } finally {
        closeQuietly(in);
    }
}

From source file:it.restrung.rest.utils.IOUtils.java

/**
 * Loads a serialized object from a file
 *
 * @param file the file where the object was serialized
 * @return the serialized object in its real in memory object representation
 *//*from  www  .j  a v a 2  s .  c o m*/
@SuppressWarnings("unchecked")
static public <T> T loadSerializableObjectFromDisk(File file) {
    T result = null;
    if (file.exists()) {
        try {
            FileInputStream fis = new FileInputStream(file);
            GZIPInputStream gzis = new GZIPInputStream(fis);
            ObjectInputStream in = new ObjectInputStream(gzis);
            result = (T) in.readObject();
            in.close();
        } catch (FileNotFoundException e) {
            Log.d(IOUtils.class.getName(), "Error, file not found for load serializable object from disk");
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    return result;
}