/
ByteBufsExample.java
47 lines (37 loc) · 1.3 KB
/
ByteBufsExample.java
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
import io.activej.bytebuf.ByteBuf;
import io.activej.bytebuf.ByteBufs;
import java.util.Arrays;
public final class ByteBufsExample {
private static final ByteBufs BUFS = new ByteBufs();
private static void addingToBufs() {
//[START REGION_1]
BUFS.add(ByteBuf.wrapForReading(new byte[]{0, 1, 2, 3}));
BUFS.add(ByteBuf.wrapForReading(new byte[]{3, 4, 5}));
// bufs consist of 2 Bufs at this moment
//[END REGION_1]
System.out.println(BUFS);
System.out.println();
}
private static void takingBufOutOfBufs() {
ByteBuf takenBuf = BUFS.take();
// Buf that is taken is the one that was put in bufs first
System.out.println("Buf taken from bufs: " + Arrays.toString(takenBuf.asArray()));
System.out.println();
}
private static void takingEverythingOutOfBufs() {
//[START REGION_2]
// Adding one more ByteBuf to bufs
BUFS.add(ByteBuf.wrapForReading(new byte[]{6, 7, 8}));
ByteBuf takenBuf = BUFS.takeRemaining();
// Taken ByteBuf is combined of every ByteBuf that were in bufs
//[END REGION_2]
System.out.println("Buf taken from bufs: " + Arrays.toString(takenBuf.asArray()));
System.out.println("Is 'ByteBufs' empty? " + BUFS.isEmpty());
System.out.println();
}
public static void main(String[] args) {
addingToBufs();
takingBufOutOfBufs();
takingEverythingOutOfBufs();
}
}