Java Code Examples for co.nstant.in.cbor.CborDecoder#decodeNext()

The following examples show how to use co.nstant.in.cbor.CborDecoder#decodeNext() . 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: 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 2
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 3
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 4
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 5
Source File: Example43Test.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(SimpleValue.NULL, simpleValue);
}
 
Example 6
Source File: AbstractStringTest.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 UnicodeString);
    UnicodeString unicodeString = (UnicodeString) dataItem;
    Assert.assertEquals(value, unicodeString.toString());
}
 
Example 7
Source File: AbstractDoublePrecisionFloatTest.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 DoublePrecisionFloat);
    DoublePrecisionFloat doublePrecisionFloat = (DoublePrecisionFloat) dataItem;
    Assert.assertEquals(value, doublePrecisionFloat.getValue(), 0);
}
 
Example 8
Source File: AbstractNumberTest.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 Number);
    Number number = (Number) dataItem;
    Assert.assertEquals(value, number.getValue());
}
 
Example 9
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 10
Source File: AbstractHalfPrecisionFloatTest.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 HalfPrecisionFloat);
    HalfPrecisionFloat halfPrecisionFloat = (HalfPrecisionFloat) dataItem;
    Assert.assertEquals(value, halfPrecisionFloat.getValue(), 0);
}
 
Example 11
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 12
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 13
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 14
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 15
Source File: Example42Test.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(SimpleValue.TRUE, simpleValue);
}
 
Example 16
Source File: Example63Test.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.assertTrue(array.getDataItems().isEmpty());
}
 
Example 17
Source File: Example44Test.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(SimpleValue.UNDEFINED, simpleValue);
}
 
Example 18
Source File: Example45Test.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 19
Source File: Example41Test.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(SimpleValue.FALSE, simpleValue);
}
 
Example 20
Source File: Example65Test.java    From cbor-java with Apache License 2.0 4 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());

    DataItem dataItem1 = array.getDataItems().get(0);
    DataItem dataItem2 = array.getDataItems().get(1);
    DataItem dataItem3 = array.getDataItems().get(2);

    Assert.assertTrue(dataItem1 instanceof Number);
    Assert.assertTrue(dataItem2 instanceof Array);
    Assert.assertTrue(dataItem3 instanceof Array);

    Number number = (Number) dataItem1;
    Array array1 = (Array) dataItem2;
    Array array2 = (Array) dataItem3;

    Assert.assertEquals(1, number.getValue().intValue());
    Assert.assertEquals(2, array1.getDataItems().size());
    Assert.assertEquals(2, array2.getDataItems().size());

    DataItem array1item1 = array1.getDataItems().get(0);
    DataItem array1item2 = array1.getDataItems().get(1);

    Assert.assertTrue(array1item1 instanceof Number);
    Assert.assertTrue(array1item2 instanceof Number);

    Number array1number1 = (Number) array1item1;
    Number array1number2 = (Number) array1item2;

    Assert.assertEquals(2, array1number1.getValue().intValue());
    Assert.assertEquals(3, array1number2.getValue().intValue());

    DataItem array2item1 = array2.getDataItems().get(0);
    DataItem array2item2 = array2.getDataItems().get(1);

    Assert.assertTrue(array2item1 instanceof Number);
    Assert.assertTrue(array2item2 instanceof Number);

    Number array2number1 = (Number) array2item1;
    Number array2number2 = (Number) array2item2;

    Assert.assertEquals(4, array2number1.getValue().intValue());
    Assert.assertEquals(5, array2number2.getValue().intValue());
}