51 lines
1.2 KiB
Java
51 lines
1.2 KiB
Java
package tech.nevets.tvpn;
|
|
|
|
import javax.swing.*;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.OutputStream;
|
|
|
|
public class LogOutputStream extends OutputStream {
|
|
private JTextArea textArea;
|
|
private FileOutputStream fileOut;
|
|
|
|
private final char[] buf = new char[4096];
|
|
private int pointer = 0;
|
|
|
|
public LogOutputStream() {
|
|
try {
|
|
this.fileOut = new FileOutputStream(Utils.APP_DIR + "/log.txt");
|
|
} catch (IOException e) {
|
|
System.err.println("Error Creating log stream: " + e.getMessage());
|
|
this.fileOut = null;
|
|
}
|
|
}
|
|
|
|
public void initLogTab(JTextArea textArea) {
|
|
this.textArea = textArea;
|
|
}
|
|
|
|
@Override
|
|
public void write(int b) throws IOException {
|
|
fileOut.write(b);
|
|
if ((char) b == '\n') {
|
|
flush();
|
|
pointer = 0;
|
|
return;
|
|
}
|
|
buf[pointer] = (char) b;
|
|
pointer++;
|
|
}
|
|
|
|
@Override
|
|
public void flush() {
|
|
StringBuilder sb = new StringBuilder(pointer + 1);
|
|
for (int i = 0; i < pointer; i++) {
|
|
sb.append(buf[i]);
|
|
}
|
|
if (textArea != null) {
|
|
textArea.append(sb.toString());
|
|
}
|
|
}
|
|
}
|