Java Utililty Methods Object to Byte Array

List of utility methods to do Object to Byte Array

Description

The list of methods to do Object to Byte Array are organized into topic(s).

Method

byte[]getBytes(Object o)
get Bytes
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(5000);
ObjectOutputStream os = new ObjectOutputStream(new BufferedOutputStream(byteStream));
os.flush();
os.writeObject(o);
os.flush();
return byteStream.toByteArray();
byte[]getBytes(Object o)
get Bytes
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
ObjectOutputStream obOut = null;
try {
    obOut = new ObjectOutputStream(bOut);
    obOut.writeObject(o);
    obOut.flush();
    bOut.flush();
    obOut.close();
...
byte[]getBytes(Object o)
get Bytes
if (o instanceof String) {
    return ((String) o).getBytes();
} else if (o instanceof byte[]) {
    return ((byte[]) o);
} else if (o instanceof Serializable) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    try {
...
byte[]getBytes(Object obj)
get Bytes
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.flush();
oos.close();
bos.close();
byte[] data = bos.toByteArray();
return data;
...
byte[]getBytes(Object obj)
Gets the bytes.
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bout);
out.writeObject(obj);
out.flush();
byte[] bytes = bout.toByteArray();
bout.close();
out.close();
return bytes;
...
byte[]getBytes(Object obj)
get Bytes
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
    baos = new ByteArrayOutputStream();
    oos = new ObjectOutputStream(baos);
    oos.writeObject(obj);
    oos.flush();
} catch (IOException e) {
...
byte[]getBytes(Object obj)
get Bytes
byte[] b = null;
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
try {
    bos = new ByteArrayOutputStream();
    oos = new ObjectOutputStream(bos);
    oos.writeObject(obj);
    oos.flush();
...
byte[]getBytes(Object obj)
get Bytes
if (obj instanceof byte[])
    return (byte[]) obj;
if (obj instanceof String)
    return ((String) obj).getBytes();
try {
    return serialize(obj);
} catch (Exception ex) {
return new byte[] {};
byte[]getBytes(Object obj)
get Bytes
if (obj == null)
    return null;
if (obj instanceof String) {
    try {
        return ((String) obj).getBytes(Charset);
    } catch (UnsupportedEncodingException e) {
        return null;
java.io.ByteArrayOutputStream a = new java.io.ByteArrayOutputStream();
try {
    ObjectOutputStream o = new ObjectOutputStream(a);
    o.writeObject(obj);
    o.flush();
    byte[] bytes = a.toByteArray();
    o.close();
    a.close();
    return bytes;
} catch (IOException e) {
    return null;
byte[]getBytes(Object v, byte[] defaultValue)
get Bytes
if (v == null || VALUE_BLANK.equals(v)) {
    return null;
} else if (v instanceof byte[]) {
    return (byte[]) v;
} else if (v instanceof java.io.Serializable) {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream oout;
    try {
...