Debian Codegraph @

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

raw
1 package com.fasterxml.jackson.dataformat.smile;
2
3 import java.io.*;
4
5 import org.junit.Assert;
6
7 import com.fasterxml.jackson.core.*;
8 import com.fasterxml.jackson.dataformat.smile.SmileGenerator.Feature;
9
10 public class TestSmileGeneratorBinary extends SmileTestBase
11 {
12 static class ThrottledInputStream extends FilterInputStream
13 {
14 protected final int _maxBytes;
15
16 public ThrottledInputStream(byte[] data, int maxBytes)
17 {
18 this(new ByteArrayInputStream(data), maxBytes);
19 }
20
21 public ThrottledInputStream(InputStream in, int maxBytes)
22 {
23 super(in);
24 _maxBytes = maxBytes;
25 }
26
27 @Override
28 public int read(byte[] buf) throws IOException {
29 return read(buf, 0, buf.length);
30 }
31
32 @Override
33 public int read(byte[] buf, int offset, int len) throws IOException {
34 return in.read(buf, offset, Math.min(_maxBytes, len));
35 }
36
37 }
38
39 /*
40 /**********************************************************
41 /* Test methods
42 /**********************************************************
43 */
44
45 public void testStreamingBinary() throws Exception
46 {
47 _testStreamingBinary(true);
48 _testStreamingBinary(false);
49 }
50
51 public void testBinaryWithoutLength() throws Exception
52 {
53 final SmileFactory f = new SmileFactory();
54 JsonGenerator jg = f.createGenerator(new ByteArrayOutputStream());
55 try {
56 jg.writeBinary(new ByteArrayInputStream(new byte[1]), -1);
57 fail("Should have failed");
58 } catch (UnsupportedOperationException e) {
59 verifyException(e, "must pass actual length");
60 }
61 jg.close();
62 }
63
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
95 /*
96 /**********************************************************
97 /* Helper methods
98 /**********************************************************
99 */
100
101 private final static String TEXT = "Some content so that we can test encoding of base64 data; must"
102 +" be long enough include a line wrap or two...";
103 private final static String TEXT4 = TEXT + TEXT + TEXT + TEXT;
104
105 private void _testStreamingBinary(boolean rawBinary) throws Exception
106 {
107 final SmileFactory f = new SmileFactory();
108 f.configure(SmileGenerator.Feature.ENCODE_BINARY_AS_7BIT, !rawBinary);
109
110 final byte[] INPUT = TEXT4.getBytes("UTF-8");
111 for (int chunkSize : new int[] { 1, 2, 3, 4, 7, 11, 29, 5000 }) {
112 JsonGenerator jgen;
113
114 final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
115 jgen = f.createGenerator(bytes);
116 jgen.writeStartArray();
117 InputStream data = new ThrottledInputStream(INPUT, chunkSize);
118 jgen.writeBinary(data, INPUT.length);
119 jgen.writeEndArray();
120 jgen.close();
121 jgen.close();
122 data.close();
123
124 JsonParser jp = f.createParser(bytes.toByteArray());
125 assertToken(JsonToken.START_ARRAY, jp.nextToken());
126 assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, jp.nextToken());
127 byte[] b = jp.getBinaryValue();
128 Assert.assertArrayEquals(INPUT, b);
129 assertToken(JsonToken.END_ARRAY, jp.nextToken());
130 jp.close();
131 }
132 }
133 }