Example usage for java.io ObjectOutput close

List of usage examples for java.io ObjectOutput close

Introduction

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

Prototype

public void close() throws IOException;

Source Link

Document

Closes the stream.

Usage

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

public void serialize(String toFile) {
    try {/*  ww w.j a v a 2  s .c o  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:wvec.WordVec.java

public byte[] getBytes() throws IOException {
    byte[] byteArray;
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
        ObjectOutput out;
        out = new ObjectOutputStream(bos);
        out.writeObject(this);
        byteArray = bos.toByteArray();//  w  ww  .ja  v  a 2  s  .com
        out.close();
    }
    return byteArray;
}

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

/**
 * Serialize an instance, restore it, and check for equality.
 */// www .  j  av  a 2s . c  o  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  ww.  j a 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.
 *//* ww w .  j  a v a  2  s .  c o 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.
 *//*  w  w  w  . ja v a 2  s  .  com*/
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);

}

From source file:com.edsoft.teknosaproject.bean.TreeNodeBean.java

public void saveFile() {
    Path path = Paths.get("E:", "TREE", "hiyerari.txt");
    //Path path = Paths.get("E:", "Hiyerari", "hiyerari.txt");
    OutputStream stream;/* w w  w.  java  2 s . c  o m*/
    ObjectOutput object;
    try {
        stream = new FileOutputStream(path.toFile());
        object = new ObjectOutputStream(stream);
        object.writeObject(root);
        object.flush();
        object.close();
        stream.close();
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_INFO, "TAMMA", "Baarl"));
    } catch (IOException ex) {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_ERROR, "HATA", "Kayt srasnda hata oldu"));
    }
}

From source file:org.rhq.enterprise.server.plugins.alertMicroblog.MicroblogServerPluginComponent.java

private String storeAccessToken(AccessToken token) throws IOException {
    //use buffering
    String filePath = null;//from www  .  j a v  a 2s .  com
    if (this.context.getDataDirectory().exists() || this.context.getDataDirectory().mkdir()) {

        filePath = this.context.getDataDirectory().getAbsolutePath() + "/OAuthAccessToken_" + token.getUserId()
                + ".ser";

        // merge the PLugin Configuration to store the token file path reference.
        // this property will be user by Microblog AlertSender to load the accessToken from file system
        this.context.getPluginConfiguration().put(new PropertySimple("accessTokenFilePath", filePath));
        this.persistConfiguration(this.context.getPluginConfiguration());

        OutputStream file = new FileOutputStream(filePath);
        OutputStream buffer = new BufferedOutputStream(file);
        ObjectOutput output = new ObjectOutputStream(buffer);

        try {
            output.writeObject(token);
        } finally {
            output.close();

            log.info("AccessToken saved at " + filePath);
        }
    } else
        throw new IOException("AccessToken not stored!");

    return filePath;
}

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

/**
 * Overridden method to create the sample
 *//*from   w ww  . ja v a  2 s  . 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();
                // 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  av a  2s. co  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());
        }
    }
}