import java.nio.CharBuffer;
/**
* Buffer fill/drain example. This code uses the simplest means of filling and
* draining a buffer: one element at a time.
*/
publicclass Main {
publicstaticvoid main(String[] argv) throws Exception {
CharBuffer buffer = CharBuffer.allocate(100);
String string = "asdf";
for (int i = 0; i < string.length(); i++) {
buffer.put(string.charAt(i));
}
buffer.flip();
drainBuffer(buffer);
buffer.clear();
}
privatestaticvoid drainBuffer(CharBuffer buffer) {
while (buffer.hasRemaining()) {
System.out.print(buffer.get());
}
System.out.println("");
}
}