Basic Usage - Write Data to the Buffer

suggest change

Given a ByteBuffer instance one can write primitive-type data to it using relative and absolute put. The striking difference is that putting data using the relative method keeps track of the index the data is inserted at for you, while the absolute method always requires giving an index to put the data at.

Both methods allow “chaining” calls. Given a sufficiently sized buffer one can accordingly do the following:

buffer.putInt(0xCAFEBABE).putChar('c').putFloat(0.25).putLong(0xDEADBEEFCAFEBABE);

which is equivalent to:

buffer.putInt(0xCAFEBABE);
buffer.putChar('c');
buffer.putFloat(0.25);
buffer.putLong(0xDEADBEEFCAFEBABE);

Do note that the method operating on bytes is not named specially. Additionally note that it’s also valid to pass both a ByteBuffer and a byte[] to put. Other than that, all primitive types have specialized put-methods.

An additional note: The index given when using absolute put* is always counted in bytes.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


ByteBuffer:
* Basic Usage - Write Data to the Buffer

Table Of Contents
8 Arrays
10 Maps
11 Strings
25 JAXB
29 Enums
32 Audio
41 Scanner
46 ByteBuffer
63 Logging
75 Lists
78 Sets
89 JAX-WS
96 XJC
98 Process
106 Modules
114 Applets
122 JNDI
139 JavaBean
141 Literals
144 Packages
150 JMX
153 JShell
159 Sockets
167 Enum Map
175 Hashtable
177 SortedMap