|
| 1 | +package com.fasterxml.jackson.dataformat.cbor; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +import java.io.ByteArrayInputStream; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.SequenceInputStream; |
| 10 | + |
| 11 | +public class ParserInputStreamTest extends CBORTestBase { |
| 12 | + |
| 13 | + @Test |
| 14 | + public void testInpuStream() throws Exception { |
| 15 | + CBORFactory f = new CBORFactory(); |
| 16 | + ObjectMapper cborMapper = new ObjectMapper(new CBORFactory()); |
| 17 | + byte[] buffer = generateHugeCBOR(f); |
| 18 | + |
| 19 | + // split the buffer in two smaller buffer |
| 20 | + int len = 160; |
| 21 | + byte[] buf1 = new byte[len]; |
| 22 | + byte[] buf2 = new byte[buffer.length - len]; |
| 23 | + System.arraycopy(buffer, 0, buf1, 0, len); |
| 24 | + System.arraycopy(buffer, len, buf2, 0, buffer.length - len); |
| 25 | + |
| 26 | + // aggregate the two buffers via a SequenceInputStream |
| 27 | + ByteArrayInputStream in1 = new ByteArrayInputStream(buf1); |
| 28 | + ByteArrayInputStream in2 = new ByteArrayInputStream(buf2); |
| 29 | + SequenceInputStream inputStream = new SequenceInputStream(in1, in2); |
| 30 | + |
| 31 | + JsonNode jsonNode = cborMapper.readTree(inputStream); |
| 32 | + assertNotNull(jsonNode); |
| 33 | + } |
| 34 | + |
| 35 | + private byte[] generateHugeCBOR(CBORFactory f) throws IOException { |
| 36 | + String hugeJson = "{"; |
| 37 | + for (char c='a'; c <= 'z'; c++) { |
| 38 | + for (char cc='a'; cc <= 'z'; cc++) { |
| 39 | + hugeJson += "\"" + c + cc + "\":0,"; |
| 40 | + } |
| 41 | + for (int i = 0; i < 50; i++) { |
| 42 | + hugeJson += "\"" + c + i + "\":" + i + ","; |
| 43 | + } |
| 44 | + } |
| 45 | + hugeJson += "\"name\":123"; |
| 46 | + hugeJson += "}"; |
| 47 | + return cborDoc(f, hugeJson); |
| 48 | + } |
| 49 | +} |
0 commit comments