Java Object to Byte Array getBytes(Object obj)

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

Description

Gets the bytes.

License

Open Source License

Parameter

Parameter Description
obj the obj

Exception

Parameter Description
IOException Signals that an I/O exception has occurred.

Return

the bytes

Declaration

public static byte[] getBytes(Object obj) throws IOException 

Method Source Code


//package com.java2s;

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

import java.io.ObjectOutputStream;

public class Main {
    /**//from   ww  w  . ja  va  2 s.  c  o  m
     * Gets the bytes.
     *
     * @param obj the obj
     * @return the bytes
     * @throws IOException Signals that an I/O exception has occurred.
     */
    public static byte[] getBytes(Object obj) throws IOException {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bout);
        out.writeObject(obj);
        out.flush();
        byte[] bytes = bout.toByteArray();
        bout.close();
        out.close();
        return bytes;
    }
}

Related

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