/
OutputStreamL1.java
336 lines (306 loc) · 10.7 KB
/
OutputStreamL1.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package net.ME1312.SubData.Server.Library;
import net.ME1312.Galaxi.Library.Callback.ExceptionRunnable;
import net.ME1312.Galaxi.Library.Util;
import java.io.IOException;
import java.io.OutputStream;
import java.net.SocketException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.logging.Logger;
import static net.ME1312.SubData.Server.Library.DataSize.*;
/**
* SubData Layer 1 OutputStream Class
*/
public class OutputStreamL1 extends OutputStream {
private static final int[] controls = {'\u0018', '\u0017'};
private final EscapedOutputStream escaped;
private final OutputStream out;
private final Logger log;
private LinkedList<ExceptionRunnable> queue;
private byte[][][][] blocks;
private int gbL, mbL, kbL, bL;
private int gb, mb, kb, b;
private long stored;
private long limit;
static {
Arrays.sort(controls);
}
/**
* Creates a SubData Layer 1 OutputStream
*
* @param stream Super stream
*/
public OutputStreamL1(Logger log, OutputStream stream, long limit) {
this.log = log;
this.out = stream;
this.escaped = new EscapedOutputStream(out, '\u0010', '\u0017');
this.limit = limit;
gbL = (int) Math.floor((double) limit / GB);
mbL = (gbL > 0) ? 1024 : (int) Math.floor((double) limit / MB);
kbL = (mbL > 0) ? 1024 : (int) Math.floor((double) limit / KB);
bL = (kbL > 0) ? 1024 : (int) Math.floor((double) limit / 4);
if (gbL != 1) while (gbL % 4 != 0) ++gbL;
if (mbL != 1) while (mbL % 4 != 0) ++mbL;
if (kbL != 1) while (kbL % 4 != 0) ++kbL;
while (bL % 4 != 0) ++bL;
if (gbL == 0) gbL = 1;
if (mbL == 0) mbL = 1;
if (kbL == 0) kbL = 1;
if (bL == 0) bL = 1;
blocks = new byte[gbL][][][];
blocks[0] = new byte[mbL][][];
blocks[0][0] = new byte[kbL][];
blocks[0][0][0] = new byte[bL];
}
/**
* Write data to the stream
*
* @param b Data
* @throws IOException
*/
@Override
public void write(int b) throws IOException {
blocks[gb][mb][kb][this.b++] = (byte) b;
++stored;
if (this.b >= 1024) {
this.b = 0;
++kb;
if (kb >= 1024) {
kb = 0;
++mb;
if (mb >= 1024) {
mb = 0;
++gb;
if (gb >= 1024 || stored >= limit) {
flush();
}
blocks[gb] = new byte[gbL][][];
} else if (stored >= limit) {
flush();
}
blocks[gb][mb] = new byte[kbL][];
}
blocks[gb][mb][kb] = new byte[bL];
}
}
/**
* Write control data to the stream
*
* @param b Control Data
* @throws IOException
*/
public void control(int b) throws IOException {
if (Arrays.binarySearch(controls, b) < 0)
throw new IllegalArgumentException("Character " + DebugUtil.toHex(0xFFFF, b) + " is not a control character");
if (this.b != 0 || kb != 0 || mb != 0 || gb != 0) {
flush();
}
queue(() -> {
out.write(b);
out.flush();
});
}
/**
* Flush the stream
*
* @throws IOException
*/
@Override
public void flush() throws IOException {
queue(new BlockFlusher(blocks, stored)::flush);
this.blocks = new byte[gbL][][][];
this.blocks[0] = new byte[mbL][][];
this.blocks[0][0] = new byte[kbL][];
this.blocks[0][0][0] = new byte[bL];
this.b = 0;
this.kb = 0;
this.mb = 0;
this.gb = 0;
this.stored = 0;
this.out.flush();
}
private synchronized void queue(ExceptionRunnable runnable) {
boolean init = false;
if (queue == null) {
queue = new LinkedList<>();
init = true;
}
queue.add(runnable);
if (init) run();
}
private void run() {
if (queue != null) new Thread(() -> {
if (queue.size() > 0) {
try {
ExceptionRunnable next = Util.getDespiteException(() -> queue.get(0), null);
Util.isException(() -> queue.remove(0));
if (next != null) {
next.run();
if (queue != null && queue.size() > 0) run();
else queue = null;
}
} catch (Throwable e) {
Util.isException(() -> queue.remove(0));
if (!(e instanceof SocketException)) { // Cut the write session short when socket issues occur
DebugUtil.logException(e, log);
if (queue != null && queue.size() > 0) run();
else queue = null;
} else queue = null;
}
} else queue = null;
}, "SubDataServer::BlockWriter(" + hashCode() + ')').start();
}
private final class BlockFlusher {
private final byte[][][][] blocks;
private long stored;
private int gb, mb, kb, b;
private BlockFlusher(byte[][][][] blocks, long stored) {
this.blocks = blocks;
this.stored = stored;
}
private void flush() throws IOException {
while (stored > 0) {
if (stored >= GBB) {
stored -= flushGB();
} else if (stored >= MBB) {
stored -= flushMB();
} else if (stored >= KBB) {
stored -= flushKB();
} else if (stored >= BB) {
stored -= flushRaw();
} else {
stored -= flushEscaped();
}
}
}
private long flushGB() throws IOException {
int size = (int) Math.floor((double) stored / GBB);
if (size > 256) size = 256;
if (size != 0) {
out.write('\u0014');
out.write((byte) (size - 1));
int limit = (size * 4) + gb;
for (; gb < limit; ++gb) {
if (blocks[gb] == null) break;
for (byte[][] mb : blocks[gb]) {
if (mb == null) break;
for (byte[] kb : mb) {
if (kb == null) break;
out.write(kb);
}
}
}
return size * GBB;
} else return 0;
}
private long flushMB() throws IOException {
if (blocks[gb] != null) {
int size = (int) Math.floor((double) stored / MBB);
if (size > 256) size = 256;
if (size != 0) {
out.write('\u0013');
out.write((byte) (size - 1));
int limit = (size * 4) + mb;
for (; mb < limit; ++mb) {
if (blocks[gb][mb] == null) break;
for (byte[] kb : blocks[gb][mb]) {
if (kb == null) break;
out.write(kb);
}
}
if (mb >= 1024) {
mb = 0;
++gb;
}
return size * MBB;
} else return 0;
} else return 0;
}
private long flushKB() throws IOException {
if (blocks[gb] != null && blocks[gb][mb] != null) {
int size = (int) Math.floor((double) stored / KBB);
if (size > 256) size = 256;
if (size != 0) {
out.write('\u0012');
out.write((byte) (size - 1));
int limit = (size * 4) + kb;
for (; kb < limit; ++kb) {
if (blocks[gb][mb][kb] == null) break;
out.write(blocks[gb][mb][kb]);
}
if (kb >= 1024) {
kb = 0;
++mb;
if (mb >= 1024) {
mb = 0;
++gb;
}
}
return size * KBB;
} else return 0;
} else return 0;
}
private long flushRaw() throws IOException {
if (blocks[gb] != null && blocks[gb][mb] != null && blocks[gb][mb][kb] != null) {
int size = (int) Math.floor((double) stored / BB);
if (size > 256) size = 256;
if (size != 0) {
out.write('\u0011');
out.write((byte) (size - 1));
int limit = (size * 4) + b;
for (; b < limit; ++b) {
out.write(blocks[gb][mb][kb][b]);
}
if (b >= 1024) {
b = 0;
++kb;
if (kb >= 1024) {
kb = 0;
++mb;
if (mb >= 1024) {
mb = 0;
++gb;
}
}
}
return size * BB;
} else return 0;
} else return 0;
}
private long flushEscaped() throws IOException {
if (blocks[gb] != null && blocks[gb][mb] != null && blocks[gb][mb][kb] != null) {
int size = (int) (stored % BB);
if (size != 0) {
out.write('\u0010');
int i = size + b;
for (; b < i; ++b) {
escaped.write(blocks[gb][mb][kb][b]);
}
escaped.control('\u0017');
if (b >= 1024) {
b = 0;
++kb;
if (kb >= 1024) {
kb = 0;
++mb;
if (mb >= 1024) {
mb = 0;
++gb;
}
}
}
return size;
} else return 0;
} else return 0;
}
}
/**
* Close the stream
*
* @throws IOException
*/
@Override
public void close() throws IOException {
out.close();
}
}