forked from tianshb/JavaLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSliceBuffer.java
More file actions
33 lines (26 loc) · 750 Bytes
/
Copy pathSliceBuffer.java
File metadata and controls
33 lines (26 loc) · 750 Bytes
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
package com.crow;
import java.nio.ByteBuffer;
/**
* Created by CrowHawk on 17/9/4.
*/
public class SliceBuffer {
public static void main(String[] args) throws Exception {
ByteBuffer buffer = ByteBuffer.allocate(10);
for(int i = 0; i < buffer.capacity(); i++) {
buffer.put((byte) i);
}
buffer.position(3);
buffer.limit(7);
ByteBuffer slice = buffer.slice();
for(int i = 0; i < slice.capacity(); i++) {
byte b = slice.get(i);
b *= 11;
slice.put(i, b);
}
buffer.position(0);
buffer.limit(buffer.capacity());
while (buffer.hasRemaining()) {
System.out.println(buffer.get());
}
}
}