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:org.openspaces.pu.container.jee.context.BootstrapWebApplicationContextListener.java

public static byte[] objectToByteBuffer(Object obj) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(outStream);
    out.writeObject(obj);/*w w w. j  av a 2 s . co m*/
    out.flush();
    byte[] result = outStream.toByteArray();
    out.close();
    outStream.close();

    return result;
}

From source file:ubic.BAMSandAllen.optimize.GreedyMultiThreaded.java

public static Object deepCopy(Object oldObj) throws Exception {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(oldObj);//from  w  w w .j ava 2 s .  c o  m
    oos.flush();
    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
    return ois.readObject();
}

From source file:net.sourceforge.jaulp.io.SerializedObjectUtils.java

/**
 * Writes the given object to the given File.
 *
 * @param obj/*from w w w  .j  a  v  a 2  s  .com*/
 *            The object to write to the File.
 * @param file
 *            In this file will be the object saved.
 * @return true if the object was written to the file otherwise false.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static boolean writeSerializedObjectToFile(final Object obj, final File file) throws IOException {
    boolean written = true;
    FileOutputStream fos = null;
    ObjectOutputStream oos = null;
    try {
        fos = new FileOutputStream(file);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
        oos.flush();
        oos.close();
    } finally {
        StreamUtils.closeOutputStream(oos);
        StreamUtils.closeOutputStream(fos);
    }
    return written;
}

From source file:com.amazon.sqs.javamessaging.message.SQSObjectMessage.java

/**
 * Serialize the <code>Serializable</code> object to <code>String</code>.
 *///w  ww . j a  v a  2  s .c  o  m
protected static String serialize(Serializable serializable) throws JMSException {
    if (serializable == null) {
        return null;
    }
    String serializedString;
    ObjectOutputStream objectOutputStream = null;
    try {
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        objectOutputStream = new ObjectOutputStream(bytesOut);
        objectOutputStream.writeObject(serializable);
        objectOutputStream.flush();
        serializedString = Base64.encodeAsString(bytesOut.toByteArray());
    } catch (IOException e) {
        LOG.error("IOException: cannot serialize objectMessage", e);
        throw convertExceptionToMessageFormatException(e);
    } finally {
        if (objectOutputStream != null) {
            try {
                objectOutputStream.close();
            } catch (IOException e) {
                LOG.warn(e.getMessage());
            }
        }
    }
    return serializedString;
}

From source file:org.apache.ode.daohib.bpel.BpelDAOConnectionImpl.java

/**
 * Helper method for inserting bpel events into the database.
 *
 * @param sess/*from w w w  .  j  av a 2s .c  o m*/
 * @param event
 * @param process
 * @param instance
 */
static void _insertBpelEvent(Session sess, BpelEvent event, ProcessDAO process, ProcessInstanceDAO instance) {
    HBpelEvent hevent = new HBpelEvent();
    hevent.setTstamp(new Timestamp(System.currentTimeMillis()));
    hevent.setType(BpelEvent.eventName(event));
    hevent.setDetail(event.toString());
    if (process != null)
        hevent.setProcess((HProcess) ((ProcessDaoImpl) process).getHibernateObj());
    if (instance != null)
        hevent.setInstance((HProcessInstance) ((ProcessInstanceDaoImpl) instance).getHibernateObj());
    if (event instanceof ScopeEvent)
        hevent.setScopeId(((ScopeEvent) event).getScopeId());
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(event);
        oos.flush();
        hevent.setData(bos.toByteArray());
    } catch (Throwable ex) {
        // this is really unexpected.
        __log.fatal("InternalError: BpelEvent serialization failed.", ex);
    }
    sess.save(hevent);
}

From source file:de.alpharogroup.io.SerializedObjectExtensions.java

/**
 * Writes the given object to the given File.
 *
 * @param obj//from   w w  w.  j av  a  2s .co m
 *            The object to write to the File.
 * @param file
 *            In this file will be the object saved.
 * @return true if the object was written to the file otherwise false.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static boolean writeSerializedObjectToFile(final Object obj, final File file) throws IOException {
    final boolean written = true;
    FileOutputStream fos = null;
    ObjectOutputStream oos = null;
    try {
        fos = new FileOutputStream(file);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
        oos.flush();
        oos.close();
    } finally {
        StreamExtensions.closeOutputStream(oos);
        StreamExtensions.closeOutputStream(fos);
    }
    return written;
}

From source file:de.alpharogroup.io.SerializedObjectUtils.java

/**
 * Writes the given object to the given File.
 *
 * @param obj// w  w  w  . j a va2  s  .co  m
 *            The object to write to the File.
 * @param file
 *            In this file will be the object saved.
 * @return true if the object was written to the file otherwise false.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static boolean writeSerializedObjectToFile(final Object obj, final File file) throws IOException {
    final boolean written = true;
    FileOutputStream fos = null;
    ObjectOutputStream oos = null;
    try {
        fos = new FileOutputStream(file);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
        oos.flush();
        oos.close();
    } finally {
        StreamUtils.closeOutputStream(oos);
        StreamUtils.closeOutputStream(fos);
    }
    return written;
}

From source file:org.socialhistoryservices.security.MongoTokenStore.java

private static byte[] serialize(Object state) {
    ObjectOutputStream oos = null;
    try {//  w ww .jav a 2s.co  m
        ByteArrayOutputStream bos = new ByteArrayOutputStream(512);
        oos = new ObjectOutputStream(bos);
        oos.writeObject(state);
        oos.flush();
        return bos.toByteArray();
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    } finally {
        if (oos != null) {
            try {
                oos.close();
            } catch (IOException e) {
                // eat it
            }
        }
    }
}

From source file:org.springframework.integration.ip.util.SocketTestUtils.java

/**
 * Sends two serialized objects over the same socket.
 * @param port/*from  w  ww .  j av a2s  . c  o  m*/
 */
public static void testSendSerialized(final int port) {
    Thread thread = new Thread(new Runnable() {
        public void run() {
            try {
                Socket socket = new Socket(InetAddress.getByName("localhost"), port);
                OutputStream outputStream = socket.getOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(outputStream);
                oos.writeObject(TEST_STRING);
                oos.flush();
                oos = new ObjectOutputStream(outputStream);
                oos.writeObject(TEST_STRING);
                oos.flush();
                Thread.sleep(1000000000L); // wait forever, but we're a daemon
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    thread.setDaemon(true);
    thread.start();
}

From source file:org.apache.storm.security.auth.AuthUtils.java

public static byte[] serializeKerberosTicket(KerberosTicket tgt) throws Exception {
    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bao);
    out.writeObject(tgt);/*from www . j  av  a  2s. c  o m*/
    out.flush();
    out.close();
    return bao.toByteArray();
}