Skip to content

Commit

Permalink
Update WriterOutputStream, re #6
Browse files Browse the repository at this point in the history
  • Loading branch information
safris committed Mar 9, 2023
1 parent 7812166 commit 3e5d7e1
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/main/java/org/libj/io/WriterOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public void write(final byte[] b, int off, int len) throws IOException {
}

if (flushImmediately)
flush();
flushBuffer();
}

@Override
Expand All @@ -199,7 +199,7 @@ public void write(final int b) throws IOException {
processInput(false);

if (flushImmediately)
flush();
flushBuffer();
}

/**
Expand All @@ -209,11 +209,7 @@ public void write(final int b) throws IOException {
*/
@Override
public void flush() throws IOException {
if (decoderOut.position() <= 0)
return;

writer.write(decoderOut.array(), 0, decoderOut.position());
decoderOut.rewind();
flushBuffer();
writer.flush();
}

Expand All @@ -226,10 +222,18 @@ public void flush() throws IOException {
@Override
public void close() throws IOException {
processInput(true);
flush();
flushBuffer();
writer.close();
}

protected void flushBuffer() throws IOException {
final int pos = decoderOut.position();
if (pos > 0) {
writer.write(decoderOut.array(), 0, pos);
decoderOut.rewind();
}
}

/**
* Decode the contents of the input ByteBuffer into a CharBuffer.
*
Expand All @@ -241,7 +245,7 @@ private void processInput(final boolean endOfInput) throws IOException {
for (CoderResult coderResult;;) {
coderResult = decoder.decode(decoderIn, decoderOut, endOfInput);
if (coderResult.isOverflow()) {
flush();
flushBuffer();
}
else if (coderResult.isUnderflow()) {
break;
Expand Down

0 comments on commit 3e5d7e1

Please sign in to comment.