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:com.moz.fiji.hive.utils.FijiDataRequestSerializer.java

/**
 * Serializes a data request.//from  w w w . j  ava  2s  . c  om
 *
 * @param dataRequest A data request.
 * @return The serialized data request.
 * @throws IOException If there is an error.
 */
public static String serialize(FijiDataRequest dataRequest) throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    try {
        oos.writeObject(dataRequest);
        oos.flush();
        return Base64.encodeBase64String(baos.toByteArray());
    } finally {
        IOUtils.closeQuietly(oos);
    }
}

From source file:org.kiji.hive.utils.KijiDataRequestSerializer.java

/**
 * Serializes a data request.//from  w ww.ja  v  a  2s. c o m
 *
 * @param dataRequest A data request.
 * @return The serialized data request.
 * @throws IOException If there is an error.
 */
public static String serialize(KijiDataRequest dataRequest) throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    try {
        oos.writeObject(dataRequest);
        oos.flush();
        return Base64.encodeBase64String(baos.toByteArray());
    } finally {
        IOUtils.closeQuietly(oos);
    }
}

From source file:Serialization.java

/**
 * Gets an array of bytes corresponding to the given object.
 * @param object the object to serialize
 * @return an array of bytes./*from   w  w w.j ava  2  s . c  om*/
 * @throws IOException if the object can't be turned into an array of bytes.
 */
public static byte[] storeObject(final Serializable object) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(baos);
        oos.writeObject(object);
        oos.flush();
        return baos.toByteArray();
    } finally {
        if (oos != null) {
            oos.close();
        }
        if (baos != null) {
            baos.close();
        }
    }
}

From source file:llc.rockford.webcast.StringUtils.java

public static String encodeBase64BinaryFile(InputStream is) {
    String base64EncodedFile = "";
    try {/*from  www  .  j ava2s  . c o m*/
        byte[] bytesFromFile = IOUtils.toByteArray(is);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream stream = new ObjectOutputStream(baos);
        stream.write(bytesFromFile);
        stream.flush();
        stream.close();
        base64EncodedFile = new String(Base64.encodeBase64(baos.toByteArray()));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return base64EncodedFile;
}

From source file:org.deeplearning4j.util.SerializationUtils.java

public static void saveObject(Object toSave, File saveTo) {
    try {//from   ww  w.j  av  a  2  s  . c  o m
        OutputStream os1 = FileUtils.openOutputStream(saveTo);
        ObjectOutputStream os = new ObjectOutputStream(os1);
        os.writeObject(toSave);
        os.flush();
        os.close();
        os1.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

From source file:com.googlecode.osde.internal.utils.MapUtil.java

public static String toString(Map<String, String> data) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(baos);
    out.writeObject(data == null ? new HashMap<String, String>() : data);
    out.flush();
    byte[] bytes = baos.toByteArray();
    byte[] encoded = Base64.encodeBase64(bytes);
    return new String(encoded, "UTF-8");
}

From source file:Main.java

/**
 * Convert Object to Byte Array//www  .j a  va 2  s. c o  m
 * 
 * @param obj
 * @return
 * @throws IOException
 */
public static byte[] convertObjectToByteArray(Object obj) throws IOException {
    ObjectOutputStream os = null;
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream(5000);
    os = new ObjectOutputStream(new BufferedOutputStream(byteStream));
    os.flush();
    os.writeObject(obj);
    os.flush();
    byte[] sendBuf = byteStream.toByteArray();
    os.close();
    return sendBuf;

}

From source file:io.smartspaces.scheduling.quartz.orientdb.internal.util.SerialUtils.java

private static byte[] stringMapToBytes(Object object) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(baos);
    out.writeObject(object);/*from w  w  w  . java  2  s  .  com*/
    out.flush();
    return baos.toByteArray();
}

From source file:com.weibo.api.motan.protocol.restful.support.RestfulUtil.java

public static Response serializeError(Exception ex) {
    try {//from w w w  .  j  a  v  a 2 s. c o  m
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(ex);
        oos.flush();

        String info = new String(Base64.encodeBytesToBytes(baos.toByteArray()),
                MotanConstants.DEFAULT_CHARACTER);
        return Response.status(Status.EXPECTATION_FAILED).header(EXCEPTION_HEADER, ex.getClass()).entity(info)
                .build();
    } catch (IOException e) {
        LoggerUtil.error("serialize " + ex.getClass() + " error", e);

        return Response.status(Status.INTERNAL_SERVER_ERROR).entity("serialization " + ex.getClass() + " error")
                .build();
    }
}

From source file:Main.java

/**
 * Serialize any object into String//from   w  w  w .ja va  2  s  .co m
 * @param object Object
 * @return String
 * @throws IOException If unable to access Stream
 */
public static String serialize(Object object) throws java.io.IOException {
    if (object == null)
        return null;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(
            new Base64OutputStream(byteArrayOutputStream, 0));
    objectOutputStream.writeObject(object);
    objectOutputStream.flush();
    objectOutputStream.close();
    return byteArrayOutputStream.toString();
}