Example usage for java.io ObjectOutputStream writeObject

List of usage examples for java.io ObjectOutputStream writeObject

Introduction

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

Prototype

public final void writeObject(Object obj) throws IOException 

Source Link

Document

Write the specified object to the ObjectOutputStream.

Usage

From source file:backtype.storm.serialization.SerializableSerializer.java

@Override
public void write(Kryo kryo, Output output, Object object) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {// w w w.  j  a  v  a2 s  . c  om
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(object);
        oos.flush();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    byte[] ser = bos.toByteArray();
    output.writeInt(ser.length);
    output.writeBytes(ser);
}

From source file:com.asquareb.kaaval.MachineKey.java

/**
 * Method to encrypt a string. Accepts the string to be encrypted and the
 * name of the file to store the key vale which can be used for decryption
 *//*from ww  w .  j av a 2  s  . co m*/
public static String encrypt(String property, String app) throws IOException, KaavalException {

    InetAddress ip = null;
    String ipAddress = null;
    ObjectOutputStream os = null;
    NetworkInterface macAddress = null;
    byte[] macId = null;
    Cipher pbeCipher = null;
    Random rand = new Random();
    rand.nextBytes(salt);
    try {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(password));
        ip = InetAddress.getLocalHost();
        ipAddress = ip.getHostAddress();
        macAddress = NetworkInterface.getByInetAddress(ip);
        macId = macAddress.getHardwareAddress();
        MachineKey mKey = new MachineKey();
        mKey.api = ipAddress;
        mKey.macad = new String(macId);
        mKey.yek = key;
        mKey.tlas = salt;
        mKey.eti = rand.nextInt(1000);
        os = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(app)));
        os.writeObject(mKey);
        os.close();
        pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(salt, mKey.eti));
        return base64Encode(pbeCipher.doFinal(property.getBytes()));
    } catch (IOException e) {
        throw new KaavalException(1, "Error in key file during encryption", e);
    } catch (Exception e) {
        throw new KaavalException(2, "Errors during encryption", e);
    } finally {
        if (os != null)
            os.close();
    }
}

From source file:SerialCloneTest.java

public Object clone() {
    try {/*from  w ww  . j av  a2 s  . c o  m*/
        // save the object to a byte array
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bout);
        out.writeObject(this);
        out.close();

        // read a clone of the object from the byte array
        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
        ObjectInputStream in = new ObjectInputStream(bin);
        Object ret = in.readObject();
        in.close();

        return ret;
    } catch (Exception e) {
        return null;
    }
}

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

public <T extends Serializable> T clone(T obj) {
    T clonedObj = null;//from w  w  w  . j  a  va 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:net.sf.ehcache.amqp.AMQEventMessage.java

public byte[] toBytes() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*from ww  w.  ja va 2s. c  o  m*/
        ObjectOutputStream out = new ObjectOutputStream(baos);
        out.writeObject(this);
    } catch (IOException e) {
        throw new CacheException(e);
    }
    return baos.toByteArray();
}

From source file:com.jaspersoft.jasperserver.api.metadata.data.cache.JavaDataSnapshotSerializer.java

public void writeSnapshot(DataSnapshot snapshot, OutputStream out) throws IOException {
    if (log.isDebugEnabled()) {
        log.debug("serializing data snapshot of type " + snapshot.getClass().getName());
    }/*from   w ww . j a v a2 s  .  c  o  m*/

    ObjectOutputStream objectOut = new ObjectOutputStream(out);
    objectOut.writeObject(snapshot);
}

From source file:org.sample.readerwriter.MyWriter.java

@Override
public void writeTo(MyObject t, Class<?> type, Type type1, Annotation[] antns, MediaType mt,
        MultivaluedMap<String, Object> mm, OutputStream out) throws IOException, WebApplicationException {
    ObjectOutputStream oos = new ObjectOutputStream(out);
    oos.writeObject(t);
}

From source file:edu.usf.cutr.opentripplanner.android.util.JacksonConfig.java

/**
 * Write the given object to Android internal storage for this app
 *
 * @param object serializable object to be written to cache (ObjectReader,
 *               ObjectMapper, or XmlReader)
 * @return true if object was successfully written to cache, false if it was
 * not/*from  w w  w  .  j  a  v a2s  . c  om*/
 */
private synchronized static boolean writeToCache(Serializable object) {

    FileOutputStream fileStream = null;
    ObjectOutputStream objectStream = null;
    String fileName = "";
    boolean success = false;

    if (context != null) {
        try {
            if (object instanceof ObjectMapper) {
                fileName = OBJECT_MAPPER + CACHE_FILE_EXTENSION;
            }
            if (object instanceof ObjectReader) {
                fileName = OBJECT_READER + CACHE_FILE_EXTENSION;
            }

            cacheWriteStartTime = System.nanoTime();
            fileStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);
            objectStream = new ObjectOutputStream(fileStream);
            objectStream.writeObject(object);
            objectStream.flush();
            fileStream.getFD().sync();
            cacheWriteEndTime = System.nanoTime();
            success = true;

            // Get size of serialized object
            long fileSize = context.getFileStreamPath(fileName).length();

            Log.d("TAG", "Wrote " + fileName + " to cache (" + fileSize + " bytes) in "
                    + df.format(getLastCacheWriteTime()) + " ms.");
        } catch (IOException e) {
            // Reset timestamps to show there was an error
            cacheWriteStartTime = 0;
            cacheWriteEndTime = 0;
            Log.e(TAG, "Couldn't write Jackson object '" + fileName + "' to cache: " + e);
        } finally {
            try {
                if (objectStream != null) {
                    objectStream.close();
                }
                if (fileStream != null) {
                    fileStream.close();
                }
            } catch (Exception e) {
                Log.e(TAG, "Error closing file connections: " + e);
            }
        }
    } else {
        Log.w(TAG,
                "Can't write to cache - no context provided.  If you want to use the cache, call JacksonConfig.setUsingCache(true, context) with a reference to your context.");
    }

    return success;
}

From source file:com.github.tell.codec.Base64Serializer.java

public byte[] encode(final Serializable o) throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final Base64OutputStream b64os = new Base64OutputStream(baos, true);
    final ObjectOutputStream oos = new ObjectOutputStream(b64os);
    oos.writeObject(o);
    oos.close();/*from  w  ww.  j  a v  a 2 s  .com*/
    return baos.toByteArray();
}

From source file:com.github.tell.codec.Base64Serializer.java

public String encodeToString(final Serializable o) throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(o);
    oos.close();//from  ww  w .  j a  va2s.c o  m
    final byte[] encoded = baos.toByteArray();
    final Base64 encoder = new Base64();
    return encoder.encodeToString(encoded);
}