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

public static byte[] toByteArray(Object obj) throws IOException {
    byte[] bytes = null;
    ByteArrayOutputStream bos = null;
    ObjectOutputStream oos = null;
    try {//  w  ww. ja v  a2s.  c  om
        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:Main.java

public static void put_(String key, Serializable value) {
    ByteArrayOutputStream baos = null;
    ObjectOutputStream oos = null;
    try {/*from   ww  w . j a  va 2s  .  c om*/
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(value);
        byte[] data = baos.toByteArray();
        put(key, data);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            oos.close();
        } catch (IOException e) {
        }
    }
}

From source file:com.cj.restspecs.mojo.Util.java

public static void writeObject(Object o, File path) {
    try {//from  w  w w.  j  a  va2  s . c  o m
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(path));
        try {
            out.writeObject(o);
        } finally {
            out.close();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static byte[] getBytes(Serializable obj) {
    byte[] bytes = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {/*from  w ww .j a v a2s . 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:com.comichentai.serialize.SerializeUtil.java

public static byte[] serialize(Object obj) {
    ByteArrayOutputStream baos = null;
    ObjectOutputStream oos = null;
    try {/*w ww. j a va2  s. c  om*/
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        return baos.toByteArray();
    } catch (RuntimeException re) {
        throw re;
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        closeQuietly(baos);
        closeQuietly(oos);
    }
}

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   w w  w  . j  a va  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:com.cloudera.kitten.util.LocalDataHelper.java

public static <T> String serialize(Map<String, T> mapping) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*from  ww w  .  j av  a 2s  . c om*/
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(mapping);
        oos.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return Base64.encodeBase64String(baos.toByteArray());
}

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  w w  w .  j a  v a 2  s  .  c  om

    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 void writeStroke(Stroke aStroke, ObjectOutputStream out) throws IOException {
    if (aStroke instanceof Serializable) {
        out.writeBoolean(true);/*from   w  ww. ja  v a2 s .c om*/
        out.writeBoolean(true);
        out.writeObject(aStroke);
    } else if (aStroke instanceof BasicStroke) {
        out.writeBoolean(true);
        out.writeBoolean(false);
        BasicStroke s = (BasicStroke) aStroke;

        float[] dash = s.getDashArray();

        if (dash == null) {
            out.write(0);
        } else {
            out.write(dash.length);
            for (int i = 0; i < dash.length; i++) {
                out.writeFloat(dash[i]);
            }
        }

        out.writeFloat(s.getLineWidth());
        out.writeInt(s.getEndCap());
        out.writeInt(s.getLineJoin());
        out.writeFloat(s.getMiterLimit());
        out.writeFloat(s.getDashPhase());
    } else {
        out.writeBoolean(false);
    }
}

From source file:Main.java

public static boolean saveObject(Context context, Serializable ser, String file) {
    FileOutputStream fos = null;/*w  ww .j  a  v  a 2  s  .  c o  m*/
    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) {
        }
    }
}