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.db4o.sync4o.SyncKey.java

static public String toEncodedString(SyncKey key) throws Exception {

    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(bs);
    os.writeObject(key);//from  w ww . j  av  a 2  s  .c  o m
    os.flush();
    os.close();

    return new String(Base64.encodeBase64(bs.toByteArray()));

}

From source file:bancvirt.Recurso.java

public Boolean commit(Long tId) {
    FileOutputStream fout = null;
    try {/*from w w w  .  j  av  a 2s.  c o  m*/
        fout = new FileOutputStream(getResourceId());
        ObjectOutputStream oos = new ObjectOutputStream(fout);
        oos.writeObject(this);
        oos.close();
        fout.close();
        return true;
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
        return false;
    } catch (IOException ex) {
        ex.printStackTrace();
        return false;
    } finally {
        try {
            fout.close();
        } catch (IOException ex) {
            ex.printStackTrace();
            return false;
        }
    }
}

From source file:jp.xet.baseunits.tests.SerializationTester.java

/**
 * ????????//  w  ww.  j a v  a2 s .  c  o  m
 * 
 * @param serializable 
 * @throws AssertionError ????
 */
public static void assertCanBeSerialized(Object serializable) {
    if (Serializable.class.isInstance(serializable) == false) {
        fail("Object doesn't implement java.io.Serializable interface: " + serializable.getClass());
    }

    ObjectOutputStream out = null;
    ObjectInputStream in = null;
    ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
    ByteArrayInputStream byteArrayIn = null;
    try {
        out = new ObjectOutputStream(byteArrayOut);
        out.writeObject(serializable);

        byteArrayIn = new ByteArrayInputStream(byteArrayOut.toByteArray());
        in = new ObjectInputStream(byteArrayIn);
        Object deserialized = in.readObject();
        if (serializable.equals(deserialized) == false) {
            fail("Reconstituted object is expected to be equal to serialized");
        }
    } catch (IOException e) {
        fail(e.getMessage());
    } catch (ClassNotFoundException e) {
        fail(e.getMessage());
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
    }
}

From source file:com.rr.familyPlanning.ui.security.encryptObject.java

/**
 * Encrypts and encodes the Object and IV for url inclusion
 *
 * @param input/*from ww  w.ja va  2s  . c o m*/
 * @return
 * @throws Exception
 */
public String[] encryptObject(Object obj) throws Exception {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    ObjectOutput out = new ObjectOutputStream(stream);
    try {
        // Serialize the object
        out.writeObject(obj);
        byte[] serialized = stream.toByteArray();
        // Setup the cipher and Init Vector
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] iv = new byte[cipher.getBlockSize()];
        new SecureRandom().nextBytes(iv);
        IvParameterSpec ivSpec = new IvParameterSpec(iv);
        // Hash the key with SHA-256 and trim the output to 128-bit for the key
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        digest.update(keyString.getBytes());
        byte[] key = new byte[16];
        System.arraycopy(digest.digest(), 0, key, 0, key.length);
        SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
        // encrypt
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
        // Encrypt & Encode the input
        byte[] encrypted = cipher.doFinal(serialized);
        byte[] base64Encoded = Base64.encodeBase64(encrypted);
        String base64String = new String(base64Encoded);
        String urlEncodedData = URLEncoder.encode(base64String, "UTF-8");
        // Encode the Init Vector
        byte[] base64IV = Base64.encodeBase64(iv);
        String base64IVString = new String(base64IV);
        String urlEncodedIV = URLEncoder.encode(base64IVString, "UTF-8");

        return new String[] { urlEncodedData, urlEncodedIV };
    } finally {
        stream.close();
        out.close();
    }
}

From source file:com.pcms.common.Common.java

public <T extends Serializable> T clone(T obj) {
    T clonedObj = null;//from ww w .  ja v a  2s .  co m
    try {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(stream);

        out.writeObject(obj);
        out.close();
        ByteArrayInputStream bais = new ByteArrayInputStream(stream.toByteArray());
        ObjectInputStream os = new ObjectInputStream(bais);
        clonedObj = (T) os.readObject();
        stream.close();
    } catch (IOException e) {
        _log.error(e.getMessage());
    } catch (ClassNotFoundException e) {
        _log.error(e.getMessage());
    }
    return clonedObj;
}

From source file:conceptnet.ConceptBuilder.java

public static void saveConcept(Concept c) {
    String path = conceptPath + c.name;
    File f = new File(path);
    if (f.exists() || f.isDirectory()) {
        System.out.println("Try to save a concept that is already saved: " + c.name);
        return;//  w  ww  .ja v a2  s .c  o m
    }
    try {
        ObjectOutputStream ois = new ObjectOutputStream(new FileOutputStream(path));
        ois.writeObject(c);
        ois.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:aot.util.IOUtil.java

public static byte[] serialize(Object object) {
    try (ByteArrayOutputStream buffer = new ByteArrayOutputStream(4096)) {
        try (ObjectOutputStream output = new ObjectOutputStream(buffer)) {
            output.writeObject(object);//w  ww.jav  a  2 s .co m
        }
        return buffer.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:io.cloudslang.content.httpclient.build.CookieStoreBuilder.java

public static byte[] serialize(Object obj) throws IOException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    ObjectOutputStream o = new ObjectOutputStream(b);
    o.writeObject(obj);/*from w  w w  . j  a va  2s .com*/
    return b.toByteArray();
}

From source file:com.yahoo.bullet.record.AvroBulletRecordTest.java

public static byte[] getRecordBytes(AvroBulletRecord record) {
    try {//from   w w  w. j  av a  2s.c om
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        ObjectOutputStream outputStream = new ObjectOutputStream(byteStream);
        outputStream.writeObject(record);
        outputStream.close();
        return byteStream.toByteArray();
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);

    }
}

From source file:foam.dao.index.PersistedIndex.java

@Override
public Object wrap(Object state) {
    synchronized (file_) {
        try {/*from  w  ww  .j  av  a2s  .  c  o  m*/
            long position = fos_.getChannel().position();
            ObjectOutputStream oos = new ObjectOutputStream(bos_);
            oos.writeObject(state);
            oos.flush();

            bos_.writeTo(fos_);
            bos_.flush();
            bos_.reset();

            return position;
        } catch (Throwable t) {
            throw new RuntimeException(t);
        }
    }
}