Java Code Examples for co.nstant.in.cbor.model.DataItem#getMajorType()

The following examples show how to use co.nstant.in.cbor.model.DataItem#getMajorType() . 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: ByteStringDecoder.java    From cbor-java with Apache License 2.0 6 votes vote down vote up
private ByteString decodeInfinitiveLength() throws CborException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    for (;;) {
        DataItem dataItem = decoder.decodeNext();
        if (dataItem == null) {
            throw new CborException("Unexpected end of stream");
        }
        MajorType majorType = dataItem.getMajorType();
        if (Special.BREAK.equals(dataItem)) {
            break;
        } else if (majorType == MajorType.BYTE_STRING) {
            ByteString byteString = (ByteString) dataItem;
            byte[] byteArray = byteString.getBytes();
            if (byteArray != null) {
                bytes.write(byteArray, 0, byteArray.length);
            }
        } else {
            throw new CborException("Unexpected major type " + majorType);
        }
    }
    return new ByteString(bytes.toByteArray());
}
 
Example 2
Source File: UnicodeStringDecoder.java    From cbor-java with Apache License 2.0 6 votes vote down vote up
private UnicodeString decodeInfinitiveLength() throws CborException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    for (;;) {
        DataItem dataItem = decoder.decodeNext();
        if (dataItem == null) {
            throw new CborException("Unexpected end of stream");
        }
        MajorType majorType = dataItem.getMajorType();
        if (Special.BREAK.equals(dataItem)) {
            break;
        } else if (majorType == MajorType.UNICODE_STRING) {
            UnicodeString unicodeString = (UnicodeString) dataItem;
            byte[] byteArray = unicodeString.toString().getBytes(StandardCharsets.UTF_8);
            bytes.write(byteArray, 0, byteArray.length);
        } else {
            throw new CborException("Unexpected major type " + majorType);
        }
    }
    return new UnicodeString(new String(bytes.toByteArray(), StandardCharsets.UTF_8));
}
 
Example 3
Source File: CborEncoder.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
/**
 * Encode a single {@link DataItem}.
 *
 * @param dataItem the {@link DataItem} to encode. If null, encoder encodes a
 *                 {@link SimpleValue} NULL value.
 * @throws CborException if {@link DataItem} could not be encoded or there was
 *                       an problem with the {@link OutputStream}.
 */
public void encode(DataItem dataItem) throws CborException {
    if (dataItem == null) {
        dataItem = SimpleValue.NULL;
    }

    if (dataItem.hasTag()) {
        Tag tagDi = dataItem.getTag();
        encode(tagDi);
    }

    switch (dataItem.getMajorType()) {
    case UNSIGNED_INTEGER:
        unsignedIntegerEncoder.encode((UnsignedInteger) dataItem);
        break;
    case NEGATIVE_INTEGER:
        negativeIntegerEncoder.encode((NegativeInteger) dataItem);
        break;
    case BYTE_STRING:
        byteStringEncoder.encode((ByteString) dataItem);
        break;
    case UNICODE_STRING:
        unicodeStringEncoder.encode((UnicodeString) dataItem);
        break;
    case ARRAY:
        arrayEncoder.encode((Array) dataItem);
        break;
    case MAP:
        mapEncoder.encode((Map) dataItem);
        break;
    case SPECIAL:
        specialEncoder.encode((Special) dataItem);
        break;
    case TAG:
        tagEncoder.encode((Tag) dataItem);
        break;
    default:
        throw new CborException("Unknown major type");
    }
}
 
Example 4
Source File: SimpleValueDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecodeBoolean() throws CborException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteArrayOutputStream);
    encoder.encode(SimpleValue.TRUE);
    encoder.encode(SimpleValue.FALSE);
    byte[] encodedBytes = byteArrayOutputStream.toByteArray();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(encodedBytes);
    List<DataItem> dataItems = new CborDecoder(byteArrayInputStream).decode();
    int result = 0;
    int position = 1;
    for (DataItem dataItem : dataItems) {
        position++;
        switch (dataItem.getMajorType()) {
        case SPECIAL:
            Special special = (Special) dataItem;
            switch (special.getSpecialType()) {
            case SIMPLE_VALUE:
                SimpleValue simpleValue = (SimpleValue) special;
                switch (simpleValue.getSimpleValueType()) {
                case FALSE:
                    result += position * 2;
                    break;
                case TRUE:
                    result += position * 3;
                    break;
                default:
                    break;
                }
                break;
            default:
                break;
            }
            break;
        default:
            break;
        }
    }
    assertEquals(12, result);
}