Java Code Examples for co.nstant.in.cbor.model.SimpleValue

The following examples show how to use co.nstant.in.cbor.model.SimpleValue. These examples are extracted from open source projects. 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 Project: cbor-java   Author: c-rack   File: MapEncoder.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void encode(Map map) throws CborException {
    Collection<DataItem> keys = map.getKeys();

    if (map.isChunked()) {
        encodeTypeChunked(MajorType.MAP);
    } else {
        encodeTypeAndLength(MajorType.MAP, keys.size());
    }

    if (keys.isEmpty()) {
        return;
    }

    if (map.isChunked()) {
        encodeNonCanonical(map);
        encoder.encode(SimpleValue.BREAK);
    } else {
        if (encoder.isCanonical()) {
            encodeCanonical(map);
        } else {
            encodeNonCanonical(map);
        }
    }
}
 
Example #2
Source Project: cbor-java   Author: c-rack   File: UnicodeStringEncoder.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void encode(UnicodeString dataItem) throws CborException {
    String string = dataItem.getString();
    if (dataItem.isChunked()) {
        encodeTypeChunked(MajorType.UNICODE_STRING);
        if (string != null) {
            encode(new UnicodeString(string));
        }
    } else if (string == null) {
        encoder.encode(SimpleValue.NULL);
    } else {
        byte[] bytes;
        bytes = string.getBytes(StandardCharsets.UTF_8);
        encodeTypeAndLength(MajorType.UNICODE_STRING, bytes.length);
        write(bytes);
    }
}
 
Example #3
Source Project: cbor-java   Author: c-rack   File: ByteStringEncoder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void encode(ByteString byteString) throws CborException {
    byte[] bytes = byteString.getBytes();
    if (byteString.isChunked()) {
        encodeTypeChunked(MajorType.BYTE_STRING);
        if (bytes != null) {
            encode(new ByteString(bytes));
        }
    } else if (bytes == null) {
        encoder.encode(SimpleValue.NULL);
    } else {
        encodeTypeAndLength(MajorType.BYTE_STRING, bytes.length);
        write(bytes);
    }
}
 
Example #4
Source Project: cbor-java   Author: c-rack   File: CborEncoder.java    License: 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 #5
Source Project: cbor-java   Author: c-rack   File: SpecialDecoder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Special decode(int initialByte) throws CborException {
    switch (SpecialType.ofByte(initialByte)) {
    case BREAK:
        return Special.BREAK;
    case SIMPLE_VALUE:
        switch (SimpleValueType.ofByte(initialByte)) {
        case FALSE:
            return SimpleValue.FALSE;
        case TRUE:
            return SimpleValue.TRUE;
        case NULL:
            return SimpleValue.NULL;
        case UNDEFINED:
            return SimpleValue.UNDEFINED;
        case UNALLOCATED:
            return new SimpleValue(initialByte & 31);
        case RESERVED:
        default:
            throw new CborException("Not implemented");
        }
    case IEEE_754_HALF_PRECISION_FLOAT:
        return halfPrecisionFloatDecoder.decode(initialByte);
    case IEEE_754_SINGLE_PRECISION_FLOAT:
        return singlePrecisionFloatDecoder.decode(initialByte);
    case IEEE_754_DOUBLE_PRECISION_FLOAT:
        return doublePrecisionFloatDecoder.decode(initialByte);
    case SIMPLE_VALUE_NEXT_BYTE:
        return new SimpleValue(nextSymbol());
    case UNALLOCATED:
    default:
        throw new CborException("Not implemented");
    }
}
 
Example #6
Source Project: cbor-java   Author: c-rack   File: AbstractBuilder.java    License: Apache License 2.0 5 votes vote down vote up
protected DataItem convert(boolean value) {
    if (value) {
        return SimpleValue.TRUE;
    } else {
        return SimpleValue.FALSE;
    }
}
 
Example #7
Source Project: cbor-java   Author: c-rack   File: Example41Test.java    License: 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 #8
Source Project: cbor-java   Author: c-rack   File: Example41Test.java    License: 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 #9
Source Project: cbor-java   Author: c-rack   File: Example45Test.java    License: 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 #10
Source Project: cbor-java   Author: c-rack   File: Example44Test.java    License: 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.UNDEFINED);
    Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray());
}
 
Example #11
Source Project: cbor-java   Author: c-rack   File: Example44Test.java    License: 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 #12
Source Project: cbor-java   Author: c-rack   File: Example42Test.java    License: 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.TRUE);
    Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray());
}
 
Example #13
Source Project: cbor-java   Author: c-rack   File: Example42Test.java    License: 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 #14
Source Project: cbor-java   Author: c-rack   File: Example47Test.java    License: 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 #15
Source Project: cbor-java   Author: c-rack   File: Example43Test.java    License: 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.NULL);
    Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray());
}
 
Example #16
Source Project: cbor-java   Author: c-rack   File: Example43Test.java    License: 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 #17
Source Project: cbor-java   Author: c-rack   File: Example46Test.java    License: 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 #18
Source Project: cbor-java   Author: c-rack   File: SimpleValueDecoderTest.java    License: 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);
}
 
Example #19
Source Project: cbor-java   Author: c-rack   File: ArrayBuilderTest.java    License: Apache License 2.0 5 votes vote down vote up
@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 #20
Source Project: SI   Author: iotoasis   File: CBORTest.java    License: BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static HashMap getHashMap(DataItem dataItem) {
	HashMap hMap = new HashMap();
	
	co.nstant.in.cbor.model.Map map = (co.nstant.in.cbor.model.Map)dataItem;
	HashMap hashMap = new HashMap();
	Iterator<DataItem> itr = map.getKeys().iterator();
	
	while(itr.hasNext()) {
		
		DataItem key = itr.next();
		//System.out.println(map.get(key).getMajorType().toString());
		if(map.get(key).getMajorType().toString().equals("MAP")) {
			
			hMap.put(key.toString(), getHashMap(map.get(key)));
		} else if(map.get(key).getMajorType().toString().equals("ARRAY")) {
			Array cborArr = (Array)map.get(key);
			
			ArrayList<String> arr = new ArrayList<String>();
			for(int i = 0; i < cborArr.getDataItems().size(); i++) {
				arr.add( cborArr.getDataItems().get(i).toString() );
			}
			
			hMap.put(key.toString(), arr);
		} else {
			String value = map.get(key).toString();
			if(map.get(key).getMajorType().name().contains("INTEGER")) {
				hMap.put(key.toString(),Integer.parseInt(value) );
			} else if(map.get(key).getMajorType().name().contains("SPECIAL")){
				SimpleValue simpleValue = (SimpleValue)map.get(key);
				
				String temp = simpleValue.getSimpleValueType().toString().toLowerCase();
				if(temp.equals("true") || temp.equals("false")) {
					hMap.put(key.toString(), Boolean.getBoolean(temp));
				} else {
					hMap.put(key.toString(), temp);
				}
			}else {
				
				hMap.put(key.toString(), value);
			}
			
		}
	}
		
	return hMap;
}
 
Example #21
Source Project: SI   Author: iotoasis   File: Utils.java    License: BSD 2-Clause "Simplified" License 4 votes vote down vote up
private static HashMap getCborHashMap(DataItem dataItem) {
	HashMap hMap = new HashMap();
	
	co.nstant.in.cbor.model.Map map = (co.nstant.in.cbor.model.Map)dataItem;
	HashMap hashMap = new HashMap();
	Iterator<DataItem> itr = map.getKeys().iterator();
	
	while(itr.hasNext()) {
		
		DataItem key = itr.next();
		
		if(map.get(key).getMajorType().toString().equals("MAP")) {
			
			hMap.put(key.toString(), getCborHashMap(map.get(key)));
		} else if(map.get(key).getMajorType().toString().equals("ARRAY")) {
			Array cborArr = (Array)map.get(key);
			
			ArrayList<String> arr = new ArrayList<String>();
			for(int i = 0; i < cborArr.getDataItems().size(); i++) {
				arr.add( cborArr.getDataItems().get(i).toString() );
			}
			
			hMap.put(key.toString(), arr);
		} else if(map.get(key).getMajorType().name().contains("SPECIAL")){
			SimpleValue simpleValue = (SimpleValue)map.get(key);
			
			String temp = simpleValue.getSimpleValueType().toString().toLowerCase();
			if(temp.equals("true") || temp.equals("false")) {
				hMap.put(key.toString(), Boolean.getBoolean(temp));
			} else {
				hMap.put(key.toString(), temp);
			}
		} else {
			String value = map.get(key).toString();
			if(map.get(key).getMajorType().name().contains("INTEGER")) {
				hMap.put(key.toString(),Integer.parseInt(value) );
			} else {
				hMap.put(key.toString(), value);
			}
			
		}
	}
		
	return hMap;
}
 
Example #22
Source Project: SI   Author: iotoasis   File: CBORTest.java    License: BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static HashMap getHashMap(DataItem dataItem) {
	HashMap hMap = new HashMap();
	
	co.nstant.in.cbor.model.Map map = (co.nstant.in.cbor.model.Map)dataItem;
	HashMap hashMap = new HashMap();
	Iterator<DataItem> itr = map.getKeys().iterator();
	
	while(itr.hasNext()) {
		
		DataItem key = itr.next();
		//System.out.println(map.get(key).getMajorType().toString());
		if(map.get(key).getMajorType().toString().equals("MAP")) {
			
			hMap.put(key.toString(), getHashMap(map.get(key)));
		} else if(map.get(key).getMajorType().toString().equals("ARRAY")) {
			Array cborArr = (Array)map.get(key);
			
			ArrayList<String> arr = new ArrayList<String>();
			for(int i = 0; i < cborArr.getDataItems().size(); i++) {
				arr.add( cborArr.getDataItems().get(i).toString() );
			}
			
			hMap.put(key.toString(), arr);
		} else {
			String value = map.get(key).toString();
			if(map.get(key).getMajorType().name().contains("INTEGER")) {
				hMap.put(key.toString(),Integer.parseInt(value) );
			} else if(map.get(key).getMajorType().name().contains("SPECIAL")){
				SimpleValue simpleValue = (SimpleValue)map.get(key);
				
				String temp = simpleValue.getSimpleValueType().toString().toLowerCase();
				if(temp.equals("true") || temp.equals("false")) {
					hMap.put(key.toString(), Boolean.getBoolean(temp));
				} else {
					hMap.put(key.toString(), temp);
				}
			}else {
				
				hMap.put(key.toString(), value);
			}
			
		}
	}
		
	return hMap;
}
 
Example #23
Source Project: SI   Author: iotoasis   File: Utils.java    License: BSD 2-Clause "Simplified" License 4 votes vote down vote up
private static HashMap getCborHashMap(DataItem dataItem) {
	HashMap hMap = new HashMap();
	
	co.nstant.in.cbor.model.Map map = (co.nstant.in.cbor.model.Map)dataItem;
	HashMap hashMap = new HashMap();
	Iterator<DataItem> itr = map.getKeys().iterator();
	
	while(itr.hasNext()) {
		
		DataItem key = itr.next();
		
		if(map.get(key).getMajorType().toString().equals("MAP")) {
			
			hMap.put(key.toString(), getCborHashMap(map.get(key)));
		} else if(map.get(key).getMajorType().toString().equals("ARRAY")) {
			Array cborArr = (Array)map.get(key);
			
			ArrayList<String> arr = new ArrayList<String>();
			for(int i = 0; i < cborArr.getDataItems().size(); i++) {
				arr.add( cborArr.getDataItems().get(i).toString() );
			}
			
			hMap.put(key.toString(), arr);
		} else if(map.get(key).getMajorType().name().contains("SPECIAL")){
			SimpleValue simpleValue = (SimpleValue)map.get(key);
			
			String temp = simpleValue.getSimpleValueType().toString().toLowerCase();
			if(temp.equals("true") || temp.equals("false")) {
				hMap.put(key.toString(), Boolean.getBoolean(temp));
			} else {
				hMap.put(key.toString(), temp);
			}
		} else {
			String value = map.get(key).toString();
			if(map.get(key).getMajorType().name().contains("INTEGER")) {
				hMap.put(key.toString(),Integer.parseInt(value) );
			} else {
				hMap.put(key.toString(), value);
			}
			
		}
	}
		
	return hMap;
}
 
Example #24
Source Project: cbor-java   Author: c-rack   File: SpecialEncoder.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void encode(Special dataItem) throws CborException {
    switch (dataItem.getSpecialType()) {
    case BREAK:
        write((7 << 5) | 31);
        break;
    case SIMPLE_VALUE:
        SimpleValue simpleValue = (SimpleValue) dataItem;
        switch (simpleValue.getSimpleValueType()) {
        case FALSE:
        case NULL:
        case TRUE:
        case UNDEFINED:
            SimpleValueType type = simpleValue.getSimpleValueType();
            write((7 << 5) | type.getValue());
            break;
        case UNALLOCATED:
            write((7 << 5) | simpleValue.getValue());
            break;
        case RESERVED:
            break;
        }
        break;
    case UNALLOCATED:
        throw new CborException("Unallocated special type");
    case IEEE_754_HALF_PRECISION_FLOAT:
        if (!(dataItem instanceof HalfPrecisionFloat)) {
            throw new CborException("Wrong data item type");
        }
        halfPrecisionFloatEncoder.encode((HalfPrecisionFloat) dataItem);
        break;
    case IEEE_754_SINGLE_PRECISION_FLOAT:
        if (!(dataItem instanceof SinglePrecisionFloat)) {
            throw new CborException("Wrong data item type");
        }
        singlePrecisionFloatEncoder.encode((SinglePrecisionFloat) dataItem);
        break;
    case IEEE_754_DOUBLE_PRECISION_FLOAT:
        if (!(dataItem instanceof DoublePrecisionFloat)) {
            throw new CborException("Wrong data item type");
        }
        doublePrecisionFloatEncoder.encode((DoublePrecisionFloat) dataItem);
        break;
    case SIMPLE_VALUE_NEXT_BYTE:
        if (!(dataItem instanceof SimpleValue)) {
            throw new CborException("Wrong data item type");
        }
        SimpleValue simpleValueNextByte = (SimpleValue) dataItem;
        write((byte) ((7 << 5) | 24), (byte) simpleValueNextByte.getValue());
        break;
    }
}
 
Example #25
Source Project: cbor-java   Author: c-rack   File: UnicodeStringBuilder.java    License: Apache License 2.0 4 votes vote down vote up
public T end() {
    getParent().addChunk(SimpleValue.BREAK);
    return getParent();
}
 
Example #26
Source Project: cbor-java   Author: c-rack   File: ArrayBuilder.java    License: Apache License 2.0 4 votes vote down vote up
public T end() {
    if (array.isChunked()) {
        add(SimpleValue.BREAK);
    }
    return getParent();
}
 
Example #27
Source Project: cbor-java   Author: c-rack   File: ByteStringBuilder.java    License: Apache License 2.0 4 votes vote down vote up
public T end() {
    getParent().addChunk(SimpleValue.BREAK);
    return getParent();
}