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.github.kutschkem.Qgen.QuestionRankerByParseProbability.java

/**
 * Load the parser from the given location within the classpath.
 * // w  ww  .  j  a  va 2  s.  c o  m
 * @param aUrl
 *            URL of the parser file.
 */
// copied from DKPro's StanfordParser
private LexicalizedParser getParserDataFromSerializedFile(URL aUrl) throws IOException {
    ObjectInputStream in;
    InputStream is = null;
    try {
        is = aUrl.openStream();

        if (aUrl.toString().endsWith(".gz")) {
            // it's faster to do the buffering _outside_ the gzipping as
            // here
            in = new ObjectInputStream(new BufferedInputStream(new GZIPInputStream(is)));
        } else {
            in = new ObjectInputStream(new BufferedInputStream(is));
        }
        LexicalizedParser pd = LexicalizedParser.loadModel(in);
        // Numberer.setNumberers(pd.numbs); // will happen later in
        // makeParsers()
        in.close();
        return pd;
    } finally {
        closeQuietly(is);
    }
}

From source file:com.canoo.webtest.util.FileUtil.java

/**
 * Helper method for reading objects from a file.
 *
 * @param file the file to read from// w ww  .  ja  v a 2  s .c  o m
 * @param step step requesting the operation
 * @return the object from the file if everything worked correctly
 */
public static Object tryReadObjectFromFile(final File file, final Step step) {
    FileInputStream fis = null;
    ObjectInputStream ois = null;
    String message = "finding";
    try {
        fis = new FileInputStream(file);
        ois = new ObjectInputStream(fis);
        message = "reading from";
        return StreamBoundary.tryReadObject(ois, step);
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
        throw new StepExecutionException("Error " + message + " file: " + e.getMessage(), step);
    } finally {
        IOUtils.closeQuietly(ois);
        IOUtils.closeQuietly(fis);
    }
}

From source file:com.orange.matosweb.MatosCampaign.java

/**
 * Read back steps.//from w w w.  j a  va  2  s  . co m
 */
@SuppressWarnings("unchecked")
private void restore() {
    try {
        File file = new File(privateFolder, BACKUP);
        if (!file.exists())
            return;
        FileInputStream fis = new FileInputStream(file);

        try {
            ObjectInputStream ois = new ObjectInputStream(fis);
            try {
                steps.addAll((List<MatosStep>) ois.readObject());
            } finally {
                ois.close();
            }
        } finally {
            fis.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.adito.boot.AbstractPropertyClass.java

public void restore() throws IOException {
    log.info("Restoring property class " + getName());
    if (store == null) {
        throw new IllegalStateException("Nothing stored for " + getName());
    }//ww w  .j  a  v  a2  s  .  c  o m
    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(store.toByteArray())) {
        protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
            try {
                return Class.forName(desc.getName(), false,
                        AbstractPropertyClass.this.getClass().getClassLoader());
            } catch (ClassNotFoundException ex) {
                return super.resolveClass(desc);
            }
        }
    };
    try {
        definitions = (Map<String, PropertyDefinition>) ois.readObject();
        categories = (List<PropertyDefinitionCategory>) ois.readObject();
        categoryMap = (Map<String, PropertyDefinitionCategory>) ois.readObject();
        store = null;

        // PropertyClass member variable is transient so we need to reinitialise
        for (PropertyDefinition def : definitions.values())
            def.init(this);
        for (PropertyDefinitionCategory cat : categories)
            cat.setPropertyClass(this);
    } catch (ClassNotFoundException cnfe) {
        throw new IOException("Deserialisation failed. " + cnfe.getMessage());
    } finally {
        ois.close();
    }
}

From source file:edu.uci.ics.jung.visualization.layout.PersistentLayoutImpl.java

/**
 * Restore the graph Vertex locations from a file
 * @param fileName the file to use/*from   www .  j a va  2s .co  m*/
 * @throws IOException for file problems
 * @throws ClassNotFoundException for classpath problems
 */
@SuppressWarnings("unchecked")
public void restore(String fileName) throws IOException, ClassNotFoundException {
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName));
    map = (Map) ois.readObject();
    ois.close();
    initializeLocations();
    locked = true;
    fireStateChanged();
}

From source file:MemComboBoxDemo.java

public void load(String fName) {
    try {//from  ww w . j av a2  s.c o  m
        if (getItemCount() > 0)
            removeAllItems();
        File f = new File(fName);
        if (!f.exists())
            return;
        FileInputStream fStream = new FileInputStream(f);
        ObjectInput stream = new ObjectInputStream(fStream);

        Object obj = stream.readObject();
        if (obj instanceof ComboBoxModel)
            setModel((ComboBoxModel) obj);

        stream.close();
        fStream.close();
    } catch (Exception e) {
        System.err.println("Serialization error: " + e.toString());
    }
}

From source file:com.jpeterson.littles3.bo.CanonicalUserTest.java

/**
 * Test that an instance is serializable.
 */// www .  j  a v a  2s.co  m
public void test_serialization1() {
    CanonicalUser user, reconstitutedUser;
    ByteArrayInputStream bais;
    ByteArrayOutputStream baos;
    ObjectInputStream ois;
    ObjectOutputStream oos;

    user = new CanonicalUser("test");

    try {
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);

        oos.writeObject(user);

        bais = new ByteArrayInputStream(baos.toByteArray());
        ois = new ObjectInputStream(bais);

        reconstitutedUser = (CanonicalUser) ois.readObject();

        assertEquals("Unexpected user", user, reconstitutedUser);
    } catch (IOException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    }
}

From source file:required.ChartPlotter.java

/**
 * Creates a sample dataset./*from  w  ww  . ja  v a2  s  .  com*/
 * 
 * @return a sample dataset.
 * @throws IOException 
 * @throws FileNotFoundException 
 * @throws ClassNotFoundException 
 */
@SuppressWarnings("unchecked")
private XYDataset createDataset() throws FileNotFoundException, IOException, ClassNotFoundException {
    LinkedHashMap<String, Long> lhash = new LinkedHashMap<>();
    try (InputStream file = new FileInputStream("SortedFreq.r");
            InputStream buffer = new BufferedInputStream(file);
            ObjectInput input = new ObjectInputStream(buffer);

    ) {
        //deserialize the List
        lhash = (LinkedHashMap<String, Long>) input.readObject();
        input.close();
        buffer.close();
        file.close();
        System.out.println(lhash.size());
    }
    final XYSeries series2 = new XYSeries("Second");
    Iterator<String> it = lhash.keySet().iterator();
    for (int i = 0; i < lhash.size(); i++) {
        if (it.hasNext())
            series2.add(i, lhash.get(it.next()));
    }

    final XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(series2);

    return dataset;

}

From source file:de.scoopgmbh.copper.persistent.StandardJavaSerializer.java

private Serializable deserialize(String _data, final WorkflowRepository wfRepo)
        throws IOException, ClassNotFoundException, DataFormatException {
    if (_data == null)
        return null;
    boolean isCompressed = _data.charAt(0) == 'C';
    byte[] data = Base64.decodeBase64(_data.substring(1));
    if (isCompressed) {
        data = compressorTL.get().uncompress(data);
    }/*from   ww w  .jav  a 2  s. c om*/
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    ObjectInputStream ois = wfRepo != null ? new ObjectInputStream(bais) {
        @Override
        protected java.lang.Class<?> resolveClass(java.io.ObjectStreamClass desc)
                throws java.io.IOException, ClassNotFoundException {
            return wfRepo.resolveClass(desc);
        };
    } : new ObjectInputStream(bais);
    Serializable o = (Serializable) ois.readObject();
    ois.close();
    return o;
}