JacksonCborFactory implements CborFactory { @Override public JsonValue readFrom(final byte[] bytes) { final ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); try { final CBORParser parser = JACKSON_CBOR_FACTORY.createParser(bytes); return parseValue(parser, byteBuffer); } catch (final IOException | IllegalArgumentException | ArrayIndexOutOfBoundsException e) { throw createJsonParseException(byteBuffer, e); } } JacksonCborFactory(); @Override boolean isCborAvailable(); @Override JsonValue readFrom(final byte[] bytes); @Override JsonValue readFrom(final byte[] bytes, final int offset, final int length); @Override JsonValue readFrom(final ByteBuffer byteBuffer); @Override byte[] toByteArray(final JsonValue jsonValue); @Override ByteBuffer toByteBuffer(final JsonValue jsonValue); @Override void writeToByteBuffer(final JsonValue jsonValue, final ByteBuffer byteBuffer); @Override byte[] createCborRepresentation(final Map<String, JsonField> jsonFieldMap, final int guessedSerializedSize); @Override byte[] createCborRepresentation(final List<JsonValue> list, final int guessedSerializedSize);  }
@Test public void readFromByteArrayWithoutOffset() { final JsonValue result = cborFactory.readFrom(testBytes); assertThat(result).isEqualTo(testValue); }
@Test public void readFromByteArrayWithOffset() { final int paddingFront = 20; final int paddingBack = 42; final byte[] arrayWithOffsetAndLength = new byte[ paddingFront + testBytes.length + paddingBack]; System.arraycopy(testBytes, 0, arrayWithOffsetAndLength, paddingFront-1, testBytes.length); final JsonValue result = cborFactory.readFrom(arrayWithOffsetAndLength, paddingFront - 1, testBytes.length); assertThat(result).isEqualTo(testValue); }
@Test public void readFromByteBuffer() { final JsonValue result = cborFactory.readFrom(ByteBuffer.wrap(testBytes)); assertThat(result).isEqualTo(testValue); }
@Test public void readFromByteBufferWithInaccessibleArray() { final ByteBuffer readOnlyBuffer = ByteBuffer.wrap(testBytes).asReadOnlyBuffer(); final JsonValue result = cborFactory.readFrom(readOnlyBuffer); assertThat(result).isEqualTo(testValue); }