ByteQueue.java
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13: package xeij;
14:
15: public class ByteQueue {
16:
17: static final class Block {
18: volatile Block p = null;
19: volatile Block n = null;
20:
21: final int s = 1 << 12;
22: final byte[] a = new byte[s];
23: volatile long w = 0;
24: volatile long r = w;
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36: int clear () {
37: return skip (used ());
38: }
39:
40:
41:
42: int read () {
43: return r != w ? a[(int) (r++) & (s - 1)] & 0xff : -1;
44: }
45:
46:
47:
48: int read (byte[] array, int offset, int length) {
49: length = Math.min (length, (int) (w - r));
50: int offset0 = offset;
51: for (;;) {
52: int k = Math.min (length, s - ((int) r & (s - 1)));
53: if (k == 0) {
54: return offset - offset0;
55: }
56: System.arraycopy (a, (int) r & (s - 1),
57: array, offset,
58: k);
59: r += k;
60: offset += k;
61: length -= k;
62: }
63: }
64:
65:
66:
67: int skip (int length) {
68: length = Math.min (length, (int) (w - r));
69: r += length;
70: return length;
71: }
72:
73:
74:
75: int unused () {
76: return (int) (r + s - w);
77: }
78:
79:
80:
81: int used () {
82: return (int) (w - r);
83: }
84:
85:
86:
87: int write (int data) {
88: if (w == r + s) {
89: return 0;
90: }
91: a[(int) (w++) & (s - 1)] = (byte) data;
92: return 1;
93: }
94:
95:
96:
97: int write (byte[] array, int offset, int length) {
98: length = Math.min (length, (int) (r + s - w));
99: int offset0 = offset;
100: for (;;) {
101: int k = Math.min (length, s - ((int) w & (s - 1)));
102: if (k == 0) {
103: return offset - offset0;
104: }
105: System.arraycopy (array, offset,
106: a, (int) w & (s - 1),
107: k);
108: w += k;
109: offset += k;
110: length -= k;
111: }
112: }
113:
114: }
115:
116: volatile Block h = new Block ();
117: volatile Block t = h;
118:
119: final int s = 0x7fffffff;
120: volatile long w = 0;
121: volatile long r = w;
122:
123:
124:
125:
126:
127:
128:
129:
130:
131: int clear () {
132: return skip (used ());
133: }
134:
135:
136:
137: int read () {
138: int data = h.read ();
139: if (data < 0) {
140: if (h == t) {
141: return data;
142: }
143: Block n = h.n;
144: n.p = null;
145: h.n = null;
146: h = n;
147: data = h.read ();
148: }
149: r++;
150: return data;
151: }
152:
153:
154:
155: int read (byte[] array, int offset, int length) {
156:
157: long r0 = r;
158: int k = h.read (array, offset, length);
159: r += k;
160: offset += k;
161: length -= k;
162: while (length != 0) {
163: if (h == t) {
164: return (int) (r - r0);
165: }
166: Block n = h.n;
167: n.p = null;
168: h.n = null;
169: h = n;
170: k = h.read (array, offset, length);
171: r += k;
172: offset += k;
173: length -= k;
174: }
175: return (int) (r - r0);
176: }
177:
178:
179:
180: int skip (int length) {
181:
182: long r0 = r;
183: int k = h.skip (length);
184: r += k;
185: length -= k;
186: while (length != 0) {
187: if (h == t) {
188: return (int) (r - r0);
189: }
190: Block n = h.n;
191: n.p = null;
192: h.n = null;
193: h = n;
194: k = h.skip (length);
195: r += k;
196: length -= k;
197: }
198: return (int) (r - r0);
199: }
200:
201:
202:
203: int unused () {
204: return (int) (r + s - w);
205: }
206:
207:
208:
209: int used () {
210: return (int) (w - r);
211: }
212:
213:
214:
215: int write (int data) {
216: if (w == r + s) {
217: return 0;
218: }
219: if (t.write (data) == 0) {
220: Block n = new Block ();
221: n.p = t;
222: t.n = n;
223: t = n;
224: t.write (data);
225: }
226: w++;
227: return 1;
228: }
229:
230:
231:
232: int write (byte[] array, int offset, int length) {
233: length = Math.min (length, (int) (r + s - w));
234: long w0 = w;
235: int k = t.write (array, offset, length);
236: w += k;
237: offset += k;
238: length -= k;
239: while (length != 0) {
240: Block n = new Block ();
241: n.p = t;
242: t.n = n;
243: t = n;
244: k = t.write (array, offset, length);
245: w += k;
246: offset += k;
247: length -= k;
248: }
249: return (int) (w - w0);
250: }
251:
252: }