Example usage for java.io ObjectOutput flush

List of usage examples for java.io ObjectOutput flush

Introduction

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

Prototype

public void flush() throws IOException;

Source Link

Document

Flushes the stream.

Usage

From source file:Main.java

public static void saveObject(Context context, String fileName, Object obj) throws IOException {
    FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
    ObjectOutput out = null;
    try {//from  w  ww  .  j  a  v a  2 s. c  o  m
        out = new ObjectOutputStream(fos);
        out.writeObject(obj);
        out.flush();
    } finally {
        out.close();
        fos.close();
    }

}

From source file:com.bah.applefox.main.plugins.pageranking.utilities.PRtoFile.java

public static boolean writeToFile(String[] args) {
    String fileName = args[16];// ww w .  j  av  a 2s  . c o m
    File f = new File(fileName);
    try {
        f.createNewFile();
        OutputStream file = new FileOutputStream(f);
        OutputStream buffer = new BufferedOutputStream(file);
        ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(createMap(args));
        out.flush();
        out.close();
    } catch (IOException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }
        return false;
    } catch (AccumuloException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }
        return false;
    } catch (AccumuloSecurityException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }
        return false;
    } catch (TableNotFoundException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }
        return false;
    }

    return true;

}

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

public static byte[] serialize(Serializable object) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    byte[] result = null;
    try {//w w w .  ja v  a 2s . com
        out = new ObjectOutputStream(bos);
        out.writeObject(object);
        out.flush();
        result = bos.toByteArray().clone();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException ignore) {
        }
        try {
            bos.close();
        } catch (IOException ignore) {
        }
    }
    return result;
}

From source file:MemComboBoxDemo.java

public void save(String fName) {
    try {//from  w  ww . j a v a2 s  . c  o  m
        FileOutputStream fStream = new FileOutputStream(fName);
        ObjectOutput stream = new ObjectOutputStream(fStream);

        stream.writeObject(getModel());

        stream.flush();
        stream.close();
        fStream.close();
    } catch (Exception e) {
        System.err.println("Serialization error: " + e.toString());
    }
}

From source file:com.mobicage.rogerthat.mfr.dal.Streamable.java

public String toBase64() {
    try {//  w  w w  .ja  v a2 s  .  c om
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ZipOutputStream zip = new ZipOutputStream(bos);
            try {
                zip.setLevel(9);
                zip.putNextEntry(new ZipEntry("entry"));
                try {
                    ObjectOutput out = new ObjectOutputStream(zip);
                    try {
                        out.writeObject(this);
                    } finally {
                        out.flush();
                    }
                } finally {
                    zip.closeEntry();
                }
                zip.flush();
                return Base64.encodeBase64URLSafeString(bos.toByteArray());
            } finally {
                zip.close();
            }
        } finally {
            bos.close();
        }
    } catch (IOException e) {
        log.log((Level.SEVERE), "IO Error while serializing member", e);
        return null;
    }
}

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. ja  v a2  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:com.bah.applefox.main.plugins.fulltextindex.FTAccumuloSampler.java

/**
 * Overridden method to create the sample
 *///  ww w  .  ja v a  2s . c om
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
 *//*w ww .j  a  v  a2s.  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:com.almende.eve.state.file.ConcurrentSerializableFileState.java

/**
 * write properties to disk./*from   ww  w  . j ava 2s . com*/
 * 
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
private void write() throws IOException {
    if (channel != null) {
        channel.position(0);
    }
    final ObjectOutput out = new ObjectOutputStream(fos);
    out.writeObject(properties);
    out.flush();

    if (channel != null) {
        channel.truncate(channel.position());
    }
}

From source file:info.magnolia.cms.core.version.VersionManager.java

/**
 * create version of the specified node and all child nodes based on the given <code>Rule</code>
 *
 * @param node to be versioned//  w  w w .j a  va2  s.  co  m
 * @param rule
 * @return newly created version node
 * @throws UnsupportedOperationException if repository implementation does not support Versions API
 * @throws javax.jcr.RepositoryException if any repository error occurs
 */
private Version createVersion(Content node, Rule rule)
        throws UnsupportedRepositoryOperationException, RepositoryException {
    if (VersionConfig.getMaxVersionIndex() < 1) {
        log.debug("Ignore create version, MaxVersionIndex < 1");
        log.debug("Returning root version of the source node");
        return node.getJCRNode().getVersionHistory().getRootVersion();
    }
    CopyUtil.getInstance().copyToversion(node, new RuleBasedContentFilter(rule));
    Content versionedNode = this.getVersionedNode(node);
    Content systemInfo = this.getSystemNode(versionedNode);
    // add serialized rule which was used to create this version
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        ObjectOutput objectOut = new ObjectOutputStream(out);
        objectOut.writeObject(rule);
        objectOut.flush();
        objectOut.close();
        NodeData nodeData;
        // PROPERTY_RULE is not a part of MetaData to allow versioning of node types which does NOT support MetaData
        if (!systemInfo.hasNodeData(PROPERTY_RULE))
            nodeData = systemInfo.createNodeData(PROPERTY_RULE);
        else
            nodeData = systemInfo.getNodeData(PROPERTY_RULE);
        nodeData.setValue(out.toString());
    } catch (IOException e) {
        throw new RepositoryException("Unable to add serialized Rule to the versioned content");
    }
    // temp fix, MgnlContext should always have user either logged-in or anonymous
    String userName = "";
    if (MgnlContext.getUser() != null)
        userName = MgnlContext.getUser().getName();
    // add all system properties for this version
    if (!systemInfo.hasNodeData(ContentVersion.VERSION_USER))
        systemInfo.createNodeData(ContentVersion.VERSION_USER).setValue(userName);
    else
        systemInfo.getNodeData(ContentVersion.VERSION_USER).setValue(userName);
    if (!systemInfo.hasNodeData(ContentVersion.NAME))
        systemInfo.createNodeData(ContentVersion.NAME).setValue(node.getName());
    else
        systemInfo.getNodeData(ContentVersion.NAME).setValue(node.getName());

    versionedNode.save();
    // add version
    Version newVersion = versionedNode.getJCRNode().checkin();
    versionedNode.getJCRNode().checkout();
    try {
        this.setMaxVersionHistory(versionedNode);
    } catch (RepositoryException re) {
        log.error("Failed to limit version history to the maximum configured", re);
        log.error("New version has already been created");
    }

    return newVersion;
}