36 lines
914 B
Java
36 lines
914 B
Java
// newio/UsingBuffers.java
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
// Visit http://OnJava8.com for more book information.
|
|
import java.nio.*;
|
|
|
|
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);
|
|
System.out.println(cb.rewind());
|
|
symmetricScramble(cb);
|
|
System.out.println(cb.rewind());
|
|
symmetricScramble(cb);
|
|
System.out.println(cb.rewind());
|
|
}
|
|
}
|
|
/* Output:
|
|
UsingBuffers
|
|
sUniBgfuefsr
|
|
UsingBuffers
|
|
*/
|