Is it possible to convert strongly-typed data to bytes using the ByteBuffer
class? If not, what is its main purpose? If yes, I am looking at its documentation and can find nothing. The put
methods require a byte and not an int fro example.

- 20,411
- 61
- 165
- 254
-
1You haven't described what you actually want to *do*. What does "convert strongly-typed data to bytes" mean in your application? – Greg Hewgill Aug 31 '11 at 02:44
3 Answers
as you might know, in computer programming most of the Data is somehow stored on the lower levels in Byte Format.
ByteBuffer =Convenient Read/Write of "all" Data Types to/from a Byte-Representation:
ByteBuffer is a very convenient Class to convert Bytes from types "like Int" to a Byte representation. But also the other way round. You can read "types" from a byte representation with the read methods.
Regarding your put Question: There is a generic put method that accepts a byte. But also a lot of convenience methods to put and read most of the standard datatypes, also int (as you asked):
http://download.oracle.com/javase/1,5.0/docs/api/java/nio/ByteBuffer.html#putInt(int)
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
byteBuffer.putInt(4);
byteBuffer.get() => gets the byte representation of the Int you put in
Practical Use Case:
To be honest, the most heaviest use of ByteBuffer for me has been so far with Cassandra NoSQL DB. They store all the Data as byte(arrays) and ByteBuffer is a convenient Class to help to yout read and write those data.
High-End Usage:
As you can see the class is in NIO Package. I think the origin of ByteBuffer was to very very efficiently write Data for example to disk. It does not write it in memory before but directly maps a region of "blocks" on disk to the buffer in java. So it avoid any intermediate steps for reading and writing data to disk. But this is very highend usage...

- 4,062
- 4
- 39
- 42
-
How would this work: `ByteBuffer byteBuffer = ByteBuffer.allocate(2); byteBuffer.putInt(4);` An int needs 4 bytes and you are just allocating 2 bytes. – darksky Aug 31 '11 at 13:10
-
Erm, you mean like ...
String foo = "My String";
ByteBuffer myBuffer = ByteBuffer.wrap(foo.getBytes());
EDIT: For an int
you can do...
int i = 1234;
ByteBuffer b = ByteBuffer.allocate(4);
b.putInt(i);

- 76,169
- 12
- 136
- 161
-
So this will take `foo` and convert it into bytes and put it into the `ByteBuffer`? This is exactly what I am looking for. – darksky Aug 31 '11 at 02:45
-
Correct. `wrap` is a static method in `ByteBuffer` that takes a byte array as its argument and returns a new `ByteBuffer`. `String`'s `getBytes()` method returns a byte array. – Brian Roach Aug 31 '11 at 02:47
-
-
@bdares - That is correct. Note that he wasn't specific, and unquestionably this is an example of how you can create a ByteBuffer from something else :) – Brian Roach Aug 31 '11 at 02:53
-
Got it.. I found a way to do it though. `byte[] array = ByteBuffer.allocate(4).putInt(intHere).array();` – darksky Aug 31 '11 at 02:54
-
+1: You may want to set the byte order with `order(ByteOrder.LITTE_ENDIAN)` as the default is BIG_ENDIAN. Provided both ends are the same it doesn't matter however your platform may be more efficient with little endian (on X86/x64 it is) – Peter Lawrey Aug 31 '11 at 06:18
Well, you can serialize your objects if they implement serializable
and store the serialized bytes in a ByteBuffer
if you really want to.
The main purpose of ByteBuffer appears to be buffering bytes, so that asynchronous transfers can be sped up.
It seems to be that you're thinking of getting the chunk of RAM where the object resides and placing that chunk into a ByteBuffer. This won't work. I mean, you could do it, but good luck getting the object back into a usable state. You have to do work on it, like turn the pointers into meaningful new addresses (referring to the places where you stored the actual fields of the object or whatnot). This is serializing, and this is what serializable
does.