Java Object to Byte Array serializeToByteArray(Object obj)

Here you can find the source of serializeToByteArray(Object obj)

Description

serialize To Byte Array

License

Open Source License

Declaration

public static byte[] serializeToByteArray(Object obj) throws NotSerializableException 

Method Source Code


//package com.java2s;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;

import java.io.ObjectOutputStream;

public class Main {
    public static byte[] serializeToByteArray(Object obj) throws NotSerializableException {
        try {/*from   ww  w  .java 2s .  c o  m*/
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(baos);
            out.writeObject(obj);
            return baos.toByteArray();
        } catch (NotSerializableException e) {
            e.fillInStackTrace();
            throw e;
        } catch (IOException e) {

            throw new Error("IOException writing to a byte array!");
        }
    }

    public static byte[] toByteArray(Object obj) throws NotSerializableException {
        return serializeToByteArray(obj);
    }
}

Related

  1. getBytes(Object obj)
  2. getBytes(Object obj)
  3. getBytes(Object v, byte[] defaultValue)
  4. serializeAsByteArray(Object b)
  5. serializeToByteArray(Object obj)
  6. serializeToByteArray(Object object)
  7. serializeToByteArray(Serializable object)
  8. serializeToByteArray(Serializable value)
  9. serializeToByteArrayNoHeader(Serializable o)