Java ByteBuffer from Object toByteBuffer(Object val)

Here you can find the source of toByteBuffer(Object val)

Description

to Byte Buffer

License

Apache License

Declaration

public static ByteBuffer toByteBuffer(Object val) 

Method Source Code

//package com.java2s;
/**/*from w  w  w.  j av a 2  s. co  m*/
 * Copyright 2011 CaneData.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.Date;

public class Main {
    public static ByteBuffer toByteBuffer(Object val) {
        ByteBuffer buffer = null;

        try {
            if (val instanceof Byte) {
                buffer = ByteBuffer.allocate(Byte.SIZE);

                buffer.put(0, (Byte) val);

                return buffer;
            }

            if (val instanceof byte[])
                return ByteBuffer.wrap((byte[]) val);

            if (val instanceof ByteBuffer)
                return (ByteBuffer) val;

            if (val instanceof Character) {
                buffer = ByteBuffer.allocate(Character.SIZE / Byte.SIZE);

                buffer.putChar(0, (Character) val);

                return buffer;
            }

            if (val instanceof Short) {
                buffer = ByteBuffer.allocate(Short.SIZE / Byte.SIZE);

                buffer.putShort(0, (Short) val);

                return buffer;
            }

            if (val instanceof Integer) {
                buffer = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE);

                buffer.putInt(0, ((Integer) val).intValue());

                return buffer;
            }

            if (val instanceof Boolean) {
                byte[] v = new byte[1];
                if ((Boolean) val)
                    v[0] = 1;
                else
                    v[0] = 0;

                return ByteBuffer.wrap(v);
            }

            if (val instanceof Double) {
                buffer = ByteBuffer.allocate(Double.SIZE / Byte.SIZE);

                buffer.putDouble(0, (Double) val);

                return buffer;
            }

            if (val instanceof Float) {
                buffer = ByteBuffer.allocate(Float.SIZE / Byte.SIZE);

                buffer.putFloat(0, (Float) val);

                return buffer;
            }

            if (val instanceof Long) {
                buffer = ByteBuffer.allocate(Long.SIZE / Byte.SIZE);

                buffer.putLong(0, (Long) val);

                return buffer;
            }

            if (val instanceof String) {
                try {
                    return ByteBuffer.wrap(((String) val).getBytes("UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
            }

            if (val instanceof Date) {
                buffer = ByteBuffer.allocate(Long.SIZE / Byte.SIZE);

                buffer.putLong(0, ((Date) val).getTime());

                return buffer;
            }

            return objectToByteBuffer(val);

        } finally {
            //         if (null != buffer)
            //            buffer.flip();
        }

    }

    public static byte[] getBytes(Object source) {
        return toByteBuffer(source).array();
    }

    public static ByteBuffer objectToByteBuffer(Object target) {
        return ByteBuffer.wrap(objectToByte(target));
    }

    public static byte[] objectToByte(Object target) {
        try {
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(target);
            oo.flush();

            byte[] rlt = bo.toByteArray();
            bo.close();

            return rlt;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. objectToByteBuffer(Object object)
  2. objectToByteBuffer(Object target)
  3. toByteBuffer(Serializable serializable)