Here you can find the source of getByteBuffer(Object obj)
Parameter | Description |
---|---|
obj | the obj |
Parameter | Description |
---|---|
IOException | Signals that an I/O exception has occurred. |
public static ByteBuffer getByteBuffer(Object obj) throws IOException
//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; } }