From 617ad86c825bf7fdc85b72c3a453d9c90bd8f770 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sat, 27 Apr 2019 23:29:24 +0200 Subject: [PATCH] steams: bug fixes + more tests --- .../utils/streams/BufferedOutputStream.java | 18 +++--- .../test_code/BufferedOutputStreamTest.java | 56 +++++++++++++++++++ 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/src/be/nikiroo/utils/streams/BufferedOutputStream.java b/src/be/nikiroo/utils/streams/BufferedOutputStream.java index 8b74ae16..5f7e6ebc 100644 --- a/src/be/nikiroo/utils/streams/BufferedOutputStream.java +++ b/src/be/nikiroo/utils/streams/BufferedOutputStream.java @@ -37,6 +37,7 @@ public class BufferedOutputStream extends OutputStream { private boolean closed; private int openCounter; + private byte[] b1; /** * Create a new {@link BufferedInputStream} that wraps the given @@ -49,19 +50,15 @@ public class BufferedOutputStream extends OutputStream { this.out = out; this.buffer = new byte[4096]; + this.b1 = new byte[1]; this.start = 0; this.stop = 0; } @Override public void write(int b) throws IOException { - checkClose(); - - if (available() <= 0) { - flush(false); - } - - buffer[start++] = (byte) b; + b1[0] = (byte) b; + write(b1, 0, 1); } @Override @@ -86,7 +83,7 @@ public class BufferedOutputStream extends OutputStream { return; } - if (sourceLength >= buffer.length) { + if (bypassFlush && sourceLength >= buffer.length) { /* * If the request length exceeds the size of the output buffer, * flush the output buffer and then write the data directly. In this @@ -94,6 +91,7 @@ public class BufferedOutputStream extends OutputStream { */ flush(false); out.write(source, sourceOffset, sourceLength); + bytesWritten += (sourceLength - sourceOffset); return; } @@ -103,7 +101,7 @@ public class BufferedOutputStream extends OutputStream { flush(false); } - int now = Math.min(sourceLength, available()); + int now = Math.min(sourceLength - done, available()); System.arraycopy(source, sourceOffset + done, buffer, stop, now); stop += now; done += now; @@ -245,7 +243,7 @@ public class BufferedOutputStream extends OutputStream { openCounter--; } else { closed = true; - flush(true); + flush(includingSubStream); if (includingSubStream && out != null) { out.close(); } diff --git a/src/be/nikiroo/utils/test_code/BufferedOutputStreamTest.java b/src/be/nikiroo/utils/test_code/BufferedOutputStreamTest.java index cf6eb2ad..5646e618 100644 --- a/src/be/nikiroo/utils/test_code/BufferedOutputStreamTest.java +++ b/src/be/nikiroo/utils/test_code/BufferedOutputStreamTest.java @@ -1,6 +1,8 @@ package be.nikiroo.utils.test_code; import java.io.ByteArrayOutputStream; +import java.util.ArrayList; +import java.util.List; import be.nikiroo.utils.streams.BufferedOutputStream; import be.nikiroo.utils.test.TestCase; @@ -25,6 +27,24 @@ class BufferedOutputStreamTest extends TestLauncher { } }); + addTest(new TestCase("Single write of 5000 bytes") { + @Override + public void test() throws Exception { + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + BufferedOutputStream out = new BufferedOutputStream(bout); + + byte[] data = new byte[5000]; + for (int i = 0; i < data.length; i++) { + data[i] = (byte) (i % 255); + } + + out.write(data); + out.close(); + + checkArrays(this, "FIRST", bout, data); + } + }); + addTest(new TestCase("Multiple writes") { @Override public void test() throws Exception { @@ -45,6 +65,42 @@ class BufferedOutputStreamTest extends TestLauncher { checkArrays(this, "FIRST", bout, dataAll); } }); + + addTest(new TestCase("Multiple writes for a 5000 bytes total") { + @Override + public void test() throws Exception { + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + BufferedOutputStream out = new BufferedOutputStream(bout); + + byte[] data = new byte[] { 42, 12, 0, 127, 51, 2, 32, 66, 7, 87 }; + + List bytes = new ArrayList(); + + // write 400 * 10 + 1000 bytes = 5000 + for (int i = 0; i < 400; i++) { + for (int j = 0; j < data.length; j++) { + bytes.add(data[j]); + } + out.write(data); + } + + for (int i = 0; i < 1000; i++) { + for (int j = 0; j < data.length; j++) { + bytes.add(data[j]); + } + out.write(data); + } + + out.close(); + + byte[] abytes = new byte[bytes.size()]; + for (int i = 0; i < bytes.size(); i++) { + abytes[i] = bytes.get(i); + } + + checkArrays(this, "FIRST", bout, abytes); + } + }); } static void checkArrays(TestCase test, String prefix, -- 2.27.0