co.nstant.in.cbor.CborEncoder Java Examples

The following examples show how to use co.nstant.in.cbor.CborEncoder. 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: Suback.java    From amazon-freertos-ble-android-sdk with Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] subackBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
            .addMap()
            .put(TYPE_KEY, type)
            .put(MSGID_KEY, msgID)
            .put(STATUS_KEY, status)
            .end()
            .build());
        subackBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return subackBytes;
}
 
Example #2
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 #3
Source File: Connect.java    From amazon-freertos-ble-android-sdk with Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] connectBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
            .addMap()
            .put(TYPE_KEY, type)
            .put(CLIENTID_KEY, clientID)
            .put(BROKERENDPOINT_KEY, brokerEndpoint)
            .put(CLEANSESSION_KEY, cleanSession)
            .end()
            .build());
        connectBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return connectBytes;
}
 
Example #4
Source File: Connack.java    From amazon-freertos-ble-android-sdk with Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] connackBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
            .addMap()
            .put(TYPE_KEY, type)
            .put(STATUS_KEY, status)
            .end()
            .build());
        connackBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return connackBytes;
}
 
Example #5
Source File: Publish.java    From amazon-freertos-ble-android-sdk with Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] publishBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
            .addMap()
            .put(TYPE_KEY, type)
            .put(TOPIC_KEY, topic)
            .put(MSGID_KEY, msgID)
            .put(QOS_KEY, qoS)
            .put(PAYLOAD_KEY, payloadBytes)
            .end()
            .build());
        publishBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return publishBytes;
}
 
Example #6
Source File: Unsuback.java    From amazon-freertos-ble-android-sdk with Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] unsubackBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
                .addMap()
                .put(TYPE_KEY, type)
                .put(MSGID_KEY, msgID)
                .end()
                .build());
        unsubackBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return unsubackBytes;
}
 
Example #7
Source File: Puback.java    From amazon-freertos-ble-android-sdk with Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] pubackBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
            .addMap()
            .put(TYPE_KEY, type)
            .put(MSGID_KEY, msgID)
            .end()
            .build());
        pubackBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return pubackBytes;
}
 
Example #8
Source File: SaveNetworkReq.java    From amazon-freertos-ble-android-sdk with Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] SaveNetworkRequestBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
                .addMap()
                .put(TYPE_KEY, SAVE_NETWORK_REQ)
                .put(INDEX_KEY, index)
                .put(SSID_KEY, ssid)
                .put(BSSID_KEY, bssid)
                .put(PSK_KEY, psk)
                .put(SECURITY_KEY, security)
                .end()
                .build());
        SaveNetworkRequestBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return SaveNetworkRequestBytes;
}
 
Example #9
Source File: ListNetworkReq.java    From amazon-freertos-ble-android-sdk with Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] ListNetworkRequestBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
                .addMap()
                .put(TYPE_KEY, LIST_NETWORK_REQ)
                .put(MAXNETWORKS_KEY, maxNetworks)
                .put(TIMEOUT_KEY, timeout)
                .end()
                .build());
        ListNetworkRequestBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return ListNetworkRequestBytes;
}
 
Example #10
Source File: EditNetworkReq.java    From amazon-freertos-ble-android-sdk with Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] EditNetworkRequestBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
                .addMap()
                .put(TYPE_KEY, EDIT_NETWORK_REQ)
                .put(INDEX_KEY, index)
                .put(NEWINDEX_KEY, newIndex)
                .end()
                .build());
        EditNetworkRequestBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return EditNetworkRequestBytes;
}
 
Example #11
Source File: DeleteNetworkReq.java    From amazon-freertos-ble-android-sdk with Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] DeleteNetworkRequestBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
                .addMap()
                .put(TYPE_KEY, DELETE_NETWORK_REQ)
                .put(INDEX_KEY, index)
                .end()
                .build());
        DeleteNetworkRequestBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return DeleteNetworkRequestBytes;
}
 
Example #12
Source File: PackedSelfAttestationObject.java    From android-webauthn-authenticator with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Encode this self-attestation attObj as the CBOR required by the WebAuthn spec
 * https://www.w3.org/TR/webauthn/#sctn-attestation
 * https://www.w3.org/TR/webauthn/#packed-attestation
 *
 * @return CBOR encoding of the attestation object as a byte array
 * @throws VirgilException
 */
@Override
public byte[] asCBOR() throws VirgilException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        new CborEncoder(baos).encode(new CborBuilder()
                .addMap()
                .put("authData", this.authData)
                .put("fmt", "packed")
                .putMap("attStmt")
                .put("alg", (long) -7)
                .put("sig", this.signature)
                .end()
                .end()
                .build()
        );
    } catch (CborException e) {
        throw new VirgilException("couldn't serialize to cbor", e);
    }
    return baos.toByteArray();
}
 
Example #13
Source File: AbstractStringTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteOutputStream);
    encoder.encode(new UnicodeString(value));
    Assert.assertArrayEquals(encodedValue, byteOutputStream.toByteArray());
}
 
Example #14
Source File: Example82Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteOutputStream);
    encoder.encode(VALUE);
    Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray());
}
 
Example #15
Source File: Example67Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteOutputStream);
    encoder.encode(VALUE);
    Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray());
}
 
Example #16
Source File: Example14Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteArrayOutputStream);
    encoder.encode(new NegativeInteger(new BigInteger("-18446744073709551617")));
    Assert.assertArrayEquals(new byte[] { (byte) 0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
        byteArrayOutputStream.toByteArray());
}
 
Example #17
Source File: UnicodeStringDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecodeChunkedUnicodeString() throws CborException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(baos);
    encoder.encode(new CborBuilder().startString().add("foo").add("bar").end().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 #18
Source File: AbstractHalfPrecisionFloatTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteOutputStream);
    encoder.encode(new HalfPrecisionFloat(value));
    Assert.assertArrayEquals(encodedValue, byteOutputStream.toByteArray());
}
 
Example #19
Source File: Example41Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteOutputStream);
    encoder.encode(SimpleValue.FALSE);
    Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray());
}
 
Example #20
Source File: Example71Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteOutputStream);
    encoder.encode(VALUE);
    Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray());
}
 
Example #21
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 #22
Source File: Example76Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteOutputStream);
    encoder.encode(VALUE);
    Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray());
}
 
Example #23
Source File: Example52Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteOutputStream);
    encoder.encode(VALUE);
    Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray());
}
 
Example #24
Source File: CborDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test(expected = CborException.class)
public void shouldThrowOnRationalNumberDecode1() throws CborException {
    List<DataItem> items = new CborBuilder().addTag(30).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 #25
Source File: Example49Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteOutputStream);
    encoder.encode(VALUE);
    Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray());
}
 
Example #26
Source File: Example48Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteOutputStream);
    encoder.encode(VALUE);
    Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray());
}
 
Example #27
Source File: Example12Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteArrayOutputStream);
    encoder.encode(new UnsignedInteger(new BigInteger("18446744073709551616")));
    Assert.assertArrayEquals(new byte[] { (byte) 0xc2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
        byteArrayOutputStream.toByteArray());
}
 
Example #28
Source File: AbstractSinglePrecisionFloatTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteOutputStream);
    encoder.encode(new SinglePrecisionFloat(value));
    Assert.assertArrayEquals(encodedValue, byteOutputStream.toByteArray());
}
 
Example #29
Source File: Example45Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteOutputStream);
    encoder.encode(VALUE);
    Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray());
}
 
Example #30
Source File: Example50Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteOutputStream);
    encoder.encode(VALUE);
    Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray());
}