Java Convert via ByteBuffer toBytes(Object obj)

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

Description

Serializes an Object to byte[].

License

Open Source License

Parameter

Parameter Description
obj the object to convert

Exception

Parameter Description
IOException an exception

Return

the object as byte[]

Declaration

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

Method Source Code

//package com.java2s;
/*//w  w w. java  2  s .  c om
Copyright (c) 2012 Thomas Zink, 
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

import java.io.ObjectOutputStream;

import java.nio.ByteBuffer;

public class Main {
    /**
      * Serializes an Object to byte[].
      * The whole Object is serialized. This is not the same as using
      * byte buffers.
      * 
      * @param obj the object to convert
      * @return the object as byte[]
      * @throws IOException 
      */
    public static byte[] toBytes(Object obj) throws IOException {
        ByteArrayOutputStream bstream = new ByteArrayOutputStream();
        ObjectOutputStream ostream = new ObjectOutputStream(bstream);
        ostream.writeObject(obj);
        return bstream.toByteArray();
    }

    /**
     * Converts a Short to byte[]
     * 
     * @param val value to convert
     * @return the byte[] representation
     */
    public static byte[] toBytes(Short val) {
        ByteBuffer buffer = ByteBuffer.allocate(2);
        buffer.putShort(val);
        byte[] bytes = buffer.array();
        buffer.clear();
        return bytes;
    }

    /**
     * Converts an Integer to byte[]
     * 
     * @param val value to convert
     * @return the byte[] representation
     */
    public static byte[] toBytes(Integer val) {
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.putInt(val);
        byte[] bytes = buffer.array();
        buffer.clear();
        return bytes;
    }

    /**
     * Converts a Long to byte[]
     * 
     * @param val value to convert
     * @return the byte[] representation
     */
    public static byte[] toBytes(Long val) {
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.putLong(val);
        byte[] bytes = buffer.array();
        buffer.clear();
        return bytes;
    }

    /**
     * Converts a Float to byte[]
     * 
     * @param val value to convert
     * @return the byte[] representation
     */
    public static byte[] toBytes(Float val) {
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.putFloat(val);
        byte[] bytes = buffer.array();
        buffer.clear();
        return bytes;
    }

    /**
     * Converts a Double to byte[]
     * 
     * @param val value to convert
     * @return the byte[] representation
     */
    public static byte[] toBytes(Double val) {
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.putDouble(val);
        byte[] bytes = buffer.array();
        buffer.clear();
        return bytes;
    }
}

Related

  1. toBytes(final UUID uuid)
  2. toBytes(InputStream input)
  3. toBytes(int value)
  4. toBytes(long l)
  5. toBytes(Number value)
  6. toBytes(String str, boolean wideChar)
  7. toBytes(String value)
  8. toBytes(UUID id)
  9. toBytesAsString(UUID uuid)