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:org.jfree.data.xy.junit.XYIntervalSeriesCollectionTest.java

/**
 * Serialize an instance, restore it, and check for equality.
 *//*from  w w  w.j a  v  a2s  .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:xbird.xquery.expr.func.FunctionCall.java

public void writeExternal(ObjectOutput out) throws IOException {
    out.writeObject(_type);
    _funcName.writeExternal(out);//from  www  .  j  a  v  a 2  s.  com
    final List<XQExpression> params = _params;
    final int numParams = params.size();
    out.writeInt(numParams);
    for (XQExpression e : params) {
        out.writeObject(e);
    }
}

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

/**
 * Serialize an object to a byte array./*from ww  w.  j a v a 2 s .  c o m*/
 *
 * @param object object
 * @return byte array
 * @throws java.io.IOException
 */
private byte[] serializeToByteArray(Object object) throws IOException {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(object);
        return bos.toByteArray();

    } finally {
        if (out != null) {
            out.close();
        }
        bos.close();
    }
}

From source file:org.apache.synapse.message.store.impl.rabbitmq.RabbitMQProducer.java

public boolean storeMessage(MessageContext synCtx) {
    if (synCtx == null) {
        return false;
    }//from  w w  w  .j a v  a  2 s .com
    if (connection == null) {
        if (logger.isDebugEnabled()) {
            logger.error(getId() + " cannot proceed. RabbitMQ Connection is null.");
        }
        logger.warn(getId() + ". Ignored MessageID : " + synCtx.getMessageID());
        return false;
    }
    StorableMessage message = MessageConverter.toStorableMessage(synCtx);
    boolean error = false;
    Throwable throwable = null;
    Channel channel = null;
    try {
        //Serializing message
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ObjectOutput objOut = new ObjectOutputStream(os);
        objOut.writeObject(message);
        byte[] byteForm = os.toByteArray();
        objOut.close();
        os.close();
        //building AMQP message
        AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties().builder();
        builder.messageId(synCtx.getMessageID());
        builder.deliveryMode(MessageProperties.MINIMAL_PERSISTENT_BASIC.getDeliveryMode());
        builder.priority(message.getPriority(DEFAULT_PRIORITY));
        channel = connection.createChannel();
        if (exchangeName == null) {
            channel.basicPublish("", queueName, builder.build(), byteForm);
        } else {
            channel.basicPublish(exchangeName, queueName, builder.build(), byteForm);
        }
    } catch (IOException e) {
        throwable = e;
        error = true;
        isConnectionError = true;
    } catch (Throwable t) {
        throwable = t;
        error = true;
    } finally {
        if (channel != null && channel.isOpen())
            try {
                channel.close();
            } catch (IOException e) {
                logger.error("Error when closing connection" + synCtx.getMessageID() + ". " + e);
            }
    }
    if (error) {
        String errorMsg = getId() + ". Ignored MessageID : " + synCtx.getMessageID()
                + ". Could not store message to store [" + store.getName() + "]. Error:"
                + throwable.getLocalizedMessage();
        logger.error(errorMsg, throwable);
        store.closeProducerConnection();
        connection = null;
        if (logger.isDebugEnabled()) {
            logger.debug(getId() + ". Ignored MessageID : " + synCtx.getMessageID());
        }
        return false;
    }
    if (logger.isDebugEnabled()) {
        logger.debug(getId() + ". Stored MessageID : " + synCtx.getMessageID());
    }
    store.enqueued();
    return true;
}

From source file:com.bah.applefox.main.plugins.fulltextindex.FTAccumuloSampler.java

/**
 * Overridden method to create the sample
 *//*from  w  w w  .jav  a2s.com*/
public void createSample() {
    try {
        // HashMap to write the sample table to
        HashMap<String, Integer> output = new HashMap<String, Integer>();

        // Scan the data table
        Scanner scan = AccumuloUtils.connectRead(dataTable);

        Map<String, String> properties = new HashMap<String, String>();
        IteratorSetting cfg2 = new IteratorSetting(11, SamplerCreator.class, properties);
        scan.addScanIterator(cfg2);

        for (Entry<Key, Value> entry : scan) {
            try {
                // Write the data from the table to the sample table
                String row = entry.getKey().getRow().toString();
                // get rid of the timestamp at the end
                row = row.substring(0, row.lastIndexOf(" "));
                int value = output.containsKey(row) ? output.get(row) : 0;
                value += (Integer) IngestUtils.deserialize(entry.getValue().get());
                output.put(row, value);
            } catch (Exception e) {
                if (e.getMessage() != null) {
                    log.error(e.getMessage());
                } else {
                    log.error(e.getStackTrace());
                }
            }
        }

        // get the total number of docs from the urls table
        Scanner scann = AccumuloUtils.connectRead(urlTable);

        IteratorSetting cfg3 = new IteratorSetting(11, TotalDocFinder.class, properties);
        scann.addScanIterator(cfg3);

        for (Entry<Key, Value> entry : scann) {
            try {
                output.put(entry.getKey().getRow().toString(),
                        (Integer) IngestUtils.deserialize(entry.getValue().get()));
            } catch (Exception e) {
                if (e.getMessage() != null) {
                    log.error(e.getMessage());
                } else {
                    log.error(e.getStackTrace());
                }
            }
        }

        // Create the sample table file
        File f = new File(sampleFile);
        f.createNewFile();

        // use buffering
        OutputStream file = new FileOutputStream(f);
        OutputStream buffer = new BufferedOutputStream(file);
        ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(output);
        out.flush();
        out.close();
    } catch (AccumuloException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }
    } catch (AccumuloSecurityException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }
    } catch (TableNotFoundException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }
    } catch (IOException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }
    }
}

From source file:com.bah.applefox.main.plugins.imageindex.ImageAccumuloSampler.java

/**
 * Overridden method to create the sample
 *///from  w w  w.  j a  v  a  2s . c  o m
public void createSample() {
    try {
        // HashMap to write the sample table to
        HashMap<String, Integer> output = new HashMap<String, Integer>();

        // Scan the data table
        Scanner scan = AccumuloUtils.connectRead(dataTable);

        Map<String, String> properties = new HashMap<String, String>();
        IteratorSetting cfg2 = new IteratorSetting(11, SamplerCreator.class, properties);
        scan.addScanIterator(cfg2);

        for (Entry<Key, Value> entry : scan) {
            try {
                // Write the data from the table to the sample table
                String row = entry.getKey().getRow().toString();
                int value = output.containsKey(row) ? output.get(row) : 0;
                value += 1;
                output.put(row, value);
            } catch (Exception e) {
                if (e.getMessage() != null) {
                    log.error(e.getMessage());
                } else {
                    log.error(e.getStackTrace());
                }
            }
        }

        // get the total number of docs from the urls table
        Scanner scann = AccumuloUtils.connectRead(urlTable);

        IteratorSetting cfg3 = new IteratorSetting(11, TotalDocFinder.class, properties);
        scann.addScanIterator(cfg3);

        for (Entry<Key, Value> entry : scann) {
            try {
                output.put(entry.getKey().getRow().toString(),
                        (Integer) IngestUtils.deserialize(entry.getValue().get()));
            } catch (Exception e) {
                if (e.getMessage() != null) {
                    log.error(e.getMessage());
                } else {
                    log.error(e.getStackTrace());
                }
            }
        }

        // Create the sample table file
        System.out.println(output.size());
        System.out.println("sample file: " + sampleFile);
        File f = new File(sampleFile);
        f.createNewFile();

        // use buffering
        OutputStream file = new FileOutputStream(f);
        OutputStream buffer = new BufferedOutputStream(file);
        ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(output);
        out.flush();
        out.close();
    } catch (AccumuloException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }
    } catch (AccumuloSecurityException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }
    } catch (TableNotFoundException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }
    } catch (IOException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }
    }
}

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

/**
 * Serialize an instance, restore it, and check for equality.
 *///from   ww w  .  ja  v a  2s . co m
public void testSerialization() {

    CategoryTableXYDataset d1 = new CategoryTableXYDataset();
    d1.add(1.0, 1.1, "Series 1");
    d1.add(2.0, 2.2, "Series 1");

    CategoryTableXYDataset 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 = (CategoryTableXYDataset) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertEquals(d1, d2);

}

From source file:org.jfree.data.category.junit.CategoryToPieDatasetTest.java

/**
 * Serialize an instance, restore it, and check for equality.
 *///from  w  w  w  .ja v  a  2 s .  c  o m
public void testSerialization() {
    DefaultCategoryDataset underlying = new DefaultCategoryDataset();
    underlying.addValue(1.1, "R1", "C1");
    underlying.addValue(2.2, "R1", "C2");
    CategoryToPieDataset d1 = new CategoryToPieDataset(underlying, TableOrder.BY_COLUMN, 1);
    CategoryToPieDataset 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 = (CategoryToPieDataset) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertEquals(d1, d2);

    // regular equality for the datasets doesn't check the fields, just
    // the data values...so let's check some more things...
    assertEquals(d1.getUnderlyingDataset(), d2.getUnderlyingDataset());
    assertEquals(d1.getExtractType(), d2.getExtractType());
    assertEquals(d1.getExtractIndex(), d2.getExtractIndex());
}

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

/**
 * Serialize an instance, restore it, and check for equality.
 *///www  .ja v a  2  s.co  m
public void testSerialization() {

    DefaultTableXYDataset d1 = new DefaultTableXYDataset();
    XYSeries s1 = new XYSeries("Series 1", true, false);
    s1.add(1.0, 1.1);
    s1.add(2.0, 2.2);
    d1.addSeries(s1);

    DefaultTableXYDataset 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 = (DefaultTableXYDataset) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertEquals(d1, d2);

}

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

/**
 * Serialize an instance, restore it, and check for equality.
 *//*from ww  w .ja va2 s  .  co  m*/
public void testSerialization() {
    DefaultWindDataset d1 = new DefaultWindDataset();
    DefaultWindDataset 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 = (DefaultWindDataset) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertEquals(d1, d2);

    // try a dataset with some content...
    d1 = createSampleDataset1();
    try {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(d1);
        out.close();

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

}