co.nstant.in.cbor.CborBuilder Java Examples
The following examples show how to use
co.nstant.in.cbor.CborBuilder.
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: MapTest.java From cbor-java with Apache License 2.0 | 6 votes |
@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 #2
Source File: Puback.java From amazon-freertos-ble-android-sdk with Apache License 2.0 | 6 votes |
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 #3
Source File: ListNetworkReq.java From amazon-freertos-ble-android-sdk with Apache License 2.0 | 6 votes |
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 #4
Source File: EditNetworkReq.java From amazon-freertos-ble-android-sdk with Apache License 2.0 | 6 votes |
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 #5
Source File: DeleteNetworkReq.java From amazon-freertos-ble-android-sdk with Apache License 2.0 | 6 votes |
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 #6
Source File: NoneAttestationObject.java From android-webauthn-authenticator with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * 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/#none-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", "none") .putMap("attStmt") .end() .end() .build() ); } catch (CborException e) { throw new VirgilException("couldn't serialize to cbor", e); } return baos.toByteArray(); }
Example #7
Source File: PackedSelfAttestationObject.java From android-webauthn-authenticator with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * 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 #8
Source File: SaveNetworkReq.java From amazon-freertos-ble-android-sdk with Apache License 2.0 | 6 votes |
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: Suback.java From amazon-freertos-ble-android-sdk with Apache License 2.0 | 6 votes |
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 #10
Source File: Publish.java From amazon-freertos-ble-android-sdk with Apache License 2.0 | 6 votes |
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 #11
Source File: Unsubscribe.java From amazon-freertos-ble-android-sdk with Apache License 2.0 | 6 votes |
public byte[] encode() { byte[] unsubscribeBytes = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ArrayBuilder<MapBuilder<CborBuilder>> topicsArray = new CborBuilder() .addMap() .put(TYPE_KEY, type) .putArray(TOPICS_KEY); for (String topic : topics) { topicsArray = topicsArray.add(topic); } List<DataItem> cbordata = topicsArray.end().end().build(); new CborEncoder(baos).encode(cbordata); unsubscribeBytes = baos.toByteArray(); } catch (CborException e) { Log.e(TAG, "Failed to encode.", e); } return unsubscribeBytes; }
Example #12
Source File: MapBuilderTest.java From cbor-java with Apache License 2.0 | 6 votes |
@Test public void startMapInMap() { CborBuilder builder = new CborBuilder(); List<DataItem> dataItems = builder.addMap().startMap(new ByteString(new byte[] { 0x01 })).put(1, 2).end() .startMap(1).end().startMap("asdf").end().end().build(); Map rootMap = (Map) dataItems.get(0); DataItem keys[] = new DataItem[3]; rootMap.getKeys().toArray(keys); assertEquals(keys[0], new ByteString(new byte[] { 0x01 })); assertEquals(keys[1], new UnsignedInteger(1)); assertEquals(keys[2], new UnicodeString("asdf")); assertTrue(rootMap.get(keys[0]) instanceof Map); assertTrue(rootMap.get(keys[1]) instanceof Map); assertTrue(rootMap.get(keys[2]) instanceof Map); }
Example #13
Source File: Connack.java From amazon-freertos-ble-android-sdk with Apache License 2.0 | 6 votes |
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 #14
Source File: Connect.java From amazon-freertos-ble-android-sdk with Apache License 2.0 | 6 votes |
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 #15
Source File: UnsignedIntegerDecoderTest.java From cbor-java with Apache License 2.0 | 6 votes |
@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 #16
Source File: Unsuback.java From amazon-freertos-ble-android-sdk with Apache License 2.0 | 6 votes |
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 #17
Source File: CborDecoderTest.java From cbor-java with Apache License 2.0 | 5 votes |
@Test public void shouldDecodeTaggedRationalNumber() throws CborException { List<DataItem> items = new CborBuilder().addTag(1).addTag(30).addArray().add(1).add(2).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); RationalNumber expected = new RationalNumber(new UnsignedInteger(1), new UnsignedInteger(2)); expected.getTag().setTag(new Tag(1)); assertEquals(expected, decoder.decodeNext()); }
Example #18
Source File: CborDecoderTest.java From cbor-java with Apache License 2.0 | 5 votes |
@Test public void shouldDecodeRationalNumber() throws CborException { List<DataItem> items = new CborBuilder().addTag(30).addArray().add(1).add(2).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); assertEquals(new RationalNumber(new UnsignedInteger(1), new UnsignedInteger(2)), decoder.decodeNext()); }
Example #19
Source File: ArrayBuilderTest.java From cbor-java with Apache License 2.0 | 5 votes |
@Test public void shouldAddByteArray() { CborBuilder builder = new CborBuilder(); List<DataItem> dataItems = builder.addArray().add(new byte[] { 0x0 }).end().build(); assertEquals(1, dataItems.size()); assertTrue(dataItems.get(0) instanceof Array); Array array = (Array) dataItems.get(0); assertEquals(1, array.getDataItems().size()); assertTrue(array.getDataItems().get(0) instanceof ByteString); }
Example #20
Source File: LanguageTaggedStringDecoderTest.java From cbor-java with Apache License 2.0 | 5 votes |
@Test(expected = CborException.class) public void testExceptionOnNotFirstElementIsString() throws CborException { List<DataItem> items = new CborBuilder().addTag(38).addArray().add(true).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(); }
Example #21
Source File: LanguageTaggedStringDecoderTest.java From cbor-java with Apache License 2.0 | 5 votes |
@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(); }
Example #22
Source File: LanguageTaggedStringDecoderTest.java From cbor-java with Apache License 2.0 | 5 votes |
@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 #23
Source File: LanguageTaggedStringDecoderTest.java From cbor-java with Apache License 2.0 | 5 votes |
@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 #24
Source File: ByteStringDecoderTest.java From cbor-java with Apache License 2.0 | 5 votes |
@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 #25
Source File: ByteStringDecoderTest.java From cbor-java with Apache License 2.0 | 5 votes |
@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 #26
Source File: ByteStringDecoderTest.java From cbor-java with Apache License 2.0 | 5 votes |
@Test public void shouldDecodeChunkedByteString() throws CborException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); CborEncoder encoder = new CborEncoder(baos); encoder.encode(new CborBuilder().startByteString().add(new byte[] { '\0' }).add(new byte[] { 0x10 }) .add(new byte[] { 0x13 }).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 #27
Source File: UnicodeStringDecoderTest.java From cbor-java with Apache License 2.0 | 5 votes |
@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 #28
Source File: AbstractDoublePrecisionFloatTest.java From cbor-java with Apache License 2.0 | 5 votes |
@Test public void shouldEncode() throws CborException { List<DataItem> dataItems = new CborBuilder().add(value).build(); ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); CborEncoder encoder = new CborEncoder(byteOutputStream); encoder.encode(dataItems.get(0)); Assert.assertArrayEquals(encodedValue, byteOutputStream.toByteArray()); }
Example #29
Source File: ArrayBuilderTest.java From cbor-java with Apache License 2.0 | 5 votes |
@Test public void shouldAddBoolean() { CborBuilder builder = new CborBuilder(); List<DataItem> dataItems = builder.addArray().add(true).add(false).end().build(); assertEquals(1, dataItems.size()); assertTrue(dataItems.get(0) instanceof Array); Array array = (Array) dataItems.get(0); assertEquals(2, array.getDataItems().size()); assertTrue(array.getDataItems().get(0) instanceof SimpleValue); assertTrue(array.getDataItems().get(1) instanceof SimpleValue); }
Example #30
Source File: LanguageTaggedStringDecoderTest.java From cbor-java with Apache License 2.0 | 5 votes |
@Test(expected = CborException.class) public void testExceptionOnNotSecondElementIsString() throws CborException { List<DataItem> items = new CborBuilder().addTag(38).addArray().add("en").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(); }