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:net.brtly.monkeyboard.api.plugin.Bundle.java

/**
 * Factory method that creates a Bundle by deserializing a byte array
 * produced by {@link #toByteArray(Bundle)}
 * /*www  .ja  va2  s  . c o  m*/
 * @param b
 * @return
 * @throws IOException
 * @throws ClassNotFoundException
 */
public static Bundle fromByteArray(byte[] b) throws IOException, ClassNotFoundException {
    ByteArrayInputStream bis = new ByteArrayInputStream(b);
    ObjectInput in = null;
    try {
        in = new ObjectInputStream(bis);
        return (Bundle) in.readObject();
    } finally {
        bis.close();
        in.close();
    }
}

From source file:io.bitsquare.common.util.Utilities.java

public static <T extends Serializable> T deserialize(byte[] data) {
    ByteArrayInputStream bis = new ByteArrayInputStream(data);
    ObjectInput in = null;
    Object result = null;/*from  w w  w. ja v  a 2 s  . c  o  m*/
    try {
        in = new LookAheadObjectInputStream(bis, true);
        result = in.readObject();
        if (!(result instanceof Serializable))
            throw new RuntimeException("Object not of type Serializable");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            bis.close();
        } catch (IOException ex) {
            // ignore close exception
        }
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
            // ignore close exception
        }
    }
    return (T) result;
}

From source file:com.gsma.mobileconnect.discovery.DiscoveryResponseTest.java

private DiscoveryResponse roundTripSerialize(DiscoveryResponse in) throws IOException, ClassNotFoundException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutput output = new ObjectOutputStream(baos);

    output.writeObject(in);/* w  w  w.  j a va2s .com*/

    output.close();

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ObjectInput input = new ObjectInputStream(bais);

    DiscoveryResponse out = (DiscoveryResponse) input.readObject();

    input.close();

    return out;
}

From source file:com.rr.familyPlanning.ui.security.decryptObject.java

/**
 * Decrypts the String and serializes the object
 *
 * @param base64Data/*from  w  ww . j a  va 2  s .  c  om*/
 * @param base64IV
 * @return
 * @throws Exception
 */
public Object decryptObject(String base64Data, String base64IV) throws Exception {
    // Decode the data
    byte[] encryptedData = Base64.decodeBase64(base64Data.getBytes());

    // Decode the Init Vector
    byte[] rawIV = Base64.decodeBase64(base64IV.getBytes());

    // Configure the Cipher
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec ivSpec = new IvParameterSpec(rawIV);
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    digest.update(keyString.getBytes());
    byte[] key = new byte[16];
    System.arraycopy(digest.digest(), 0, key, 0, key.length);
    SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
    cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

    // Decrypt the data..
    byte[] decrypted = cipher.doFinal(encryptedData);

    // Deserialize the object
    ByteArrayInputStream stream = new ByteArrayInputStream(decrypted);

    ObjectInput in = new ObjectInputStream(stream);
    Object obj = null;
    try {
        obj = in.readObject();
    } finally {
        stream.close();
        in.close();
    }
    return obj;
}

From source file:org.jfree.experimental.chart.annotations.junit.XYTitleAnnotationTests.java

/**
 * Serialize an instance, restore it, and check for equality.
 *///  w  w w.j  a v  a  2 s  . co  m
public void testSerialization() {
    TextTitle t = new TextTitle("Title");
    XYTitleAnnotation a1 = new XYTitleAnnotation(1.0, 2.0, t);
    XYTitleAnnotation a2 = null;
    try {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(a1);
        out.close();

        ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
        a2 = (XYTitleAnnotation) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertEquals(a1, a2);
}

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

/**
 * Constructs a new demonstration application.
 *
 * @param title  the frame title.//from  w  ww  .  j  ava 2 s  .c  om
 */
public SerializationTest1(final String title) {

    super(title);
    this.series = new TimeSeries("Random Data", Millisecond.class);
    TimeSeriesCollection dataset = new TimeSeriesCollection(this.series);
    JFreeChart chart = createChart(dataset);

    // SERIALIZE - DESERIALIZE for testing purposes
    JFreeChart deserializedChart = null;

    try {
        final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        final ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(chart);
        out.close();
        chart = null;
        dataset = null;
        this.series = null;
        System.gc();

        final ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
        deserializedChart = (JFreeChart) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    final TimeSeriesCollection c = (TimeSeriesCollection) deserializedChart.getXYPlot().getDataset();
    this.series = c.getSeries(0);
    // FINISHED TEST

    final ChartPanel chartPanel = new ChartPanel(deserializedChart);
    final JButton button = new JButton("Add New Data Item");
    button.setActionCommand("ADD_DATA");
    button.addActionListener(this);

    final JPanel content = new JPanel(new BorderLayout());
    content.add(chartPanel);
    content.add(button, BorderLayout.SOUTH);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    setContentPane(content);

}

From source file:MemComboBoxDemo.java

public void load(String fName) {
    try {//from  www  .ja  va  2s .  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:org.jaqpot.algorithms.resource.Standarization.java

@POST
@Path("prediction")
public Response prediction(PredictionRequest request) {
    try {//  w  w  w  .  ja v  a  2s .c  o  m
        if (request.getDataset().getDataEntry().isEmpty()
                || request.getDataset().getDataEntry().get(0).getValues().isEmpty()) {
            return Response.status(Response.Status.BAD_REQUEST)
                    .entity("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:org.jfree.data.xy.junit.MatrixSeriesCollectionTest.java

/**
 * Serialize an instance, restore it, and check for equality.
 *//*from  w w w.  j a va2 s  .c  o  m*/
public void testSerialization() {
    MatrixSeries s1 = new MatrixSeries("Series", 2, 3);
    s1.update(0, 0, 1.1);
    MatrixSeriesCollection c1 = new MatrixSeriesCollection();
    c1.addSeries(s1);
    MatrixSeriesCollection 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 = (MatrixSeriesCollection) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertEquals(c1, c2);
}

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

/**
 * Serialize an instance, restore it, and check for equality.
 *//*  www  .j  a  v a 2s . c om*/
public void testSerialization() {
    DefaultXYDataset d1 = new DefaultXYDataset();
    double[] x1 = new double[] { 1.0, 2.0, 3.0 };
    double[] y1 = new double[] { 4.0, 5.0, 6.0 };
    double[][] data1 = new double[][] { x1, y1 };
    d1.addSeries("S1", data1);
    XYBarDataset bd1 = new XYBarDataset(d1, 5.0);
    XYBarDataset bd2 = null;
    try {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(bd1);
        out.close();

        ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
        bd2 = (XYBarDataset) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertEquals(bd1, bd2);
}