ByteBuffer.java
3.89 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Decompiled with CFR 0_118.
*/
package com.day.io;
public class ByteBuffer {
private int length = 0;
private Chunk firstChunk;
private Chunk lastChunk;
public ByteBuffer() {
this.reset();
}
public void reset() {
this.firstChunk = this.lastChunk = new Chunk();
this.length = 0;
}
public byte[] get() {
byte[] ret = new byte[this.length];
this.get(ret, 0, this.length);
return ret;
}
public byte[] get(int num) {
byte[] ret = new byte[Math.min(num, this.length)];
this.get(ret, 0, ret.length);
return ret;
}
public int get(byte[] output, int offset, int num) {
if (num > this.length) {
num = this.length;
}
int total = num;
while (num > 0) {
int rd = this.firstChunk.get(output, offset, num);
offset += rd;
num -= rd;
this.length -= rd;
if (!this.firstChunk.isEmpty() || this.firstChunk.next == null) continue;
this.firstChunk = this.firstChunk.next;
}
return total;
}
public void put(byte[] input, int offset, int len) {
if (len > 0) {
this.lastChunk = this.lastChunk.put(input, offset, len);
this.length += len;
}
}
public void put(byte[] bytes) {
if (bytes != null) {
this.put(bytes, 0, bytes.length);
}
}
public void put(byte b) {
this.lastChunk = this.lastChunk.put(b);
++this.length;
}
public int length() {
return this.length;
}
public boolean isEmpty() {
return this.length == 0;
}
private static class Chunk {
private static final int SIZE = 4096;
private byte[] bytes;
private int start;
private int end;
private Chunk next;
private Chunk() {
this(null, 0, 0);
}
private Chunk(byte b) {
this(null, 0, 0);
this.bytes[this.start] = b;
++this.end;
}
private Chunk(byte[] input) {
this(input, 0, input.length);
}
private Chunk(byte[] input, int offset, int len) {
this.bytes = new byte[Math.max(len, 4096)];
this.start = 0;
this.end = len;
if (len > 0) {
System.arraycopy(input, offset, this.bytes, 0, len);
}
}
private int get(byte[] output, int offset, int len) {
if (len > this.length()) {
len = this.length();
}
System.arraycopy(this.bytes, this.start, output, offset, len);
this.start += len;
if (this.start == this.end) {
this.end = 0;
this.start = 0;
}
return len;
}
private Chunk put(byte[] input, int offset, int len) {
if (len < this.bytes.length - this.end) {
System.arraycopy(input, offset, this.bytes, this.end, len);
this.end += len;
return this;
}
int num = this.bytes.length - this.end;
System.arraycopy(input, offset, this.bytes, this.end, num);
this.end = this.bytes.length;
Chunk cnk = new Chunk(input, offset + num, len - num);
cnk.next = this.next;
this.next = cnk;
return cnk;
}
private Chunk put(byte b) {
if (1 < this.bytes.length - this.end) {
this.bytes[this.end++] = b;
return this;
}
Chunk cnk = new Chunk(b);
cnk.next = this.next;
this.next = cnk;
return cnk;
}
private boolean isEmpty() {
return this.start == this.end;
}
private int length() {
return this.end - this.start;
}
}
}