Example usage for java.io ObjectInput close

List of usage examples for java.io ObjectInput close

Introduction

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

Prototype

public void close() throws IOException;

Source Link

Document

Closes the input stream.

Usage

From source file:org.jaqpot.algorithm.resource.Standarization.java

@POST
@Path("prediction")
public Response prediction(PredictionRequest request) {
    try {// w  w  w.  j a va2  s  .c o  m
        if (request.getDataset().getDataEntry().isEmpty()
                || request.getDataset().getDataEntry().get(0).getValues().isEmpty()) {
            return Response
                    .status(Response.Status.BAD_REQUEST).entity(ErrorReportFactory
                            .badRequest("Dataset is empty", "Cannot make predictions on empty dataset"))
                    .build();
        }
        List<String> features = request.getDataset().getDataEntry().stream().findFirst().get().getValues()
                .keySet().stream().collect(Collectors.toList());
        String base64Model = (String) request.getRawModel();
        byte[] modelBytes = Base64.getDecoder().decode(base64Model);
        ByteArrayInputStream bais = new ByteArrayInputStream(modelBytes);
        ObjectInput in = new ObjectInputStream(bais);
        ScalingModel model = (ScalingModel) in.readObject();
        in.close();
        bais.close();

        List<LinkedHashMap<String, Object>> predictions = new ArrayList<>();

        for (DataEntry dataEntry : request.getDataset().getDataEntry()) {
            LinkedHashMap<String, Object> data = new LinkedHashMap<>();
            for (String feature : features) {
                Double stdev = model.getMaxValues().get(feature);
                Double mean = model.getMinValues().get(feature);
                Double value = Double.parseDouble(dataEntry.getValues().get(feature).toString());
                if (stdev != null && stdev != 0.0 && mean != null) {
                    value = (value - mean) / stdev;
                } else {
                    value = 1.0;
                }
                data.put("Standarized " + feature, value);
            }
            predictions.add(data);
        }

        PredictionResponse response = new PredictionResponse();
        response.setPredictions(predictions);

        return Response.ok(response).build();
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, null, ex);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ex.getMessage()).build();
    }
}

From source file:com.act.reachables.ActData.java

public void deserialize(String fromFile) {
    try {//  ww w . jav  a2 s  .  c o  m
        InputStream file = new FileInputStream(fromFile);
        InputStream buffer = new BufferedInputStream(file);
        ObjectInput input = new ObjectInputStream(buffer);
        try {
            ActData._instance = (ActData) input.readObject();
        } finally {
            input.close();
        }
    } catch (ClassNotFoundException ex) {
        throw new RuntimeException("ActData deserialize failed: Class not found: " + ex);
    } catch (IOException ex) {
        throw new RuntimeException("ActData deserialize failed: IO problem: " + ex);
    }
}

From source file:org.jfree.chart.demo.ChartPanelSerializationTest.java

/**
 * A demonstration application showing how to create a simple time series chart.  This
 * example uses monthly data.// www  .  java 2s.  com
 *
 * @param title  the frame title.
 */
public ChartPanelSerializationTest(final String title) {

    super(title);
    final XYDataset dataset = createDataset();
    final JFreeChart chart = createChart(dataset);
    final ChartPanel chartPanel1 = new ChartPanel(chart);
    chartPanel1.setPreferredSize(new java.awt.Dimension(500, 270));
    chartPanel1.setMouseZoomable(true, false);

    ChartPanel chartPanel2 = null;
    try {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(chartPanel1);
        out.close();

        ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
        chartPanel2 = (ChartPanel) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    setContentPane(chartPanel2);

}

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

/**
 * Serialize an instance, restore it, and check for equality.
 *///w w w .ja  v a 2s .  c o m
public void testSerialization() {
    VectorSeries s1 = new VectorSeries("Series");
    s1.add(1.0, 1.1, 1.2, 1.3);
    VectorSeriesCollection c1 = new VectorSeriesCollection();
    c1.addSeries(s1);
    VectorSeriesCollection 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 = (VectorSeriesCollection) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertEquals(c1, c2);
}

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

/**
 * Serialize an instance, restore it, and check for equality.
 *//*from   ww  w .java 2  s.c  om*/
public void testSerialization() {
    DefaultOHLCDataset d1 = new DefaultOHLCDataset("Series 1", new OHLCDataItem[0]);
    DefaultOHLCDataset d2 = null;

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

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

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

/**
 * Serialize an instance, restore it, and check for equality.
 *//*from  w ww.j  a va  2  s.co m*/
public void testSerialization() {
    DefaultHighLowDataset d1 = new DefaultHighLowDataset("Series 1", new Date[] { new Date(123L) },
            new double[] { 1.2 }, new double[] { 3.4 }, new double[] { 5.6 }, new double[] { 7.8 },
            new double[] { 99.9 });
    DefaultHighLowDataset d2 = null;

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

        ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
        d2 = (DefaultHighLowDataset) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertEquals(d1, d2);
}

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

/**
 * Serialize an instance, restore it, and check for equality.
 *///  w w w  .  j  a va 2  s  .  c o m
public void testSerialization() {
    XIntervalSeriesCollection c1 = new XIntervalSeriesCollection();
    XIntervalSeries s1 = new XIntervalSeries("Series");
    s1.add(1.0, 1.1, 1.2, 1.3);
    XIntervalSeriesCollection 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 = (XIntervalSeriesCollection) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertEquals(c1, c2);
}

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

/**
 * Serialize an instance, restore it, and check for equality.
 *//* w  w  w . jav  a 2 s  .  co m*/
public void testSerialization() {
    YIntervalSeriesCollection c1 = new YIntervalSeriesCollection();
    YIntervalSeries s1 = new YIntervalSeries("Series");
    s1.add(1.0, 1.1, 1.2, 1.3);
    YIntervalSeriesCollection 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 = (YIntervalSeriesCollection) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertEquals(c1, c2);
}

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

/**
 * Serialize an instance, restore it, and check for equality.
 *//*from www .j  a v a 2  s  . c om*/
public void testSerialization() {
    XYIntervalSeriesCollection c1 = new XYIntervalSeriesCollection();
    XYIntervalSeries s1 = new XYIntervalSeries("Series");
    s1.add(1.0, 1.1, 1.2, 1.3, 1.4, 1.5);
    XYIntervalSeriesCollection 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 = (XYIntervalSeriesCollection) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertEquals(c1, c2);

    // check independence
    c1.addSeries(new XYIntervalSeries("Empty"));
    assertFalse(c1.equals(c2));
    c2.addSeries(new XYIntervalSeries("Empty"));
    assertTrue(c1.equals(c2));
}

From source file:org.apache.stratos.mock.iaas.persistence.RegistryManager.java

/**
 * Deserialize a byte array and retrieve the object.
 *
 * @param bytes bytes to be deserialized
 * @return the deserialized {@link Object}
 * @throws Exception if the deserialization is failed.
 *///ww  w  .java2 s .c  o  m
private Object deserializeFromByteArray(byte[] bytes) throws Exception {

    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    ObjectInput in = null;
    try {
        in = new ObjectInputStream(bis);
        return in.readObject();
    } finally {
        bis.close();
        if (in != null) {
            in.close();
        }
    }
}