2015-04-20 15:36:01 -07:00
|
|
|
|
//: io/UsingBuffers.java
|
2015-05-29 14:18:51 -07:00
|
|
|
|
// <20>2015 MindView LLC: see Copyright.txt
|
2015-04-20 15:36:01 -07:00
|
|
|
|
import java.nio.*;
|
|
|
|
|
import static net.mindview.util.Print.*;
|
|
|
|
|
|
|
|
|
|
public class UsingBuffers {
|
|
|
|
|
private static void symmetricScramble(CharBuffer buffer){
|
|
|
|
|
while(buffer.hasRemaining()) {
|
|
|
|
|
buffer.mark();
|
|
|
|
|
char c1 = buffer.get();
|
|
|
|
|
char c2 = buffer.get();
|
|
|
|
|
buffer.reset();
|
|
|
|
|
buffer.put(c2).put(c1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
char[] data = "UsingBuffers".toCharArray();
|
|
|
|
|
ByteBuffer bb = ByteBuffer.allocate(data.length * 2);
|
|
|
|
|
CharBuffer cb = bb.asCharBuffer();
|
|
|
|
|
cb.put(data);
|
|
|
|
|
print(cb.rewind());
|
|
|
|
|
symmetricScramble(cb);
|
|
|
|
|
print(cb.rewind());
|
|
|
|
|
symmetricScramble(cb);
|
|
|
|
|
print(cb.rewind());
|
|
|
|
|
}
|
|
|
|
|
} /* Output:
|
|
|
|
|
UsingBuffers
|
|
|
|
|
sUniBgfuefsr
|
|
|
|
|
UsingBuffers
|
|
|
|
|
*///:~
|