Java ByteBuffer from getByteBuffer(Object obj)

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

Description

Gets the byte buffer.

License

Open Source License

Parameter

Parameter Description
obj the obj

Exception

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

Return

the byte buffer

Declaration

public static ByteBuffer getByteBuffer(Object obj) throws IOException 

Method Source Code


//package com.java2s;

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

import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;

public class Main {
    /**//from  ww w.  j av a  2  s.c o m
     * Gets the byte buffer.
     *
     * @param obj the obj
     * @return the byte buffer
     * @throws IOException Signals that an I/O exception has occurred.
     */
    public static ByteBuffer getByteBuffer(Object obj) throws IOException {
        byte[] bytes = getBytes(obj);
        ByteBuffer buff = ByteBuffer.wrap(bytes);
        return buff;
    }

    /**
     * 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. getByteBuffer(byte[] array)
  2. getByteBuffer(byte[] data, int fromByte, int length)
  3. getByteBuffer(ByteBuffer source, int count)
  4. getByteBuffer(final String string, final Charset charset)
  5. getByteBuffer(int size)
  6. getByteBuffer(String base64)
  7. getByteBuffer(String filePath, int start, long size)
  8. getByteBufferArray(ByteBuffer byteBuffer)
  9. getByteBufferAsString(ByteBuffer buff)