Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flush multiple lines together #204

Merged
merged 5 commits into from
Sep 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
public class ConsoleBufferedOutputStream extends OutputStream {

/** The maximum length of the line buffer. */
static final int MAX_BUFFER_LENGTH = 1024;
static final int DEFAULT_MAX_BUFFER_LENGTH = 4096;
private final int maxBufferLength;

private final Timer timer;

Expand All @@ -63,7 +64,23 @@ public class ConsoleBufferedOutputStream extends OutputStream {
* The style type to be used for the output
*/
public ConsoleBufferedOutputStream(final JTextPane textPane, final OutputStyle style) {
this.lineBuffer = new StringBuilder(MAX_BUFFER_LENGTH * 2);
this(textPane, style, DEFAULT_MAX_BUFFER_LENGTH);
}

/**
* Constructor with maxBufferLenght included
*
* @param textPane
* The text pane to place the stream data into
* @param style
* The style type to be used for the output
* @param maxBufferLength
* the maximum length of the text buffer before it is flushed. The buffer is also flushed every 32ms. Choosing a
* small value may result in one output stream being cut of by the messages from another.
*/
public ConsoleBufferedOutputStream(final JTextPane textPane, final OutputStyle style, int maxBufferLength) {
this.maxBufferLength = maxBufferLength;
this.lineBuffer = new StringBuilder(maxBufferLength * 2);

this.textPane = textPane;
this.style = this.textPane.addStyle(style.toString(), null);
Expand All @@ -78,7 +95,7 @@ public ConsoleBufferedOutputStream(final JTextPane textPane, final OutputStyle s
throw new UnsupportedOperationException("With stye type " + style.toString());
}

this.timer = new Timer(500, (event) -> {
this.timer = new Timer(32, (event) -> {
try {
// flush the line buffer in regular intervalls
this.flushLineBufferToTextPane();
Expand All @@ -105,13 +122,12 @@ public void close() throws IOException {
@Override
public void write(final int character) throws IOException {
final char symbol = (char) character;
final boolean newline = symbol == '\n'; // should catch CR/LF and LF line endings

synchronized (this.lineBuffer) {
this.lineBuffer.append(symbol);
}

if (newline || this.lineBuffer.length() >= MAX_BUFFER_LENGTH) {
if (this.lineBuffer.length() >= this.maxBufferLength) {
this.flushLineBufferToTextPane();
}
}
Expand Down Expand Up @@ -142,4 +158,14 @@ private void flushLineBufferToTextPane() throws IOException {
});
}
}

/**
* Get's {@link #maxBufferLength maxBufferLength}
*
* @return maxBufferLength
*/
public int getMaxBufferLength() {
return this.maxBufferLength;
}

}
Loading