-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewBuffers.java
More file actions
executable file
·61 lines (58 loc) · 2.07 KB
/
ViewBuffers.java
File metadata and controls
executable file
·61 lines (58 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package basic.nio;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;
/**
* ����������ͼ
*
* @author yangqc 2016/1/21
*/
public class ViewBuffers {
public static void main(String[] args) {
ByteBuffer bb = ByteBuffer
.wrap(new byte[]{0, 0, 0, 0, 0, 0, 0, 'a'});
bb.rewind(); // ���ƻ���������λ�ñ��Ϊ�㣬��������� rewind�᷵��һ��Buffer ��Buffer������
//��ͼ�Լ�ByteBuffer�ij���
System.out.println("Byte Buffer");
while (bb.hasRemaining()) //��֪��ǰλ�ú�����֮���Ƿ���Ԫ��
{
System.out.println(bb.position() + "->" + bb.get() + ", ");
}
System.out.println();
CharBuffer cb = ((ByteBuffer) bb.rewind()).asCharBuffer();
while (cb.hasRemaining()) {
System.out.println(cb.position() + "->" + cb.get() + ", ");
}
System.out.println();
IntBuffer ib = ((ByteBuffer) bb.rewind()).asIntBuffer();
System.out.println("IntBuffer");
while (ib.hasRemaining()) {
System.out.println(ib.position() + "->" + ib.get() + ", ");
}
System.out.println();
FloatBuffer fb = ((ByteBuffer) bb.rewind()).asFloatBuffer();
while (fb.hasRemaining()) {
System.out.println(fb.position() + "->" + fb.get() + ", ");
}
System.out.println();
LongBuffer lb = ((ByteBuffer) bb.rewind()).asLongBuffer();
while (lb.hasRemaining()) {
System.out.println(lb.position() + "->" + lb.get() + ",");
}
System.out.println();
ShortBuffer sb = ((ByteBuffer) bb.rewind()).asShortBuffer();
while (sb.hasRemaining()) {
System.out.println(sb.position() + "->" + sb.get() + ", ");
}
System.out.println();
DoubleBuffer db = ((ByteBuffer) bb.rewind()).asDoubleBuffer();
while (db.hasRemaining()) {
System.out.println(db.position() + "->" + db.get() + ", ");
}
System.out.println();
}
}