Skip to content

Commit 1dff2e1

Browse files
committed
Merge pull request #25 from lseeker/master
Patches over read when use raw binary streaming write.
2 parents 2411476 + d531575 commit 1dff2e1

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/main/java/com/fasterxml/jackson/dataformat/smile/SmileGenerator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,6 +1943,9 @@ private final int _writeBytes(InputStream in, int bytesLeft) throws IOException
19431943
_flushBuffer();
19441944
room = _outputEnd - _outputTail;
19451945
}
1946+
if (room > bytesLeft) {
1947+
room = bytesLeft;
1948+
}
19461949
int count = in.read(_outputBuffer, _outputTail, room);
19471950
if (count < 0) {
19481951
break;

src/test/java/com/fasterxml/jackson/dataformat/smile/TestSmileGeneratorBinary.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.junit.Assert;
66

77
import com.fasterxml.jackson.core.*;
8+
import com.fasterxml.jackson.dataformat.smile.SmileGenerator.Feature;
89

910
public class TestSmileGeneratorBinary extends SmileTestBase
1011
{
@@ -60,6 +61,37 @@ public void testBinaryWithoutLength() throws Exception
6061
jg.close();
6162
}
6263

64+
public void testStreamingBinaryPartly() throws Exception {
65+
_testStreamingBinaryPartly(true);
66+
_testStreamingBinaryPartly(false);
67+
}
68+
69+
private void _testStreamingBinaryPartly(boolean rawBinary) throws Exception
70+
{
71+
final SmileFactory f = new SmileFactory();
72+
f.configure(Feature.ENCODE_BINARY_AS_7BIT, rawBinary);
73+
74+
final byte[] INPUT = TEXT4.getBytes("UTF-8");
75+
ByteArrayInputStream in = new ByteArrayInputStream(INPUT);
76+
77+
ByteArrayOutputStream out = new ByteArrayOutputStream();
78+
JsonGenerator jg = f.createGenerator(out);
79+
jg.writeStartArray();
80+
jg.writeBinary(in, 1);
81+
jg.writeEndArray();
82+
jg.close();
83+
in.close();
84+
85+
JsonParser jp = f.createParser(out.toByteArray());
86+
assertToken(JsonToken.START_ARRAY, jp.nextToken());
87+
assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, jp.nextToken());
88+
byte[] b = jp.getBinaryValue();
89+
assertToken(JsonToken.END_ARRAY, jp.nextToken());
90+
jp.close();
91+
92+
assertEquals(1, b.length);
93+
}
94+
6395
/*
6496
/**********************************************************
6597
/* Helper methods

0 commit comments

Comments
 (0)