From d53157557d8bbbf9761143795e6dbe834f7b8b90 Mon Sep 17 00:00:00 2001 From: Yun-young LEE Date: Thu, 16 Jul 2015 07:44:47 +0900 Subject: [PATCH] Patches over read when use raw binary streaming write. Length check before fill to buffer to ensure read exactly specified length. --- .../dataformat/smile/SmileGenerator.java | 3 ++ .../smile/TestSmileGeneratorBinary.java | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileGenerator.java b/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileGenerator.java index d9638f9..4167015 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileGenerator.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileGenerator.java @@ -1943,6 +1943,9 @@ private final int _writeBytes(InputStream in, int bytesLeft) throws IOException _flushBuffer(); room = _outputEnd - _outputTail; } + if (room > bytesLeft) { + room = bytesLeft; + } int count = in.read(_outputBuffer, _outputTail, room); if (count < 0) { break; diff --git a/src/test/java/com/fasterxml/jackson/dataformat/smile/TestSmileGeneratorBinary.java b/src/test/java/com/fasterxml/jackson/dataformat/smile/TestSmileGeneratorBinary.java index 905b6b0..660e0e4 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/smile/TestSmileGeneratorBinary.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/smile/TestSmileGeneratorBinary.java @@ -5,6 +5,7 @@ import org.junit.Assert; import com.fasterxml.jackson.core.*; +import com.fasterxml.jackson.dataformat.smile.SmileGenerator.Feature; public class TestSmileGeneratorBinary extends SmileTestBase { @@ -60,6 +61,37 @@ public void testBinaryWithoutLength() throws Exception jg.close(); } + public void testStreamingBinaryPartly() throws Exception { + _testStreamingBinaryPartly(true); + _testStreamingBinaryPartly(false); + } + + private void _testStreamingBinaryPartly(boolean rawBinary) throws Exception + { + final SmileFactory f = new SmileFactory(); + f.configure(Feature.ENCODE_BINARY_AS_7BIT, rawBinary); + + final byte[] INPUT = TEXT4.getBytes("UTF-8"); + ByteArrayInputStream in = new ByteArrayInputStream(INPUT); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + JsonGenerator jg = f.createGenerator(out); + jg.writeStartArray(); + jg.writeBinary(in, 1); + jg.writeEndArray(); + jg.close(); + in.close(); + + JsonParser jp = f.createParser(out.toByteArray()); + assertToken(JsonToken.START_ARRAY, jp.nextToken()); + assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, jp.nextToken()); + byte[] b = jp.getBinaryValue(); + assertToken(JsonToken.END_ARRAY, jp.nextToken()); + jp.close(); + + assertEquals(1, b.length); + } + /* /********************************************************** /* Helper methods