Example usage for java.io ObjectOutputStream ObjectOutputStream

List of usage examples for java.io ObjectOutputStream ObjectOutputStream

Introduction

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

Prototype

public ObjectOutputStream(OutputStream out) throws IOException 

Source Link

Document

Creates an ObjectOutputStream that writes to the specified OutputStream.

Usage

From source file:com.impetus.kundera.property.accessor.ObjectAccessor.java

@Override
public final byte[] toBytes(Object o) {

    try {/*from   w  w w.  ja  va 2 s . c o  m*/
        if (o != null) {
            if (o instanceof byte[]) {
                return (byte[]) o;
            }

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(o);
            oos.close();
            return baos.toByteArray();
        }
    } catch (IOException e) {
        throw new PropertyAccessException(e);
    }
    return null;
}

From source file:SaveYourDrawingToFile.java

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == clearBtn) {
        displayList = new Vector();
        repaint();/*w w w  .j a va  2s.co m*/
    } else if (e.getSource() == saveBtn) {
        try {
            FileOutputStream fos = new FileOutputStream(pathname);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(displayList);
            oos.flush();
            oos.close();
            fos.close();
        } catch (Exception ex) {
            System.out.println("Trouble writing display list vector");
        }
    } else if (e.getSource() == restoreBtn) {
        try {
            FileInputStream fis = new FileInputStream(pathname);
            ObjectInputStream ois = new ObjectInputStream(fis);
            displayList = (Vector) (ois.readObject());
            ois.close();
            fis.close();
            repaint();
        } catch (Exception ex) {
            System.out.println("Trouble reading display list vector");
        }
    } else if (e.getSource() == quitBtn) {
        setVisible(false);
        dispose();
        System.exit(0);
    }
}

From source file:FileSerializeCollection.java

public void clear() {
    size = 0;//from   w  w w . ja va2 s .  c  o m
    try {
        if (oos != null) {
            oos.close();
        }
        oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
    } catch (final IOException io) {
        System.err.println(io);
    }
}

From source file:com.qwazr.server.InFileSessionPersistenceManager.java

private void writeSession(final Path deploymentDir, final String sessionId,
        final PersistentSession persistentSession) {
    final Date expDate = persistentSession.getExpiration();
    if (expDate == null)
        return; // No expiry date? no serialization
    final Map<String, Object> sessionData = persistentSession.getSessionData();
    if (sessionData == null)
        return; // No sessionData? no serialization
    final File sessionFile = deploymentDir.resolve(sessionId).toFile();
    try (final ObjectOutputStream draftOutputStream = new ObjectOutputStream(new NullOutputStream())) {
        try (final ObjectOutputStream sessionOutputStream = new ObjectOutputStream(
                new FileOutputStream(sessionFile))) {
            sessionOutputStream.writeLong(expDate.getTime()); // The date is stored as long
            sessionData.forEach((attribute, object) -> writeSessionAttribute(draftOutputStream,
                    sessionOutputStream, attribute, object));
        }/*from w  ww.  j a va  2s.c  o  m*/
    } catch (IOException | CancellationException e) {
        LOGGER.log(Level.SEVERE, e, () -> "Cannot save sessions in " + sessionFile);
    }
}

From source file:com.mvdb.etl.dao.impl.JdbcGenericDAO.java

private boolean writeDataHeader(DataHeader dataHeader, String objectName, File snapshotDirectory) {
    try {//from w w  w . ja v  a2  s .  c om
        snapshotDirectory.mkdirs();
        String headerFileName = "header-" + objectName + ".dat";
        File headerFile = new File(snapshotDirectory, headerFileName);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(dataHeader);
        FileUtils.writeByteArrayToFile(headerFile, baos.toByteArray());
        return true;
    } catch (Throwable t) {
        t.printStackTrace();
        return false;
    }

}

From source file:de.tudarmstadt.ukp.dkpro.wsd.si.dictionary.UkbDictionaryInventory.java

public UkbDictionaryInventory(String inputPath, String serializiblePath, String neededMentionsPath)
        throws FileNotFoundException, IOException {
    // Open the serialized version or create it from scratch
    try {/*from  w w  w.j av  a2 s .  c  o m*/
        // System.out.println("Trying to load dictionary from serializable.");
        ObjectInputStream dictionaryReader = new ObjectInputStream(
                new BZip2CompressorInputStream(new FileInputStream(serializiblePath)));
        dictionary = (UkbDictionary) dictionaryReader.readObject();
        dictionaryReader.close();
        // System.out.println("Loaded dictionary from serializable.");
    } catch (Exception e) {
        // System.out.println("Trying to load dictionary from input.");
        dictionary = new UkbDictionary(inputPath, neededMentionsPath);
        System.out.println("Loaded dictionary from input.");

        ObjectOutputStream dictionaryWriter = new ObjectOutputStream(
                new BZip2CompressorOutputStream(new FileOutputStream(serializiblePath)));
        dictionaryWriter.writeObject(dictionary);
        dictionaryWriter.close();
        // System.out.println("Stored dictionary in serializable.");
    }

}

From source file:FileUserAccess.java

@SuppressWarnings("resource")
@Override/* ww  w. jav a  2 s  . c  o  m*/
public boolean saveNewUser(User newUser) {
    FileOutputStream fout;
    try {
        fout = new FileOutputStream(
                System.getProperty("user.dir") + File.separator + newUser.getUsername() + ".userobj", false);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(FileUserAccess.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    }
    ObjectOutputStream oos;
    try {
        oos = new ObjectOutputStream(fout);
    } catch (IOException ex) {
        Logger.getLogger(FileUserAccess.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    }

    try {
        oos.writeObject(newUser);
        oos.close();
    } catch (IOException ex) {
        Logger.getLogger(FileUserAccess.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    }
    return true;
}

From source file:de.scoopgmbh.copper.test.versioning.compatibility.TestJavaSerializer.java

private String serialize(final Object o) throws IOException {
    if (o == null)
        return null;
    final ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    final ObjectOutputStream oos = new ObjectOutputStream(baos);

    oos.writeObject(o);/*  www .  j  a v  a 2 s .c om*/
    oos.close();
    baos.close();
    byte[] data = baos.toByteArray();
    final ByteArrayOutputStream baos2 = new ByteArrayOutputStream(1024);
    baos2.write(data, 0, 8);
    baos2.write(classNameReplacement.getBytes("UTF-8"));
    int offset = classNameReplacement.length() + 8;
    baos2.write(data, offset, data.length - offset);
    baos2.flush();
    baos2.close();
    data = baos2.toByteArray();
    boolean isCompressed = false;
    if (compress && compressThresholdSize <= data.length && data.length <= compressorMaxSize) {
        data = compressorTL.get().compress(data);
        isCompressed = true;
    }
    final String encoded = new Base64().encodeToString(data);
    final StringBuilder sb = new StringBuilder(encoded.length() + 4);
    sb.append(isCompressed ? 'C' : 'U').append(encoded);
    return sb.toString();
}

From source file:edu.jhu.hlt.parma.inference.transducers.StringEditModelTrainer.java

private void writeModel(StringEditModel transducer, String filename) {
    try {/*from   www.ja  va2 s  .  c  o m*/
        FileOutputStream out = new FileOutputStream(filename);
        ObjectOutputStream objOut = new ObjectOutputStream(out);
        objOut.writeObject(transducer);
        objOut.close();
    } catch (IOException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    }
}

From source file:de.bps.webservices.clients.onyxreporter.HashMapWrapper.java

/**
 * Serializes the given object o and encodes the result as non-chunked base64.
 * /*  w ww.ja v  a 2 s .c o m*/
 * @param objectToSerializeAndEncode
 * @return
 * @throws OnyxReporterException
 */
private static final String serialize(final Object objectToSerializeAndEncode) throws OnyxReporterException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(baos);
        oos.writeObject(objectToSerializeAndEncode);
        oos.close();
        oos = null;
        return Base64.encodeBase64String(baos.toByteArray());
    } catch (final IOException e) {
        throw new OnyxReporterException("Could not serialize object!", e);
    } finally {
        try {
            if (oos != null) {
                oos.close();
            }
        } catch (final IOException e) {
        }
    }
}