co.nstant.in.cbor.CborDecoder Java Examples

The following examples show how to use co.nstant.in.cbor.CborDecoder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Example14Test.java    From cbor-java with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
        new byte[] { (byte) 0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });
    CborDecoder decoder = new CborDecoder(byteArrayInputStream);
    DataItem b = decoder.decodeNext();

    Assert.assertTrue(b.hasTag());
    Tag tag = b.getTag();
    Assert.assertEquals(3, tag.getValue());

    Assert.assertTrue(b instanceof ByteString);
    ByteString byteString = (ByteString) b;
    Assert.assertArrayEquals(new byte[] { (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
        byteString.getBytes());
}
 
Example #2
Source File: RationalNumberEncoderTest.java    From cbor-java with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    encoder.encode(new RationalNumber(ONE, TWO));
    ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    CborDecoder decoder = new CborDecoder(inputStream);

    Array expected = new Array();
    expected.setTag(11);
    expected.add(ONE);
    expected.add(TWO);

    Object object = decoder.decodeNext();
    assertTrue(object instanceof Array);
    Array decoded = (Array) object;

    assertEquals(new Tag(30), decoded.getTag());
    assertEquals(2, decoded.getDataItems().size());
    assertEquals(ONE, decoded.getDataItems().get(0));
    assertEquals(TWO, decoded.getDataItems().get(1));
}
 
Example #3
Source File: Example12Test.java    From cbor-java with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
        new byte[] { (byte) 0xc2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });
    CborDecoder decoder = new CborDecoder(byteArrayInputStream);
    DataItem b = decoder.decodeNext();

    Assert.assertTrue(b.hasTag());
    Tag tag = b.getTag();
    Assert.assertEquals(2, tag.getValue());

    Assert.assertTrue(b instanceof ByteString);
    ByteString byteString = (ByteString) b;
    Assert.assertArrayEquals(new byte[] { (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
        byteString.getBytes());
}
 
Example #4
Source File: MapTest.java    From cbor-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testItemOrderIsPreserved() throws CborException {
    List<DataItem> input = new CborBuilder().addMap().put(1, "v1").put(0, "v2").end().build();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteArrayOutputStream);
    encoder.nonCanonical().encode(input);
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    CborDecoder decoder = new CborDecoder(byteArrayInputStream);
    List<DataItem> output = decoder.decode();
    assertEquals(input, output);
    DataItem dataItem = output.get(0);
    assertEquals(MajorType.MAP, dataItem.getMajorType());
    Map map = (Map) dataItem;
    Collection<DataItem> values = map.getValues();
    Iterator<DataItem> iterator = values.iterator();
    assertEquals(new UnicodeString("v1"), iterator.next());
    assertEquals(new UnicodeString("v2"), iterator.next());
}
 
Example #5
Source File: UnsignedIntegerDecoderTest.java    From cbor-java with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDecodeBigNumbers() throws CborException {
    BigInteger value = BigInteger.ONE;
    for (int i = 1; i < 64; i++) {
        value = value.shiftLeft(1);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        CborEncoder encoder = new CborEncoder(baos);
        encoder.encode(new CborBuilder().add(value).build());
        byte[] encodedBytes = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(encodedBytes);
        CborDecoder decoder = new CborDecoder(bais);
        List<DataItem> dataItems = decoder.decode();
        assertNotNull(dataItems);
        assertEquals(1, dataItems.size());
        DataItem dataItem = dataItems.get(0);
        assertTrue(dataItem instanceof UnsignedInteger);
        assertEquals(value, ((UnsignedInteger) dataItem).getValue());
    }
}
 
Example #6
Source File: ByteStringDecoderTest.java    From cbor-java with Apache License 2.0 6 votes vote down vote up
public void decodingExample() throws CborException {
    byte bytes[] = { 0, 1, 2, 3 };
    // Encode
    ByteArrayOutputStream encodedStream = new ByteArrayOutputStream();
    new CborEncoder(encodedStream).encode(new ByteString(bytes));
    byte encodedBytes[] = encodedStream.toByteArray();
    // Decode
    ByteArrayInputStream inputStream = new ByteArrayInputStream(encodedBytes);
    CborDecoder decoder = new CborDecoder(inputStream);
    DataItem dataItem = decoder.decodeNext();
    assertEquals(MajorType.BYTE_STRING, dataItem.getMajorType());
    ByteString byteString = (ByteString) dataItem;
    byte[] decodedBytes = byteString.getBytes();
    // Verify
    assertEquals(bytes.length, decodedBytes.length);
    assertEquals(bytes[0], decodedBytes[0]);
    assertEquals(bytes[1], decodedBytes[1]);
    assertEquals(bytes[2], decodedBytes[2]);
    assertEquals(bytes[3], decodedBytes[3]);
}
 
Example #7
Source File: CborDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test(expected = CborException.class)
public void shouldThrowCborException2() throws CborException {
    CborDecoder cborDecoder = new CborDecoder(new InputStream() {

        @Override
        public int read() throws IOException {
            return (8 << 5); // invalid major type
        }

    });
    cborDecoder.decodeNext();
}
 
Example #8
Source File: MapDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldUseLastOfDuplicateKeysByDefault() throws CborException {
    byte[] bytes = new byte[] { (byte) 0xa2, 0x01, 0x01, 0x01, 0x02 };
    List<DataItem> decoded = CborDecoder.decode(bytes);
    Map map = (Map) decoded.get(0);
    assertEquals(map.getKeys().size(), 1);
    assertEquals(map.get(new UnsignedInteger(1)), new UnsignedInteger(2));
}
 
Example #9
Source File: Example69Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #10
Source File: CborDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecodeTaggedTags() throws CborException {
    DataItem decoded = CborDecoder.decode(new byte[] { (byte) 0xC1, (byte) 0xC2, 0x02 }).get(0);

    Tag outer = new Tag(1);
    Tag inner = new Tag(2);
    UnsignedInteger expected = new UnsignedInteger(2);
    inner.setTag(outer);
    expected.setTag(inner);

    assertEquals(expected, decoded);
}
 
Example #11
Source File: ByteStringDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecodeByteString1M() throws CborException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(baos);
    encoder.encode(new CborBuilder().add(new byte[1024 * 1024]).build());
    byte[] encodedBytes = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(encodedBytes);
    CborDecoder decoder = new CborDecoder(bais);
    List<DataItem> dataItems = decoder.decode();
    assertNotNull(dataItems);
    assertEquals(1, dataItems.size());
}
 
Example #12
Source File: MapDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test(expected = CborException.class)
public void shouldThrowOnDuplicateKeyIfEnabled() throws CborException {
    byte[] bytes = new byte[] { (byte) 0xa2, 0x01, 0x01, 0x01, 0x02 };
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    CborDecoder decoder = new CborDecoder(bais);
    decoder.setRejectDuplicateKeys(true);
    decoder.decode();
}
 
Example #13
Source File: LanguageTaggedStringDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test(expected = CborException.class)
public void shouldThrowException() throws CborException {
    List<DataItem> items = new CborBuilder().addTag(38).build();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(baos);
    encoder.encode(items);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    CborDecoder decoder = new CborDecoder(bais);
    decoder.decode();
}
 
Example #14
Source File: AbstractSinglePrecisionFloatTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(encodedValue);
    CborDecoder decoder = new CborDecoder(inputStream);
    DataItem dataItem = decoder.decodeNext();
    Assert.assertTrue(dataItem instanceof SinglePrecisionFloat);
    SinglePrecisionFloat singlePrecisionFloat = (SinglePrecisionFloat) dataItem;
    Assert.assertEquals(value, singlePrecisionFloat.getValue(), 0);
}
 
Example #15
Source File: LanguageTaggedStringDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test(expected = CborException.class)
public void testExceptionOnNotAnArray() throws CborException {
    List<DataItem> items = new CborBuilder().addTag(38).add(true).build();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(baos);
    encoder.encode(items);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    CborDecoder decoder = new CborDecoder(bais);
    decoder.decode();
}
 
Example #16
Source File: ByteStringDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecodeByteString1K() throws CborException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(baos);
    encoder.encode(new CborBuilder().add(new byte[1024]).build());
    byte[] encodedBytes = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(encodedBytes);
    CborDecoder decoder = new CborDecoder(bais);
    List<DataItem> dataItems = decoder.decode();
    assertNotNull(dataItems);
    assertEquals(1, dataItems.size());
}
 
Example #17
Source File: LanguageTaggedStringDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecoding() throws CborException {
    List<DataItem> items = new CborBuilder().addTag(38).addArray().add("en").add("string").end().build();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(baos);
    encoder.encode(items);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    CborDecoder decoder = new CborDecoder(bais);
    DataItem item = decoder.decodeNext();
    assertEquals(new LanguageTaggedString(new UnicodeString("en"), new UnicodeString("string")), item);
}
 
Example #18
Source File: Example50Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #19
Source File: AbstractByteStringTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(encodedValue);
    CborDecoder decoder = new CborDecoder(inputStream);
    DataItem dataItem = decoder.decodeNext();
    Assert.assertTrue(dataItem instanceof ByteString);
    ByteString byteString = (ByteString) dataItem;
    Assert.assertArrayEquals(value, byteString.getBytes());
}
 
Example #20
Source File: Example74Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    decoder.setAutoDecodeInfinitiveArrays(false);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #21
Source File: Example83Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #22
Source File: Example70Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #23
Source File: Example46Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    DataItem dataItem = decoder.decodeNext();
    Assert.assertTrue(dataItem instanceof SimpleValue);
    SimpleValue simpleValue = (SimpleValue) dataItem;
    Assert.assertEquals(VALUE, simpleValue);
}
 
Example #24
Source File: Example53Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #25
Source File: Example79Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #26
Source File: CborDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test(expected = CborException.class)
public void shouldThrowCborException() throws CborException {
    CborDecoder cborDecoder = new CborDecoder(new InputStream() {

        @Override
        public int read() throws IOException {
            throw new IOException();
        }

    });
    cborDecoder.decodeNext();
}
 
Example #27
Source File: Example81Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #28
Source File: Example64Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    DataItem dataItem = decoder.decodeNext();
    Assert.assertTrue(dataItem instanceof Array);
    Array array = (Array) dataItem;
    Assert.assertEquals(3, array.getDataItems().size());
    Assert.assertEquals(1, ((Number) array.getDataItems().get(0)).getValue().intValue());
    Assert.assertEquals(2, ((Number) array.getDataItems().get(1)).getValue().intValue());
    Assert.assertEquals(3, ((Number) array.getDataItems().get(2)).getValue().intValue());
}
 
Example #29
Source File: Example75Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    decoder.setAutoDecodeInfinitiveArrays(false);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #30
Source File: LanguageTaggedStringDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test(expected = CborException.class)
public void testExceptionOnNot2ElementArray() throws CborException {
    List<DataItem> items = new CborBuilder().addTag(38).addArray().add(true).end().build();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(baos);
    encoder.encode(items);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    CborDecoder decoder = new CborDecoder(bais);
    decoder.decode();
}