/
LogStreamWriter.java
57 lines (49 loc) · 1.55 KB
/
LogStreamWriter.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
package net.ME1312.Galaxi.Library.Log;
import net.ME1312.Galaxi.Library.Container;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
/**
* Log Stream Writer Class
*/
public class LogStreamWriter {
private Container<OutputStream[]> out;
private StringBuilder buffer = new StringBuilder();
LogStreamWriter(Container<OutputStream[]> stream) {
this.out = stream;
}
private void publish() throws IOException {
String str = buffer.toString();
buffer.append('\n');
byte[] bytes = buffer.toString().getBytes(StandardCharsets.UTF_8);
buffer = new StringBuilder();
if (out.get() != null) for (OutputStream out : this.out.get()) {
if (out instanceof PrintStream) {
((PrintStream) out).println(str);
} else {
out.write(bytes);
}
out.flush();
}
}
public void write(String... str) throws IOException {
for (String s : str) {
if (s.contains("\n")) {
do {
int i = s.indexOf("\n");
buffer.append(s, 0, i);
publish();
s = s.substring(i + 1);
} while (s.contains("\n"));
if (s.length() > 0)
buffer.append(s);
} else {
buffer.append(s);
}
}
}
public void write(char... c) throws IOException {
write(new String(c));
}
}