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:Main.java

public static byte[] getBytes(Serializable obj) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bout);
    out.writeObject(obj);/*from  ww w.  jav  a2s .  c o m*/
    out.flush();
    byte[] bytes = bout.toByteArray();
    bout.close();
    out.close();
    return bytes;
}

From source file:com.haulmont.cuba.core.sys.serialization.StandardSerialization.java

@Override
public void serialize(Object object, OutputStream os) {
    ObjectOutputStream out = null;
    boolean isObjectStream = os instanceof ObjectOutputStream;
    try {/*from   w ww .  ja v  a2s.  c  om*/
        out = isObjectStream ? (ObjectOutputStream) os : new ObjectOutputStream(os);
        out.writeObject(object);
    } catch (IOException ex) {
        throw new IllegalStateException("Failed to serialize object", ex);
    } finally {
        //Prevent close stream. Stream closed only by:
        //com.haulmont.cuba.core.sys.remoting.HttpServiceExporter,
        //com.haulmont.cuba.core.sys.remoting.ClusteredHttpInvokerRequestExecutor()
        //Only flush buffer to output stream
        if (!isObjectStream && out != null) {
            try {
                out.flush();
            } catch (IOException ex) {
                throw new IllegalStateException("Failed to serialize object", ex);
            }
        }
    }
}

From source file:de.tudarmstadt.ukp.dkpro.core.frequency.tfidf.util.TfidfUtils.java

public static void serialize(Object object, String fileName) throws Exception {
    File file = new File(fileName);
    if (!file.exists())
        FileUtils.touch(file);//from  ww  w.  j  a v a  2 s  . co m
    if (file.isDirectory()) {
        throw new IOException("A directory with that name exists!");
    }
    ObjectOutputStream objOut;
    objOut = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
    objOut.writeObject(object);
    objOut.flush();
    objOut.close();

}

From source file:cs.register.cddao.java

@Override
public void salvaresair(int status) {
    ObjectOutputStream objectOut = null;
    try {/*w w  w  .  j a  v  a2 s  .com*/
        objectOut = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("save")));
        objectOut.writeObject(this.list1);

        objectOut.close();
    } catch (IOException ex) {
        Logger.getLogger(cddao.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            objectOut.close();
        } catch (IOException ex) {
            Logger.getLogger(cddao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    if (status == 0)
        System.exit(0);
}

From source file:com.code.savemarks.utils.Utils.java

/**
 * Serialize an object into a byte array.
 * // www.j  ava  2 s. c om
 * @param obj An object to be serialized.
 * @return A byte array containing the serialized object
 * @throws QueueFailureException If an I/O error occurs during the
 * serialization process.
 */
public static byte[] serialize(Object obj) {
    try {
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        ObjectOutputStream objectOut = new ObjectOutputStream(new BufferedOutputStream(bytesOut));
        objectOut.writeObject(obj);
        objectOut.close();
        return encodeBase64(bytesOut.toByteArray());
    } catch (IOException e) {
        throw new QueueFailureException(e);
    }
}

From source file:cs.register.abrirefeichar.java

@Override
public void salvaresair(int status) {
    ObjectOutputStream objectOut = null;
    try {// www .  j a  v a2  s  . c o m
        objectOut = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("save")));
        objectOut.writeObject(this.list1);

        objectOut.close();
    } catch (IOException ex) {
        Logger.getLogger(abrirefeichar.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            objectOut.close();
        } catch (IOException ex) {
            Logger.getLogger(abrirefeichar.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    if (status == 0)
        System.exit(0);
}

From source file:javaslang.jackson.datatype.serialize.SerializableSerializer.java

@Override
Object toJavaObj(T value) throws IOException {
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    ObjectOutputStream stream = new ObjectOutputStream(buf);
    stream.writeObject(value);/*w  w w.  jav  a 2 s.c o  m*/
    return buf.toByteArray();
}

From source file:jedi.util.serialization.Pickle.java

/**
 * Serializes a object into a file.//from  ww w  .  j a va  2  s. c o m
 * 
 * @param o Object to be serialized.
 * @param f File where the serialization will occurs.
 * @param append defines if the write will occur in append mode.
 */
public static void dump(Object o, File f, boolean append) {

    if (o != null && f != null) {

        try {
            // File where the data will be written.
            FileOutputStream fos = new FileOutputStream(f, append);

            // Object that writes (writer) the data on the file.
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.flush();
            oos.writeObject(o); // write the data.
            oos.close();

            fos.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.earldouglas.xjdl.io.LicenseCreator.java

public String encryptLicense(License license, String key)
        throws Exception, NoSuchAlgorithmException, NoSuchPaddingException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
    objectOutputStream.writeObject(license);
    objectOutputStream.close();// w  w  w .  ja v a2 s .  c om
    byte[] serializedLicense = byteArrayOutputStream.toByteArray();

    SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
    byte[] encryptedLicense = cipher.doFinal(serializedLicense);
    String encodedLicense = Base64.encodeBase64String(encryptedLicense);
    encodedLicense = encodedLicense.replaceAll("\n", "");
    encodedLicense = encodedLicense.replaceAll("\r", "");
    return encodedLicense;
}

From source file:io.gumga.application.GumgaTempFileService.java

/**
 * Criar um arquivo temporario/*from   w  ww.j  a  va 2s . com*/
 * @param gumgaFile
 * @return dados do arquivo
 */
public String create(GumgaFile gumgaFile) {
    String tempFileName = "uploadData" + (System.currentTimeMillis() * 1000 + new Random().nextInt(1000));

    try { //TODO Arrumar o tratamento da Exception
        File folder = new File(gumgaValues.getUploadTempDir());
        folder.mkdirs();
        FileOutputStream fos = new FileOutputStream(gumgaValues.getUploadTempDir() + "/" + tempFileName);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(gumgaFile);
        oos.close();
        fos.close();
        return tempFileName;
    } catch (Exception ex) {
        log.error("erro ao criar arquivo temporario " + tempFileName, ex);
    }
    return "error";
}