diff --git a/src/main/java/io/usethesource/vallang/io/binary/util/ByteBufferOutputStream.java b/src/main/java/io/usethesource/vallang/io/binary/util/ByteBufferOutputStream.java index 50c3d93a..bf088965 100644 --- a/src/main/java/io/usethesource/vallang/io/binary/util/ByteBufferOutputStream.java +++ b/src/main/java/io/usethesource/vallang/io/binary/util/ByteBufferOutputStream.java @@ -31,10 +31,20 @@ public void flush() throws IOException { if (closed) { throw new IOException("Stream closed"); } + target.flip(); if (target.hasRemaining()) { target = flush(target); + if (!target.hasRemaining()) { + throw new IOException("flush implementation didn't correctly provide a new buffer to write to: " + target); + } + } + else { + // the buffer was empty, so flush was called on an already flushed stream + // so we'll reset the buffer to make sure we don't continue with empty buffer + target.clear(); } + assert target.hasRemaining(): "after a flush, we should have a buffer with some room. (it was: " + target + ")"; } @Override diff --git a/src/main/java/io/usethesource/vallang/io/binary/wire/binary/BinaryWireOutputStream.java b/src/main/java/io/usethesource/vallang/io/binary/wire/binary/BinaryWireOutputStream.java index 1821ae79..b004531c 100644 --- a/src/main/java/io/usethesource/vallang/io/binary/wire/binary/BinaryWireOutputStream.java +++ b/src/main/java/io/usethesource/vallang/io/binary/wire/binary/BinaryWireOutputStream.java @@ -36,7 +36,6 @@ public BinaryWireOutputStream(OutputStream stream, int stringSharingWindowSize) this(stream, stringSharingWindowSize, 8*1024); } public BinaryWireOutputStream(OutputStream stream, int stringSharingWindowSize, int bufferSize) throws IOException { - assert stringSharingWindowSize > 0; if (stream instanceof BufferedOutputStream || stream instanceof ByteBufferOutputStream) { __stream = stream; }