Java Reference
In-Depth Information
Listing 9-2. Writing to and Reading from a Buffer
// BufferReadWrite.java
package com.jdojo.nio;
import java.nio.ByteBuffer;
public class BufferReadWrite {
public static void main(String[] args) {
// Create a byte buffer with a capacity of 8
ByteBuffer bb = ByteBuffer.allocate(8);
// Print the buffer info
System.out.println("After creation:");
printBufferInfo(bb);
// Populate buffer elements from 50 to 57
for (int i = 50; i < 58; i++) {
bb.put((byte) i);
}
// Print the buffer info
System.out.println("After populating data:");
printBufferInfo(bb);
}
public static void printBufferInfo(ByteBuffer bb) {
int limit = bb.limit();
System.out.println("Position = " + bb.position() +
", Limit = " + limit);
// Use absolute reading without affecting the position
System.out.print("Data: ");
for (int i = 0; i < limit; i++) {
System.out.print(bb.get(i) + " ");
}
System.out.println();
}
}
After creation:
Position = 0, Limit = 8
Data: 0 0 0 0 0 0 0 0
After populating data:
Position = 8, Limit = 8
Data: 50 51 52 53 54 55 56 57
Now you understand that there is a big difference in using relative and absolute methods for reading from and
writing to a buffer. Both methods have a working range. The data must be read and written in the working range. The
working range for relative and absolute methods is different.
 
Search WWH ::




Custom Search