Example usage for java.io ObjectInputStream close

List of usage examples for java.io ObjectInputStream close

Introduction

In this page you can find the example usage for java.io ObjectInputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes the input stream.

Usage

From source file:com.yahoo.labs.yamall.local.Yamall.java

private static double evalHoldoutError() throws FileNotFoundException, IOException, ClassNotFoundException {
    double cumLoss = 0;
    double weightedSampleSum = 0;
    ObjectInputStream oin = new ObjectInputStream(new FileInputStream("cache_holdout.bin"));

    Instance testSample;//from   w  w  w  .jav  a  2 s  .c o m
    while ((testSample = (Instance) oin.readObject()) != null) {
        weightedSampleSum += testSample.getWeight();
        double score = learner.predict(testSample);
        score = Math.min(Math.max(score, minPrediction), maxPrediction);
        if (!binary)
            cumLoss += learner.getLoss().lossValue(score, testSample.getLabel()) * testSample.getWeight();
        else if (Math.signum(score) != testSample.getLabel())
            cumLoss += testSample.getWeight();
    }
    oin.close();

    return cumLoss / weightedSampleSum;
}

From source file:cerrla.LocalCrossEntropyDistribution.java

/**
 * Loads a serialised {@link LocalCrossEntropyDistribution} from file (if it
 * exists).//from w ww . jav a  2 s. c om
 * 
 * @param serializedFile
 *            The serialised file.
 * @return The loaded distribution, or null.
 */
public static LocalCrossEntropyDistribution loadDistribution(File serializedFile) {
    try {
        if (serializedFile.exists()) {
            // The file exists!
            FileInputStream fis = new FileInputStream(serializedFile);
            ObjectInputStream ois = new ObjectInputStream(fis);
            LocalCrossEntropyDistribution lced = (LocalCrossEntropyDistribution) ois.readObject();
            ois.close();
            fis.close();

            // Load Local Agent Observations
            lced.localAgentObservations_ = LocalAgentObservations.loadAgentObservations(lced.getGoalCondition(),
                    lced);
            lced.policyGenerator_.rebuildCurrentData();
            lced.isSpecialising_ = true;
            lced.setState(AlgorithmState.TRAINING);

            return lced;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:SerialCloneTest.java

public Object clone() {
    try {/*from  w  w  w  .ja v a  2  s.  co m*/
        // save the object to a byte array
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bout);
        out.writeObject(this);
        out.close();

        // read a clone of the object from the byte array
        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
        ObjectInputStream in = new ObjectInputStream(bin);
        Object ret = in.readObject();
        in.close();

        return ret;
    } catch (Exception e) {
        return null;
    }
}

From source file:gumga.framework.application.GumgaTempFileService.java

public GumgaFile find(String tempFileName) {
    if (tempFileName == null || tempFileName.isEmpty()) {
        return null;
    }/*from w  w  w .j  a  va 2s  .c  om*/
    try { //TODO Melhorar o tratamento da Exception para FileNotFound
        FileInputStream fis = new FileInputStream(gumgaValues.getUploadTempDir() + "/" + tempFileName);
        ObjectInputStream ois = new ObjectInputStream(fis);
        GumgaFile gf = (GumgaFile) ois.readObject();
        ois.close();
        fis.close();
        return gf;
    } catch (Exception ex) {
        log.error("erro ao recuperar arquivo temporario " + tempFileName, ex);
    }
    return null;
}

From source file:com.heliosapm.tsdblite.json.JSON.java

private static Object jdeserialize(final byte[] bytes) {
    if (bytes == null || bytes.length == 0)
        return null;
    ByteArrayInputStream bais = null;
    ObjectInputStream ois = null;
    try {/*from w  w w.j  a  va 2 s. c om*/
        bais = new ByteArrayInputStream(bytes);
        ois = new ObjectInputStream(bais);
        return ois.readObject();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        if (ois != null)
            try {
                ois.close();
            } catch (Exception x) {
                /* No Op */}
        if (bais != null)
            try {
                bais.close();
            } catch (Exception x) {
                /* No Op */}
    }
}

From source file:com.winvector.logistic.demo.MapReduceScore.java

public double run(final String modelFileName, final String testFileName, final String resultFileName)
        throws Exception {
    final Log log = LogFactory.getLog(MapReduceScore.class);
    final Random rand = new Random();
    final String tmpPrefix = "TMPAC_" + rand.nextLong();
    final Configuration mrConfig = getConf();
    log.info("start");
    log.info("reading model: " + modelFileName);
    final Model model;
    {//from w w  w . ja v a 2s.c o m
        final Path modelPath = new Path(modelFileName);
        final FSDataInputStream fdi = modelPath.getFileSystem(mrConfig).open(modelPath);
        final ObjectInputStream ois = new ObjectInputStream(fdi);
        model = (Model) ois.readObject();
        ois.close();
    }
    log.info("model:\n" + model.config.formatSoln(model.coefs));
    final Path testFile = new Path(testFileName);
    final Path resultFile = new Path(resultFileName);
    log.info("scoring data: " + testFile);
    log.info("writing: " + resultFile);
    final SigmoidLossMultinomial underlying = new SigmoidLossMultinomial(model.config.dim(),
            model.config.noutcomes());
    final WritableVariableList lConfig = WritableVariableList.copy(model.config.def());
    final String headerLine = WritableUtils.readFirstLine(mrConfig, testFile);
    final Pattern sepPattern = Pattern.compile("\t");
    final LineBurster burster = new HBurster(sepPattern, headerLine, false);
    mrConfig.set(MapRedScan.BURSTERSERFIELD, SerialUtils.serializableToString(burster));
    final StringBuilder b = new StringBuilder();
    b.append("predict" + "." + model.config.def().resultColumn + "\t");
    b.append("predict" + "." + model.config.def().resultColumn + "." + "score" + "\t");
    for (int i = 0; i < model.config.noutcomes(); ++i) {
        final String cat = model.config.outcome(i);
        b.append("predict" + "." + model.config.def().resultColumn + "." + cat + "." + "score" + "\t");
    }
    b.append(headerLine);
    mrConfig.set(MapRedScore.IDEALHEADERFIELD, b.toString());
    final MapRedScore sc = new MapRedScore(underlying, lConfig, model.config.useIntercept(), mrConfig,
            testFile);
    sc.score(model.coefs, resultFile);
    final MapRedAccuracy ac = new MapRedAccuracy(underlying, lConfig, model.config.useIntercept(), tmpPrefix,
            mrConfig, testFile);
    final long[] testAccuracy = ac.score(model.coefs);
    final double accuracy = testAccuracy[0] / (double) testAccuracy[1];
    log.info("test accuracy: " + testAccuracy[0] + "/" + testAccuracy[1] + "\t" + accuracy);
    log.info("done");
    return accuracy;
}

From source file:io.gumga.application.GumgaTempFileService.java

/**
 * Encontrar o arquivo temporario//from  w w  w .  j a  va 2  s .co  m
 * @param tempFileName nome do arquivo a ser procurado
 * @return arquivo
 */
public GumgaFile find(String tempFileName) {
    if (tempFileName == null || tempFileName.isEmpty()) {
        return null;
    }
    try { //TODO Melhorar o tratamento da Exception para FileNotFound
        FileInputStream fis = new FileInputStream(gumgaValues.getUploadTempDir() + "/" + tempFileName);
        ObjectInputStream ois = new ObjectInputStream(fis);
        GumgaFile gf = (GumgaFile) ois.readObject();
        ois.close();
        fis.close();
        return gf;
    } catch (Exception ex) {
        log.error("erro ao recuperar arquivo temporario " + tempFileName, ex);
    }
    return null;
}

From source file:ch.epfl.leb.sass.ijplugin.IJPluginModel.java

/**
 * Loads a model from a file.//ww w  . j ava 2  s . c o  m
 * 
 * @param fileIn The input stream from the file.
 */
public static IJPluginModel read(FileInputStream fileIn) {
    IJPluginModel model = null;
    try {
        ObjectInputStream in = new ObjectInputStream(fileIn);
        model = (IJPluginModel) in.readObject();
        in.close();
        fileIn.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    } catch (ClassNotFoundException c) {
        c.printStackTrace();
    }

    return model;
}

From source file:com.enonic.cms.core.portal.instruction.PostProcessInstruction.java

public final void deserialize(String value) throws IOException, ClassNotFoundException {
    ByteArrayInputStream in = new ByteArrayInputStream(Base64.decodeBase64(value.getBytes()));

    ObjectInputStream din = new ObjectInputStream(in);

    readExternal(din);//from   www. j a v a 2 s. c  o  m

    in.close();
    din.close();
}

From source file:com.acme.CodeDrivenProcessor.java

public CodeDrivenProcessor(String resource) {
    // 1. load the resource
    // 2. deserialize the lambda
    System.out.println("resource=" + resource);
    if (resource == null)
        return;//  w ww  .j ava 2s  .  c o m
    try {
        // InputStream is = new FileInputStream(new File("/tmp/bytes"));//
        InputStream is = this.getClass().getClassLoader().getResourceAsStream(resource);
        if (is == null)
            return;
        System.out.println("deserializing");
        ObjectInputStream ois = new ObjectInputStream(is);
        delegate = (Processor<Tuple, Tuple>) ois.readObject();
        ois.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}