Example usage for java.io ObjectOutput writeObject

List of usage examples for java.io ObjectOutput writeObject

Introduction

In this page you can find the example usage for java.io ObjectOutput writeObject.

Prototype

public void writeObject(Object obj) throws IOException;

Source Link

Document

Write an object to the underlying storage or stream.

Usage

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

public void serialize(String toFile) {
    try {/*from   www . j  a  v  a 2 s .  co m*/
        OutputStream file = new FileOutputStream(toFile);
        OutputStream buffer = new BufferedOutputStream(file);
        ObjectOutput output = new ObjectOutputStream(buffer);
        try {
            output.writeObject(_instance);
        } finally {
            output.close();
        }
    } catch (IOException ex) {
        throw new RuntimeException("ActData serialize failed: " + ex);
    }
}

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

/**
 * Serialize an instance, restore it, and check for equality.
 *//*from w w  w .j av  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.chart.demo.ChartPanelSerializationTest.java

/**
 * A demonstration application showing how to create a simple time series chart.  This
 * example uses monthly data.//from   w  ww  . ja  v a2 s  .  c o m
 *
 * @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.apache.camel.component.bean.BeanInvocation.java

public void writeExternal(ObjectOutput objectOutput) throws IOException {
    if (methodBean == null) {
        methodBean = new MethodBean(method);
    }/*  www .  j  a v a 2s  .co m*/
    objectOutput.writeObject(methodBean);
    objectOutput.writeObject(args);
}

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

/**
 * Serialize an instance, restore it, and check for equality.
 *//*w w  w  . java2  s  .c  o m*/
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  ava 2s . c o 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.jaqpot.algorithms.resource.Standarization.java

@POST
@Path("training")
public Response training(TrainingRequest request) {
    try {/*  www  .j a va 2  s.  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 train model on empty dataset.").build();
        }
        List<String> features = request.getDataset().getDataEntry().stream().findFirst().get().getValues()
                .keySet().stream().filter(feature -> !feature.equals(request.getPredictionFeature()))
                .collect(Collectors.toList());

        LinkedHashMap<String, Double> maxValues = new LinkedHashMap<>();
        LinkedHashMap<String, Double> minValues = new LinkedHashMap<>();

        features.stream().forEach(feature -> {
            List<Double> values = request.getDataset().getDataEntry().stream().map(dataEntry -> {
                return Double.parseDouble(dataEntry.getValues().get(feature).toString());
            }).collect(Collectors.toList());
            double[] doubleValues = values.stream().mapToDouble(Double::doubleValue).toArray();

            Double mean = StatUtils.mean(doubleValues);
            Double stddev = Math.sqrt(StatUtils.variance(doubleValues));

            maxValues.put(feature, stddev);
            minValues.put(feature, mean);
        });
        ScalingModel model = new ScalingModel();
        model.setMaxValues(maxValues);
        model.setMinValues(minValues);

        TrainingResponse response = new TrainingResponse();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(baos);
        out.writeObject(model);
        String base64Model = Base64.getEncoder().encodeToString(baos.toByteArray());
        response.setRawModel(base64Model);
        response.setIndependentFeatures(features);
        response.setPredictedFeatures(features.stream().map(feature -> {
            return "Standarized " + feature;
        }).collect(Collectors.toList()));
        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.splicemachine.derby.stream.function.TableScanQualifierFunction.java

@Override
public void writeExternal(ObjectOutput out) throws IOException {
    super.writeExternal(out);
    out.writeBoolean(optionalProbeValue != null);
    if (optionalProbeValue != null)
        out.writeObject(optionalProbeValue);
}

From source file:com.conwet.silbops.model.SubscriptionTest.java

@Test
public void shouldExternalize() throws Exception {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutput output = new ObjectOutputStream(baos);

    output.writeObject(subscription);
    output.close();/*from w w w.j a  v  a 2  s  . co m*/

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

    assertThat((Subscription) input.readObject()).isEqualTo(subscription);
}

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

@POST
@Path("training")
public Response training(TrainingRequest request) {
    try {/*from  w  ww.j  a  va2s  . co 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 train model on empty dataset"))
                    .build();
        }
        List<String> features = request.getDataset().getDataEntry().stream().findFirst().get().getValues()
                .keySet().stream().filter(feature -> !feature.equals(request.getPredictionFeature()))
                .collect(Collectors.toList());

        LinkedHashMap<String, Double> maxValues = new LinkedHashMap<>();
        LinkedHashMap<String, Double> minValues = new LinkedHashMap<>();

        features.stream().forEach(feature -> {
            List<Double> values = request.getDataset().getDataEntry().stream().map(dataEntry -> {
                return Double.parseDouble(dataEntry.getValues().get(feature).toString());
            }).collect(Collectors.toList());
            double[] doubleValues = values.stream().mapToDouble(Double::doubleValue).toArray();

            Double mean = StatUtils.mean(doubleValues);
            Double stddev = Math.sqrt(StatUtils.variance(doubleValues));

            maxValues.put(feature, stddev);
            minValues.put(feature, mean);
        });
        ScalingModel model = new ScalingModel();
        model.setMaxValues(maxValues);
        model.setMinValues(minValues);

        TrainingResponse response = new TrainingResponse();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(baos);
        out.writeObject(model);
        String base64Model = Base64.getEncoder().encodeToString(baos.toByteArray());
        response.setRawModel(base64Model);
        response.setIndependentFeatures(features);
        response.setPredictedFeatures(features.stream().map(feature -> {
            return "Standarized " + feature;
        }).collect(Collectors.toList()));
        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();
    }
}