Search in sources :

Example 1 with ThrottledInputStream

use of com.fasterxml.jackson.dataformat.smile.testutil.ThrottledInputStream in project jackson-dataformats-binary by FasterXML.

the class BasicParserTest method _testSimple.

@SuppressWarnings("resource")
private void _testSimple(boolean throttle, byte[] data) throws IOException {
    InputStream in = new ByteArrayInputStream(data);
    if (throttle) {
        in = new ThrottledInputStream(in, 1);
    }
    JsonParser p = _smileParser(in, true);
    assertNull(p.currentToken());
    assertNull(p.currentName());
    assertToken(JsonToken.START_ARRAY, p.nextToken());
    assertNull(p.currentName());
    assertToken(JsonToken.VALUE_TRUE, p.nextToken());
    assertNull(p.currentName());
    assertToken(JsonToken.VALUE_NULL, p.nextToken());
    assertNull(p.currentName());
    assertToken(JsonToken.VALUE_FALSE, p.nextToken());
    assertNull(p.currentName());
    assertToken(JsonToken.END_ARRAY, p.nextToken());
    assertNull(p.currentName());
    assertNull(p.nextToken());
    p.close();
}
Also used : ThrottledInputStream(com.fasterxml.jackson.dataformat.smile.testutil.ThrottledInputStream) ThrottledInputStream(com.fasterxml.jackson.dataformat.smile.testutil.ThrottledInputStream)

Example 2 with ThrottledInputStream

use of com.fasterxml.jackson.dataformat.smile.testutil.ThrottledInputStream in project jackson-dataformats-binary by FasterXML.

the class GeneratorBinaryTest method _testStreamingBinaryPartly.

private void _testStreamingBinaryPartly(boolean rawBinary, boolean throttle) throws Exception {
    final SmileFactory f = SmileFactory.builder().configure(Feature.ENCODE_BINARY_AS_7BIT, rawBinary).build();
    final byte[] INPUT = TEXT4.getBytes("UTF-8");
    InputStream in;
    if (throttle) {
        in = new ThrottledInputStream(INPUT, 3);
    } else {
        in = new ByteArrayInputStream(INPUT);
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JsonGenerator g = f.createGenerator(ObjectWriteContext.empty(), out);
    g.writeStartArray();
    g.writeBinary(in, 1);
    g.writeEndArray();
    g.close();
    in.close();
    JsonParser p = _smileParser(out.toByteArray());
    assertToken(JsonToken.START_ARRAY, p.nextToken());
    assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, p.nextToken());
    byte[] b = p.getBinaryValue();
    assertToken(JsonToken.END_ARRAY, p.nextToken());
    p.close();
    assertEquals(1, b.length);
}
Also used : SmileFactory(com.fasterxml.jackson.dataformat.smile.SmileFactory) ThrottledInputStream(com.fasterxml.jackson.dataformat.smile.testutil.ThrottledInputStream) ThrottledInputStream(com.fasterxml.jackson.dataformat.smile.testutil.ThrottledInputStream)

Example 3 with ThrottledInputStream

use of com.fasterxml.jackson.dataformat.smile.testutil.ThrottledInputStream in project jackson-dataformats-binary by FasterXML.

the class BinaryReadTest method _testBinary.

private void _testBinary(ObjectMapper mapper, int size) throws Exception {
    byte[] input = _bytes(size, 0);
    // First, read/write as individual value
    byte[] raw = mapper.writeValueAsBytes(input);
    byte[] b2 = mapper.readValue(raw, byte[].class);
    assertNotNull(b2);
    Assert.assertArrayEquals(input, b2);
    // then as POJO member
    raw = mapper.writeValueAsBytes(new Bytes(input));
    Bytes bytes = mapper.readValue(raw, Bytes.class);
    assertNotNull(bytes);
    assertNotNull(bytes.bytes);
    Assert.assertArrayEquals(input, bytes.bytes);
    // then using incremental access method
    raw = mapper.writeValueAsBytes(input);
    InputStream in = new ThrottledInputStream(raw, 3);
    JsonParser p = mapper.createParser(in);
    assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, p.nextToken());
    ByteArrayOutputStream bout = new ByteArrayOutputStream(input.length / 3);
    assertEquals(input.length, p.readBinaryValue(bout));
    assertEquals(input.length, bout.size());
    b2 = bout.toByteArray();
    Assert.assertArrayEquals(input, b2);
    assertNull(p.nextToken());
    p.close();
    in.close();
    // and finally streaming but skipping
    in = new ThrottledInputStream(raw, 3);
    p = mapper.createParser(in);
    assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, p.nextToken());
    assertNull(p.nextToken());
    p.close();
    in.close();
    // And once more! Read as tree
    JsonNode n = mapper.readTree(mapper.writeValueAsBytes(new Bytes(input)));
    assertTrue(n.isObject());
    assertEquals(1, n.size());
    n = n.get("bytes");
    assertNotNull(n);
    assertTrue(n.isBinary());
    BinaryNode bn = (BinaryNode) n;
    Assert.assertArrayEquals(input, bn.binaryValue());
    _testBinaryInArray(mapper, size);
}
Also used : ThrottledInputStream(com.fasterxml.jackson.dataformat.smile.testutil.ThrottledInputStream) BinaryNode(com.fasterxml.jackson.databind.node.BinaryNode) ThrottledInputStream(com.fasterxml.jackson.dataformat.smile.testutil.ThrottledInputStream) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 4 with ThrottledInputStream

use of com.fasterxml.jackson.dataformat.smile.testutil.ThrottledInputStream in project jackson-dataformats-binary by FasterXML.

the class GeneratorBinaryTest method _testStreamingBinary.

private void _testStreamingBinary(boolean rawBinary, boolean throttle) throws Exception {
    final SmileFactory f = SmileFactory.builder().configure(Feature.ENCODE_BINARY_AS_7BIT, !rawBinary).build();
    final byte[] INPUT = TEXT4.getBytes("UTF-8");
    for (int chunkSize : new int[] { 1, 2, 3, 4, 7, 11, 29, 5000 }) {
        JsonGenerator gen;
        final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        gen = f.createGenerator(ObjectWriteContext.empty(), bytes);
        gen.writeStartArray();
        InputStream data = new ThrottledInputStream(INPUT, chunkSize);
        gen.writeBinary(data, INPUT.length);
        gen.writeEndArray();
        gen.close();
        gen.close();
        data.close();
        final byte[] b2 = bytes.toByteArray();
        InputStream in;
        if (throttle) {
            in = new ThrottledInputStream(b2, 3);
        } else {
            in = new ByteArrayInputStream(b2);
        }
        JsonParser p = _smileParser(in);
        assertToken(JsonToken.START_ARRAY, p.nextToken());
        assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, p.nextToken());
        byte[] b = p.getBinaryValue();
        Assert.assertArrayEquals(INPUT, b);
        assertToken(JsonToken.END_ARRAY, p.nextToken());
        p.close();
    }
}
Also used : SmileFactory(com.fasterxml.jackson.dataformat.smile.SmileFactory) ThrottledInputStream(com.fasterxml.jackson.dataformat.smile.testutil.ThrottledInputStream) ThrottledInputStream(com.fasterxml.jackson.dataformat.smile.testutil.ThrottledInputStream)

Example 5 with ThrottledInputStream

use of com.fasterxml.jackson.dataformat.smile.testutil.ThrottledInputStream in project jackson-dataformats-binary by FasterXML.

the class BinaryReadTest method _testBinaryInArray.

private void _testBinaryInArray(ObjectMapper mapper, int size) throws Exception {
    byte[] b1 = _bytes(size, 1);
    byte[] b2 = _bytes(size, 7);
    byte[] doc = mapper.writeValueAsBytes(new ByteArrays(b1, b2));
    @SuppressWarnings("resource") ByteArrays result = mapper.readValue(new ThrottledInputStream(doc, 5), ByteArrays.class);
    assertNotNull(result.arrays);
    assertEquals(2, result.arrays.size());
    Assert.assertArrayEquals(b1, result.arrays.get(0));
    Assert.assertArrayEquals(b2, result.arrays.get(1));
    // and once more, now as JsonNode
    JsonNode n = mapper.readTree(doc);
    assertTrue(n.isObject());
    JsonNode n2 = n.get("arrays");
    assertTrue(n2.isArray());
    assertEquals(2, n2.size());
    JsonNode bin = n2.get(0);
    assertTrue(bin.isBinary());
    Assert.assertArrayEquals(b1, ((BinaryNode) bin).binaryValue());
    bin = n2.get(1);
    assertTrue(bin.isBinary());
    Assert.assertArrayEquals(b2, ((BinaryNode) bin).binaryValue());
}
Also used : ThrottledInputStream(com.fasterxml.jackson.dataformat.smile.testutil.ThrottledInputStream) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

ThrottledInputStream (com.fasterxml.jackson.dataformat.smile.testutil.ThrottledInputStream)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 SmileFactory (com.fasterxml.jackson.dataformat.smile.SmileFactory)2 JsonParser (com.fasterxml.jackson.core.JsonParser)1 StreamReadException (com.fasterxml.jackson.core.exc.StreamReadException)1 BinaryNode (com.fasterxml.jackson.databind.node.BinaryNode)1