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:BytesUtil.java

public static byte[] toByteArray(Object obj) throws IOException {
    byte[] bytes = null;
    ByteArrayOutputStream bos = null;
    ObjectOutputStream oos = null;
    try {//from  w ww . j  a v  a2 s  .c  o m
        bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        oos.flush();
        bytes = bos.toByteArray();
    } finally {
        if (oos != null) {
            oos.close();
        }
        if (bos != null) {
            bos.close();
        }
    }
    return bytes;
}

From source file:fr.inria.atlanmod.neoemf.data.hbase.util.HBaseEncoderUtil.java

public static byte[] toBytes(String[] strings) {
    try {//from  w  w w  .ja  va 2s  . c  o  m
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(strings);
        objectOutputStream.flush();
        objectOutputStream.close();
        return byteArrayOutputStream.toByteArray();
    } catch (IOException e) {
        NeoLogger.error("Unable to convert ''{0}'' to byte[]", Arrays.toString(strings));
    }
    return null;
}

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

private static String getKeyOfNonSerializableStringMapEntry(Map<String, ?> data) {
    for (Map.Entry<String, ?> entry : data.entrySet()) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {//from  ww  w.j a  v  a  2s  .  com
            ObjectOutputStream out = new ObjectOutputStream(baos);
            out.writeObject(entry.getValue());
            out.flush();
        } catch (IOException e) {
            return entry.getKey();
        }
    }
    return null;
}

From source file:org.openhie.openempi.util.SerializationUtil.java

public static byte[] serializeObject(Object o) {
    log.debug("Attempting to serialize object into memory");
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try {//from   ww  w  .j a v a2s  . c  o m
        ObjectOutputStream ois = new ObjectOutputStream(new BufferedOutputStream(byteArrayOutputStream));
        ois.writeObject(o);
        ois.flush();
        ois.close();
    } catch (Exception e) {
        log.error("Failed while serializing matchConfiguration (into memory): " + e.getMessage(), e);
        throw new RuntimeException("Failed while serializing object (into memory): " + e.getMessage());
    }
    return byteArrayOutputStream.toByteArray();
}

From source file:Main.java

public static byte[] getBytes(Serializable obj) {
    byte[] bytes = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {/*from  w  w  w  .j a v a 2s  .  c om*/
        oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        oos.flush();
        bytes = bos.toByteArray();
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (oos != null) {
            try {
                oos.close();
                bos.close();
                bos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    return bytes;
}

From source file:org.ow2.proactive_grid_cloud_portal.cli.utils.FileUtility.java

public static void writeObjectToFile(Object object, File file) {
    FileOutputStream outputStream = null;
    try {/*from ww w . j  ava 2 s .  c  o m*/
        outputStream = new FileOutputStream(file);
        ObjectOutputStream decorated = new ObjectOutputStream(outputStream);
        decorated.writeObject(object);
        decorated.flush();
    } catch (IOException ioe) {
        throw new CLIException(REASON_IO_ERROR, ioe);
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
}

From source file:com.ebay.erl.mobius.util.SerializableUtil.java

public final static String serializeToBase64(Serializable obj) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {/*from www.  j  a  v  a  2  s .  c  om*/
        oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        oos.flush();
        oos.close();

        String result = new String(Base64.encodeBase64(bos.toByteArray()), "UTF-8");
        return result;
    } catch (NotSerializableException e) {
        throw new IllegalArgumentException("Cannot serialize " + obj.getClass().getCanonicalName(), e);
    } finally {
        try {
            bos.close();
        } catch (Throwable e) {
            e = null;
        }

        try {
            if (oos != null)
                oos.close();
        } catch (Throwable e) {
            e = null;
        }
    }
}

From source file:Main.java

public static boolean saveObject(@NonNull Context context, Object obj, String fileName) {
    if (obj == null || TextUtils.isEmpty(fileName)) {
        return false;
    }//from   ww w  . jav  a 2s .co m

    FileOutputStream fos = null;
    ObjectOutputStream oos = null;
    try {
        fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
        oos.flush();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            if (fos != null)
                fos.close();
            if (oos != null)
                oos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static boolean saveObject(Context context, Serializable ser, String file) {
    FileOutputStream fos = null;//from   www .j a  va 2  s  . c  om
    ObjectOutputStream oos = null;
    try {
        fos = context.openFileOutput(file, Context.MODE_PRIVATE);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(ser);
        oos.flush();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            oos.close();
        } catch (Exception e) {
        }
        try {
            fos.close();
        } catch (Exception e) {
        }
    }
}

From source file:com.jaspersoft.jasperserver.core.util.StreamUtil.java

public static byte[] compress(Object object) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = new GZIPOutputStream(baos);
    ObjectOutputStream oos = new ObjectOutputStream(gzos);
    oos.writeObject(object);/*from   w w  w  .  jav  a2s.  c  om*/
    oos.flush();
    oos.close();
    return baos.toByteArray();
}