Java Object to Byte Array serializeAsByteArray(Object b)

Here you can find the source of serializeAsByteArray(Object b)

Description

Serialize an object as a raw byte array

License

Open Source License

Parameter

Parameter Description
the object

Exception

Parameter Description
IOException an exception

Return

byte array

Declaration

public static byte[] serializeAsByteArray(Object b) throws IOException 

Method Source Code


//package com.java2s;

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

import java.io.ObjectOutputStream;

public class Main {
    /**/*from  www .ja va 2 s .  c o  m*/
     * Serialize an object as a raw byte array
     * 
     * @param the object
     * @return byte array
     * @throws IOException
     */
    public static byte[] serializeAsByteArray(Object b) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bos);
        try {
            out.writeObject(b);
        } finally {
            out.close();
        }
        return bos.toByteArray();
    }
}

Related

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