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:geva.Main.State.java

/**
 * Load a State//from w  w  w .  java 2  s.c  o  m
 * @param fileName Name of file to load
 **/
@SuppressWarnings({ "IOResourceOpenedButNotSafelyClosed" })
public void load(String fileName) {
    FileInputStream fIn;
    ObjectInputStream oIn;
    try {
        fIn = new FileInputStream(fileName);
        oIn = new ObjectInputStream(fIn);

        State emp = (State) oIn.readObject();
        logger.info(emp);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:net.sf.dynamicreports.googlecharts.test.AbstractJasperTest.java

private JasperReportBuilder serializableTest(JasperReportBuilder report)
        throws IOException, ClassNotFoundException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(report);//w ww.  j a v a  2  s.co  m
    oos.flush();
    oos.close();

    InputStream stream = new ByteArrayInputStream(bos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(stream);
    return (JasperReportBuilder) ois.readObject();
}

From source file:com.lurencun.cfuture09.androidkit.http.async.PersistentCookieStore.java

protected Cookie decodeCookie(String cookieStr) {
    byte[] bytes = StringUtil.hexStringToByteArray(cookieStr);
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    Cookie cookie = null;//from w w  w .  j av  a2s .c o m
    try {
        ObjectInputStream ois = new ObjectInputStream(is);
        cookie = ((SerializableCookie) ois.readObject()).getCookie();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return cookie;
}

From source file:de.tudarmstadt.ukp.dkpro.core.io.bincas.BinaryCasReader.java

@Override
public void getNext(CAS aCAS) throws IOException, CollectionException {
    Resource res = nextFile();//w  w w  .  j ava2  s.  co m
    InputStream is = null;
    try {
        is = CompressionUtils.getInputStream(res.getLocation(), res.getInputStream());
        BufferedInputStream bis = new BufferedInputStream(is);

        TypeSystemImpl ts = null;

        // Check if this is original UIMA CAS format or DKPro Core format
        bis.mark(10);
        DataInputStream dis = new DataInputStream(bis);
        byte[] dkproHeader = new byte[] { 'D', 'K', 'P', 'r', 'o', '1' };
        byte[] header = new byte[dkproHeader.length];
        dis.read(header);

        // If it is DKPro Core format, read the type system
        if (Arrays.equals(header, dkproHeader)) {
            ObjectInputStream ois = new ObjectInputStream(bis);
            CASMgrSerializer casMgrSerializer = (CASMgrSerializer) ois.readObject();
            ts = casMgrSerializer.getTypeSystem();
            ts.commit();
        } else {
            bis.reset();
        }

        if (ts == null) {
            // Check if this is a UIMA binary CAS stream
            byte[] uimaHeader = new byte[] { 'U', 'I', 'M', 'A' };

            byte[] header4 = new byte[uimaHeader.length];
            System.arraycopy(header, 0, header4, 0, header4.length);

            if (header4[0] != 'U') {
                ArrayUtils.reverse(header4);
            }

            // If it is not a UIMA binary CAS stream, assume it is output from
            // SerializedCasWriter
            if (!Arrays.equals(header4, uimaHeader)) {
                ObjectInputStream ois = new ObjectInputStream(bis);
                CASCompleteSerializer serializer = (CASCompleteSerializer) ois.readObject();
                deserializeCASComplete(serializer, (CASImpl) aCAS);
            } else {
                // Since there was no type system, it must be type 0 or 4
                deserializeCAS(aCAS, bis);
            }
        } else {
            // Only format 6 can have type system information
            deserializeCAS(aCAS, bis, ts, null);
        }
    } catch (ResourceInitializationException e) {
        throw new IOException(e);
    } catch (ClassNotFoundException e) {
        throw new IOException(e);
    } finally {
        closeQuietly(is);
    }
}

From source file:de.tudarmstadt.ukp.dkpro.tc.ml.report.BatchPredictionReport.java

@Override
public void execute() throws Exception {
    StorageService store = getContext().getStorageService();

    FlexTable<String> table = FlexTable.forClass(String.class);

    for (TaskContextMetadata subcontext : getSubtasks()) {
        // FIXME this is a bad hack
        if (subcontext.getType().contains("ExtractFeaturesAndPredictTask")) {

            Map<String, String> discriminatorsMap = store
                    .retrieveBinary(subcontext.getId(), Task.DISCRIMINATORS_KEY, new PropertiesAdapter())
                    .getMap();/*  w  ww.  ja  va 2s. co m*/

            // deserialize file
            FileInputStream f = new FileInputStream(
                    store.getStorageFolder(subcontext.getId(), PREDICTION_MAP_FILE_NAME));
            ObjectInputStream s = new ObjectInputStream(f);
            Map<String, List<String>> resultMap = (Map<String, List<String>>) s.readObject();
            s.close();

            // write one file per batch
            // in files: one line per instance

            for (String id : resultMap.keySet()) {
                Map<String, String> row = new HashMap<String, String>();
                row.put(predicted_value, StringUtils.join(resultMap.get(id), ","));
                table.addRow(id, row);
            }
            // create a separate output folder for each execution of
            // ExtractFeaturesAndPredictTask, 36 is the length of the UUID hash
            File contextFolder = store.getStorageFolder(getContext().getId(),
                    subcontext.getId().substring(subcontext.getId().length() - 36));
            // Excel cannot cope with more than 255 columns
            if (table.getColumnIds().length <= 255) {
                getContext().storeBinary(contextFolder.getName() + System.getProperty("file.separator")
                        + report_name + SUFFIX_EXCEL, table.getExcelWriter());
            }
            getContext().storeBinary(
                    contextFolder.getName() + System.getProperty("file.separator") + report_name + SUFFIX_CSV,
                    table.getCsvWriter());
            getContext().storeBinary(
                    contextFolder.getName() + System.getProperty("file.separator") + Task.DISCRIMINATORS_KEY,
                    new PropertiesAdapter(discriminatorsMap));
        }
    }

    // output the location of the batch evaluation folder
    // otherwise it might be hard for novice users to locate this
    File dummyFolder = store.getStorageFolder(getContext().getId(), "dummy");
    // TODO can we also do this without creating and deleting the dummy folder?
    getContext().getLoggingService().message(getContextLabel(),
            "Storing detailed results in:\n" + dummyFolder.getParent() + "\n");
    dummyFolder.delete();
}

From source file:de.tudarmstadt.ukp.dkpro.tc.weka.report.WekaBatchPredictionReport.java

@Override
public void execute() throws Exception {
    StorageService store = getContext().getStorageService();

    FlexTable<String> table = FlexTable.forClass(String.class);

    for (TaskContextMetadata subcontext : getSubtasks()) {
        if (subcontext.getType().startsWith(ExtractFeaturesAndPredictTask.class.getName())) {

            Map<String, String> discriminatorsMap = store
                    .retrieveBinary(subcontext.getId(), Task.DISCRIMINATORS_KEY, new PropertiesAdapter())
                    .getMap();// w  w  w  .jav a  2 s .  c  o m

            // deserialize file
            FileInputStream f = new FileInputStream(store.getStorageFolder(subcontext.getId(),
                    ExtractFeaturesAndPredictConnector.PREDICTION_MAP_FILE_NAME));
            ObjectInputStream s = new ObjectInputStream(f);
            Map<String, List<String>> resultMap = (Map<String, List<String>>) s.readObject();
            s.close();

            // write one file per batch
            // in files: one line per instance

            for (String id : resultMap.keySet()) {
                Map<String, String> row = new HashMap<String, String>();
                row.put(predicted_value, StringUtils.join(resultMap.get(id), ","));
                table.addRow(id, row);
            }
            // create a separate output folder for each execution of
            // ExtractFeaturesAndPredictTask, 36 is the length of the UUID hash
            File contextFolder = store.getStorageFolder(getContext().getId(),
                    subcontext.getId().substring(subcontext.getId().length() - 36));
            // Excel cannot cope with more than 255 columns
            if (table.getColumnIds().length <= 255) {
                getContext().storeBinary(contextFolder.getName() + System.getProperty("file.separator")
                        + report_name + SUFFIX_EXCEL, table.getExcelWriter());
            }
            getContext().storeBinary(
                    contextFolder.getName() + System.getProperty("file.separator") + report_name + SUFFIX_CSV,
                    table.getCsvWriter());
            getContext().storeBinary(
                    contextFolder.getName() + System.getProperty("file.separator") + Task.DISCRIMINATORS_KEY,
                    new PropertiesAdapter(discriminatorsMap));
        }
    }

    // output the location of the batch evaluation folder
    // otherwise it might be hard for novice users to locate this
    File dummyFolder = store.getStorageFolder(getContext().getId(), "dummy");
    // TODO can we also do this without creating and deleting the dummy folder?
    getContext().getLoggingService().message(getContextLabel(),
            "Storing detailed results in:\n" + dummyFolder.getParent() + "\n");
    dummyFolder.delete();
}

From source file:org.obiba.onyx.magma.ScriptedVariableValueSourceFactory.java

public Set<VariableValueSource> createSources() {
    if (resource.exists() == false) {
        return ImmutableSet.of();
    }/*from   www . j  a  v a 2s  .c  om*/

    try {
        ImmutableSet.Builder<VariableValueSource> sources = new ImmutableSet.Builder<VariableValueSource>();
        ObjectInputStream ois = getXStream().createObjectInputStream(resource.getInputStream());
        try {
            while (true) {
                Variable variable = (Variable) ois.readObject();
                sources.add(new JavascriptVariableValueSource(variable));
            }
        } catch (EOFException e) {
            // Reached the end of the file.
        } finally {
            ois.close();
        }
        return sources.build();
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:captureplugin.drivers.defaultdriver.ParamEntry.java

/**
 * Read Data from Stream//from w w w  .j a v  a2 s  .c  o  m
 * @param in read data from this stream
 * @throws IOException read errors
 * @throws ClassNotFoundException problems while creating classes
 */
public void readData(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
    int version = in.readInt();
    mName = (String) in.readObject();
    mParam = (String) in.readObject();

    mEnabled = version < 2 || in.readBoolean();
}

From source file:org.agiso.tempel.core.processor.xstream.XStreamTempelFileProcessor.java

@Override
public void process(InputStream xmlStream, ITempelEntryProcessor entryProcessor) throws Exception {
    ObjectInputStream in = xStream.createObjectInputStream(xmlStream);
    try {/*from  w w w. j  a  v  a 2s. c  om*/
        while (true) {
            entryProcessor.processObject(in.readObject());
        }
    } catch (EOFException e) {
        // Normalne zakoczenie przetwarzania pliku szablonu
    }
}