Example usage for java.io ObjectOutputStream flush

List of usage examples for java.io ObjectOutputStream flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the stream.

Usage

From source file:grails.plugin.cache.web.filter.redis.GrailsSerializer.java

public void serialize(Object object, OutputStream outputStream) throws IOException {
    if (!(object instanceof Serializable)) {
        throw new IllegalArgumentException(
                getClass().getSimpleName() + " requires a Serializable payload but received an object of type ["
                        + object.getClass().getName() + "]");
    }/*w  w  w .j a  v a2  s.  c o  m*/

    ObjectOutputStream oos = new ObjectOutputStream(outputStream);
    oos.writeObject(object);
    oos.flush();
}

From source file:de.dfki.asr.compass.model.AbstractCompassEntity.java

public AbstractCompassEntity deepCopy() throws IOException, ClassNotFoundException {
    AbstractCompassEntity entity = null;
    forceEagerFetch();/*from  w  w  w .ja v a  2 s .c  om*/
    // Write the object out to a byte array
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bos);
    out.writeObject(this);
    out.flush();
    out.close();

    // Make an input stream from the byte array and read
    // a copy of the object back in.
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
    entity = (AbstractCompassEntity) in.readObject();
    entity.clearIdsAfterDeepCopy();
    return entity;
}

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 .ja  v a2s .c  o m
        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:org.nuxeo.ecm.core.redis.contribs.RedisInvalidations.java

private String serializeInvalidations(Invalidations invals) throws IOException {
    ByteArrayOutputStream baout = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(baout);
    out.writeObject(invals);/*from   w ww.  j  a v a 2 s . c  o m*/
    out.flush();
    out.close();
    // use base64 because Jedis don't have onMessage with bytes
    return Base64.encodeBase64String(baout.toByteArray());
}

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 . jav a2s. c  o  m*/
 */
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:foam.dao.index.PersistedIndex.java

@Override
public Object wrap(Object state) {
    synchronized (file_) {
        try {//from   ww  w  .j a  va 2  s .  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);
        }
    }
}

From source file:com.g3net.tool.ObjectUtils.java

/**
 * /*from  w ww . j  av a2s  .c  o m*/
 * @param o   ???java??,???
 * @return     
 * @throws Exception
 */
public static byte[] serializeObject(Object o) throws Exception {
    ByteArrayOutputStream bos = null;
    ObjectOutputStream oos = null;
    if (o == null) {
        return null;
    }

    try {
        bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        oos.writeObject(o);
        oos.flush();
        return bos.toByteArray();
    } finally {
        if (oos != null) {
            oos.close();
        }
    }
}

From source file:org.ambraproject.model.article.ArticleTypeTest.java

@Test
public void testDeserializedArticleTypeEquality() throws Exception {
    ArticleType art1 = ArticleType// www.j av  a 2s . com
            .getArticleTypeForURI(new URI("http://rdf.plos.org/RDF/articleType/Interview"), false);

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bout);

    out.writeObject(art1);
    out.flush();

    ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
    ObjectInputStream in = new ObjectInputStream(bin);

    ArticleType art2 = (ArticleType) in.readObject();

    assertTrue(art1 == art2, "Article 1 == Article 2");
    assertTrue(art1.equals(art2), "Article 1 should .equals() Article 2");
}

From source file:com.vnomicscorp.spring.security.cas.authentication.redis.DefaultCasAuthenticationTokenSerializer.java

@Override
public String serialize(CasAuthenticationToken token) throws CasAuthenticationTokenSerializerException {
    if (token == null) {
        throw new NullPointerException("Expected given token to be non-null");
    }// w w  w  .  j a v a2 s. c o m
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(token);
        oos.flush();
        return new String(Base64.encode(baos.toByteArray()), charset);
    } catch (IOException e) {
        throw new CasAuthenticationTokenSerializerException(e);
    }
}

From source file:org.openspotlight.persist.support.SimplePersistImpl.java

private static <T extends Serializable> InputStream asStream(final T o) throws Exception {
    if (o == null) {
        return null;
    }//  w  w  w  .  ja va  2 s. co  m
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream ois = new ObjectOutputStream(baos);
    ois.writeObject(o);
    ois.flush();
    return new ByteArrayInputStream(baos.toByteArray());
}