import java.nio.ByteBuffer;
public class BufferTest {
public static void main(String[] args) {
ByteBuffer buffer = ByteBuffer.allocate(100);
printBuffer("init after", buffer);
String text = "helloworld";
buffer.put(text.getBytes());
printBuffer("put after", buffer);
buffer.flip();
printBuffer("flip after", buffer);
while(buffer.remaining() > 0){
System.out.print((char)buffer.get());
}
System.out.println();
printBuffer("get after", buffer);
buffer.clear();
printBuffer("clear after", buffer);
}
public static void printBuffer(String state,ByteBuffer buffer){
System.out.println("state:"+state+",position:"+buffer.position()+",limit:"+buffer.limit()+",capacity:"+buffer.capacity());
}
}
public class BufferTest {
public static void main(String[] args) {
ByteBuffer buffer = ByteBuffer.allocate(100);
printBuffer("init after", buffer);
String text = "helloworld";
buffer.put(text.getBytes());
printBuffer("put after", buffer);
buffer.flip();
printBuffer("flip after", buffer);
while(buffer.remaining() > 0){
System.out.print((char)buffer.get());
}
System.out.println();
printBuffer("get after", buffer);
buffer.clear();
printBuffer("clear after", buffer);
}
public static void printBuffer(String state,ByteBuffer buffer){
System.out.println("state:"+state+",position:"+buffer.position()+",limit:"+buffer.limit()+",capacity:"+buffer.capacity());
}
}