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.koda.common.lcm.Tool.java

public static String doObject(Serializable obj) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(obj);//from   w ww  .j  a  va2 s .c om
    return doStuff(baos.toByteArray());
}

From source file:com.talis.storage.s3.ExternalizableS3ObjectTest.java

@Test
public void roundTripS3ObjectSerialization() throws Exception {
    ByteArrayOutputStream tmp = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(tmp);

    byte[] bytes = ("When replying to any emails you have prior to migration "
            + "(ie any in your inbox before you have been migrated) "
            + "you will need to change the way you reply to them").getBytes();

    ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
    buffer.put(bytes);/*  w ww. ja va  2 s  . c o  m*/

    S3ObjectFactory factory = new S3ObjectFactory("bucket");
    S3Object original = factory.newObject("foo", 0, MediaType.TEXT_PLAIN_TYPE, buffer);

    out.writeObject(original);
    out.flush();
    out.close();

    byte[] serialized = tmp.toByteArray();

    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(serialized));
    S3Object clone = (S3Object) in.readObject();

    assertObjectsEqual(original, clone);
}

From source file:com.mtgi.io.RelocatableFileTest.java

@Before
public void setUp() throws IOException {
    source = File.createTempFile("RelocatableFileTest", ".tmp");
    inst = new RelocatableFile(source);

    buffer = new ByteArrayOutputStream();
    output = new ObjectOutputStream(buffer);
}

From source file:com.alta189.deskbin.util.CryptUtils.java

private static boolean writeKey(SecretKey key) {
    FileOutputStream fos = null;/*w  w w  . j  ava2  s  .  com*/
    ObjectOutputStream out = null;
    try {
        if (keyFile.getParentFile() != null) {
            keyFile.getParentFile().mkdirs();
        }
        fos = new FileOutputStream(keyFile);
        out = new ObjectOutputStream(fos);
        out.writeObject(key);
        out.flush();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(out);
    }
    return false;
}

From source file:br.com.ufjf.labredes.crypto.Cryptography.java

public static void geraChave() {
    try {//from   w  ww.  j a v  a2  s .c om

        final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM_ASYM);
        keyGen.initialize(1024);
        final KeyPair key = keyGen.generateKeyPair();

        File chavePrivadaFileServer = new File(path, PATH_CHAVE_PRIVADA_SERVER);
        File chavePublicaFileServer = new File(path, PATH_CHAVE_PUBLICA_SERVER);

        // Cria os arquivos para armazenar a chave Privada e a chave Publica            
        if (chavePrivadaFileServer.getParentFile() != null) {
            chavePrivadaFileServer.getParentFile().mkdirs();
        }

        chavePrivadaFileServer.createNewFile();

        if (chavePublicaFileServer.getParentFile() != null) {
            chavePublicaFileServer.getParentFile().mkdirs();
        }

        chavePublicaFileServer.createNewFile();

        // Salva a Chave Pblica do servidor no arquivo
        ObjectOutputStream chavePublicaOSS = new ObjectOutputStream(
                new FileOutputStream(chavePublicaFileServer));
        chavePublicaOSS.writeObject(key.getPublic());
        chavePublicaOSS.close();

        // Salva a Chave Privada do servidor no arquivo
        ObjectOutputStream chavePrivadaOSS = new ObjectOutputStream(
                new FileOutputStream(chavePrivadaFileServer));
        chavePrivadaOSS.writeObject(key.getPrivate());
        chavePrivadaOSS.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:eu.supersede.fe.security.redis.template.ObjectRedisSerializer.java

@Override
public byte[] serialize(Object obj) throws SerializationException {
    if (obj == null) {
        return null;
    }//from   ww  w . j a v  a2s  . co m

    try (ByteArrayOutputStream b = new ByteArrayOutputStream()) {
        try (ObjectOutputStream o = new ObjectOutputStream(b)) {
            o.writeObject(obj);
        }

        return b.toByteArray();
    } catch (Exception ex) {
        log.error(ex.getMessage());
    }

    return null;
}

From source file:ByteFIFOTest.java

private void makeSrcData() throws IOException {
    String[] list = { "The first string is right here", "The second string is a bit longer and also right here",
            "The third string", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "0123456789", "The last string in the list" };

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(list);//  www.j  a  va2  s  . com
    oos.flush();
    oos.close();

    srcData = baos.toByteArray();
}

From source file:com.oprisnik.semdroid.utils.FileUtils.java

public static void writeObjectToFile(Object object, File file) throws IOException {
    if (!file.exists()) {
        file.getParentFile().mkdirs();/* w w w .  j  a v a  2s .c  o m*/
    }
    ObjectOutputStream out = null;
    try {
        out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
        out.writeObject(object);
        out.flush();
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:com.ikon.util.Serializer.java

/**
 * @param obj/* ww w .ja  v a  2  s.  c  o m*/
 */
public static byte[] write(Object obj) throws IOException {
    ByteArrayOutputStream baos = null;
    ObjectOutputStream oos = null;

    try {
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        oos.flush();
        baos.flush();
        return baos.toByteArray();
    } finally {
        IOUtils.closeQuietly(oos);
        IOUtils.closeQuietly(baos);
    }
}

From source file:org.glom.web.server.Utils.java

static public Object deepCopy(final Object oldObj) {
    ObjectOutputStream oos = null;
    ObjectInputStream ois = null;

    try {/*from   w  ww.j  av a2  s. c  o  m*/
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        // serialize and pass the object
        oos.writeObject(oldObj);
        oos.flush();
        final ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
        ois = new ObjectInputStream(bin);

        // return the new object
        return ois.readObject();
    } catch (final Exception e) {
        System.out.println("Exception in deepCopy:" + e);
        return null;
    } finally {
        try {
            oos.close();
            ois.close();
        } catch (final IOException e) {
            System.out.println("Exception in deepCopy during finally: " + e);
            return null;
        }
    }
}