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.
*/
public class Main {
public static void 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();
}
private static void drainBuffer(CharBuffer buffer) {
while (buffer.hasRemaining()) {
System.out.print(buffer.get());
}
System.out.println("");
}
}
|