Java CharBuffer mark and reset to set buffer content
import java.nio.ByteBuffer; import java.nio.CharBuffer; public class Main { public static void main(String[] args) { char[] data = "demo2s.com".toCharArray(); ByteBuffer bb = ByteBuffer.allocate(data.length * 2); CharBuffer cb = bb.asCharBuffer(); cb.put(data);/*from w w w . j a v a 2s.c om*/ System.out.println(cb.rewind()); symmetricScramble(cb); System.out.println(cb.rewind()); symmetricScramble(cb); System.out.println(cb.rewind()); } 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); } } }