/
OutputStreamL1.java
161 lines (148 loc) · 5.02 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
package net.ME1312.SubData.Server.Library;
import java.io.IOException;
import java.io.OutputStream;
import java.net.SocketException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Logger;
import static net.ME1312.SubData.Server.Library.DataSize.*;
/**
* SubData Layer 1 OutputStream Class
*/
public class OutputStreamL1 extends OutputStream {
private final ExecutorService writer;
private final Logger log;
private final Runnable shutdown;
private final OutputStream out;
private byte[] block;
private int cursor = 0;
public int limit;
public OutputStreamL1(Logger log, OutputStream stream, int limit, Runnable shutdown, String name) {
this.writer = Executors.newSingleThreadExecutor(r -> new Thread(r, name));
this.shutdown = shutdown;
this.log = log;
this.out = stream;
this.limit = limit;
this.block = new byte[limit];
}
public void resize(int limit) {
if (this.limit != limit) {
this.limit = limit;
if (cursor >= limit) {
flush();
} else {
final byte[] block = this.block;
this.block = new byte[limit];
System.arraycopy(block, 0, this.block, 0, cursor);
}
}
}
@Override
public void write(byte[] data, int offset, int length) {
int transferred;
for (;;) {
System.arraycopy(data, offset, block, cursor, transferred = Math.min(length, block.length - cursor));
cursor += transferred;
if (transferred == length) {
if (cursor == block.length) flush();
return;
}
flush();
offset += transferred;
length -= transferred;
}
}
@Override
public void write(int b) {
block[cursor++] = (byte) b;
if (cursor == block.length) flush();
}
@Override
public void flush() {
if (cursor != 0 && !writer.isShutdown()) {
if (cursor == block.length || limit != block.length) {
writer.submit(new DataFlusher(block, cursor)::flush);
block = new byte[limit];
} else {
final byte[] block = new byte[cursor];
System.arraycopy(this.block, 0, block, 0, cursor);
writer.submit(new DataFlusher(block, cursor)::flush);
}
cursor = 0;
}
}
public void control(int b) {
if (!writer.isShutdown()) writer.submit(() -> {
try {
out.write(b);
out.flush();
} catch (IOException e) {
if (!(e instanceof SocketException)) {
DebugUtil.logException(e, log);
} else shutdown.run();
}
});
}
private final class DataFlusher {
private final byte[] block;
private int stored, cursor = 0;
private DataFlusher(byte[] data, int length) {
this.stored = length;
this.block = data;
}
private void flush() {
try {
while (stored > 0) {
if (stored >= MBB) {
stored -= flushMBB();
} else if (stored >= KBB) {
stored -= flushKBB();
} else if (stored >= BB) {
stored -= flushBB();
} else {
stored -= flushByte();
}
}
} catch (IOException e) {
if (!(e instanceof SocketException)) {
DebugUtil.logException(e, log);
} else shutdown.run();
}
}
private int flushMBB() throws IOException {
int size = Math.min((int) Math.floor((double) stored / MBB), 256);
int length = size * MBB;
out.write('\u0013');
out.write((byte) (size - 1));
out.write(block, cursor, length);
cursor += length;
return length;
}
private int flushKBB() throws IOException {
int size = Math.min((int) Math.floor((double) stored / KBB), 256);
int length = size * KBB;
out.write('\u0012');
out.write((byte) (size - 1));
out.write(block, cursor, length);
cursor += length;
return length;
}
private int flushBB() throws IOException {
int size = Math.min((int) Math.floor((double) stored / BB), 256);
int length = size * BB;
out.write('\u0011');
out.write((byte) (size - 1));
out.write(block, cursor, length);
cursor += length;
return length;
}
private int flushByte() throws IOException {
out.write('\u0010');
out.write(block[cursor++]);
return 1;
}
}
public void shutdown() {
writer.shutdown();
}
}