import java.nio.ByteBuffer;
import java.nio.CharBuffer;
public class UsingBuffers {
private static void symmetricScramble(CharBuffer buffer){
//若有介于position和limit之间的元素,则返回true
while(buffer.hasRemaining()){
buffer.mark();
char c1 = buffer.get();
char c2 = buffer.get();
buffer.reset();//把position设为mark的值
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());//把position的值设为起始位置
symmetricScramble(cb);
System.out.println(cb.rewind());
symmetricScramble(cb);
System.out.println(cb.rewind());
}
/*
UsingBuffers
sUniBgfuefsr
UsingBuffers
*/
}
[java][nio]用Buffer操纵数据
最新推荐文章于 2024-03-06 19:43:58 发布