Example usage for java.io ObjectInput readObject

List of usage examples for java.io ObjectInput readObject

Introduction

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

Prototype

public Object readObject() throws ClassNotFoundException, IOException;

Source Link

Document

Read and return an object.

Usage

From source file:org.jfree.data.time.junit.MonthTest.java

/**
 * Serialize an instance, restore it, and check for equality.
 *///  ww w.j  a  va2 s . c o m
public void testSerialization() {

    Month m1 = new Month(12, 1999);
    Month m2 = null;

    try {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(m1);
        out.close();

        ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
        m2 = (Month) in.readObject();
        in.close();
    } catch (Exception e) {
        System.out.println(e.toString());
    }
    assertEquals(m1, m2);

}

From source file:org.jfree.data.time.junit.TimePeriodValuesTest.java

/**
 * Serialize an instance, restore it, and check for equality.
 *//*w  w  w  . ja  v  a  2  s. c o m*/
public void testSerialization() {

    TimePeriodValues s1 = new TimePeriodValues("A test");
    s1.add(new Year(2000), 13.75);
    s1.add(new Year(2001), 11.90);
    s1.add(new Year(2002), null);
    s1.add(new Year(2005), 19.32);
    s1.add(new Year(2007), 16.89);
    TimePeriodValues s2 = null;

    try {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(s1);
        out.close();

        ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
        s2 = (TimePeriodValues) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertTrue(s1.equals(s2));

}

From source file:info.magnolia.cms.core.version.VersionManager.java

/**
 * get Rule used for this version/*from   w  ww.jav  a2s  . co m*/
 * @param versionedNode
 * @throws IOException
 * @throws ClassNotFoundException
 * @throws RepositoryException
 * */
protected Rule getUsedFilter(Content versionedNode)
        throws IOException, ClassNotFoundException, RepositoryException {
    // if restored, update original node with the restored node and its subtree
    ByteArrayInputStream inStream = null;
    try {
        String ruleString = this.getSystemNode(versionedNode).getNodeData(PROPERTY_RULE).getString();
        inStream = new ByteArrayInputStream(ruleString.getBytes());
        ObjectInput objectInput = new ObjectInputStream(inStream);
        return (Rule) objectInput.readObject();
    } catch (IOException e) {
        throw e;
    } catch (ClassNotFoundException e) {
        throw e;
    } finally {
        IOUtils.closeQuietly(inStream);
    }
}

From source file:com.bah.bahdit.main.plugins.fulltextindex.FullTextIndex.java

/**
 * loads the necessary files from the properties file
 * called from FullTextIndex.configure//from  www. jav a  2  s.c  om
 * 
 * @param context - passed from the servlet
 */
@SuppressWarnings("unchecked")
private void loadResources(ServletContext context) {

    // get the sample table from resources
    try {
        InputStream sample = context.getResourceAsStream(properties.getProperty(FT_SAMPLE));
        InputStream samplebuffer = new BufferedInputStream(sample);
        ObjectInput objectsample = new ObjectInputStream(samplebuffer);
        sampleTable = (HashMap<String, Integer>) objectsample.readObject();
    } catch (ClassNotFoundException e) {
        log.error(e.getMessage());
    } catch (IOException e) {
        log.error(e.getMessage());
    }

    // get the pagerank table from resources
    try {
        InputStream pagerank = context.getResourceAsStream(properties.getProperty(PR_FILE));
        InputStream pagerankbuffer = new BufferedInputStream(pagerank);
        ObjectInput objectpagerank = new ObjectInputStream(pagerankbuffer);
        pagerankTable = (HashMap<String, Double>) objectpagerank.readObject();
    } catch (ClassNotFoundException e) {
        log.error(e.getMessage());
    } catch (IOException e) {
        log.error(e.getMessage());
    }

    // get the spell checker
    spellChecker = LevenshteinDistance.createSpellChecker(context, sampleTable);

    // create stop words list from general list
    try {
        stopWords = new HashSet<String>();
        InputStream gstop = context.getResourceAsStream(properties.getProperty(Search.GENERAL_STOP));
        DataInputStream gin = new DataInputStream(gstop);
        BufferedReader gbr = new BufferedReader(new InputStreamReader(gin));
        String strLine;
        while ((strLine = gbr.readLine()) != null)
            stopWords.add(strLine);
    } catch (IOException e) {
        log.error(e.getMessage());
    }

}

From source file:com.github.naoghuman.cm.model.subcategory.SubCategoryModel.java

@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    this.setId(in.readLong());
    this.setMatrixId(in.readLong());
    this.setCategoryId(in.readLong());
    this.setGenerationTime(in.readLong());
    this.setTitle(StringEscapeUtils.unescapeHtml4(String.valueOf(in.readObject())));
    this.setDescription(StringEscapeUtils.unescapeHtml4(String.valueOf(in.readObject())));
}

From source file:de.pro.dbw.file.dream.api.DreamModel.java

@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    this.setId(in.readLong());
    this.setGenerationTime(in.readLong());
    this.setFavorite(in.readBoolean());
    this.setFavoriteReason(String.valueOf(in.readObject()));
    this.setDescription(String.valueOf(in.readObject()));
    this.setText(String.valueOf(in.readObject()));
    this.setTitle(String.valueOf(in.readObject()));
}

From source file:xbird.xquery.expr.ext.BDQExpr.java

public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    this._endpoint = (XQExpression) in.readObject();
    this._hostVar = (ExecHostVariable) in.readObject();
    final XQExpression queryExpr = (XQExpression) in.readObject();
    this._queryExpr = queryExpr;
    this._type = queryExpr.getType();
}

From source file:org.wso2.carbon.identity.application.authentication.framework.store.SessionDataStore.java

private Object getBlobObject(InputStream is)
        throws IdentityApplicationManagementException, IOException, ClassNotFoundException {
    if (is != null) {
        ObjectInput ois = null;
        try {//from  www . j  a v a  2  s  .c  o m
            ois = new ObjectInputStream(is);
            return ois.readObject();
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    log.error("IOException while trying to close ObjectInputStream.", e);
                }
            }
        }
    }
    return null;
}

From source file:org.jfree.data.xy.junit.XYSeriesCollectionTest.java

/**
 * Serialize an instance, restore it, and check for equality.
 *//*from  w w w.  j a  v a  2  s.c o m*/
public void testSerialization() {
    XYSeries s1 = new XYSeries("Series");
    s1.add(1.0, 1.1);
    XYSeriesCollection c1 = new XYSeriesCollection();
    c1.addSeries(s1);
    XYSeriesCollection c2 = null;

    try {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(c1);
        out.close();

        ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
        c2 = (XYSeriesCollection) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertEquals(c1, c2);
}

From source file:com.ethanruffing.preferenceabstraction.AutoPreferences.java

/**
 * Reads the stored value for an Object preference.
 *
 * @param key The key that the preference is stored under.
 * @param def The default value to return if a setting is not found for the
 *            given key./*from  w  w w  .ja v  a  2 s  . c  om*/
 * @return The value stored for the preference, or the default value on
 * failure.
 */
@Override
public Object getObject(String key, Object def) {
    if (configType == ConfigurationType.SYSTEM) {
        byte[] inArr = prefs.getByteArray(key, null);
        if (inArr == null)
            return def;

        ByteArrayInputStream bis = new ByteArrayInputStream(inArr);
        ObjectInput in = null;
        try {
            in = new ObjectInputStream(bis);
            return in.readObject();
        } catch (ClassNotFoundException | IOException e) {
            e.printStackTrace();
            return def;
        } finally {
            try {
                bis.close();
            } catch (IOException ex) {
                // ignore close exception
            }
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                // ignore close exception
            }
        }
    } else {
        Object val = fileConfig.getProperty(key);
        if (val == null)
            return def;
        else
            return val;
    }
}