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

The following examples show how to use co.nstant.in.cbor.model.Array. 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: webauthndemo   Author: google   File: PackedAttestationStatement.java    License: Apache License 2.0 6 votes vote down vote up
@Override
DataItem encode() throws CborException {
  Map result = new Map();
  if (attestnCert != null) {
    Array x5c = new Array();
    x5c.add(new ByteString(attestnCert));
    for (byte[] cert : this.caCert) {
      x5c.add(new ByteString(cert));
    }
    result.put(new UnicodeString("x5c"), x5c);
  }
  if (ecdaaKeyId != null) {
    result.put(new UnicodeString("ecdaaKeyId"), new ByteString(ecdaaKeyId));
  }
  result.put(new UnicodeString("sig"), new ByteString(sig));
  result.put(new UnicodeString("alg"), new UnicodeString(alg.toString()));

  return result;
}
 
Example #2
Source Project: cbor-java   Author: c-rack   File: ArrayDecoder.java    License: Apache License 2.0 6 votes vote down vote up
private Array decodeInfinitiveLength() throws CborException {
    Array array = new Array();
    array.setChunked(true);
    if (decoder.isAutoDecodeInfinitiveArrays()) {
        DataItem dataItem;
        for (;;) {
            dataItem = decoder.decodeNext();
            if (dataItem == null) {
                throw new CborException("Unexpected end of stream");
            }
            if (Special.BREAK.equals(dataItem)) {
                array.add(Special.BREAK);
                break;
            }
            array.add(dataItem);
        }
    }
    return array;
}
 
Example #3
Source Project: cbor-java   Author: c-rack   File: RationalNumberEncoderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    encoder.encode(new RationalNumber(ONE, TWO));
    ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    CborDecoder decoder = new CborDecoder(inputStream);

    Array expected = new Array();
    expected.setTag(11);
    expected.add(ONE);
    expected.add(TWO);

    Object object = decoder.decodeNext();
    assertTrue(object instanceof Array);
    Array decoded = (Array) object;

    assertEquals(new Tag(30), decoded.getTag());
    assertEquals(2, decoded.getDataItems().size());
    assertEquals(ONE, decoded.getDataItems().get(0));
    assertEquals(TWO, decoded.getDataItems().get(1));
}
 
Example #4
Source Project: webauthndemo   Author: google   File: FidoU2fAttestationStatement.java    License: Apache License 2.0 5 votes vote down vote up
@Override
DataItem encode() throws CborException {
  Map result = new Map();
  Array x5c = new Array();
  x5c.add(new ByteString(attestnCert));
  for (byte[] cert : this.caCert) {
    x5c.add(new ByteString(cert));
  }
  result.put(new UnicodeString("x5c"), x5c);
  result.put(new UnicodeString("sig"), new ByteString(sig));

  return result;
}
 
Example #5
Source Project: cbor-java   Author: c-rack   File: ArrayEncoder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void encode(Array array) throws CborException {
    List<DataItem> dataItems = array.getDataItems();
    if (array.isChunked()) {
        encodeTypeChunked(MajorType.ARRAY);
    } else {
        encodeTypeAndLength(MajorType.ARRAY, dataItems.size());
    }
    for (DataItem dataItem : dataItems) {
        encoder.encode(dataItem);
    }
}
 
Example #6
Source Project: cbor-java   Author: c-rack   File: CborDecoder.java    License: Apache License 2.0 5 votes vote down vote up
private DataItem decodeLanguageTaggedString(DataItem dataItem) throws CborException {
    if (!(dataItem instanceof Array)) {
        throw new CborException("Error decoding LanguageTaggedString: not an array");
    }

    Array array = (Array) dataItem;

    if (array.getDataItems().size() != 2) {
        throw new CborException("Error decoding LanguageTaggedString: array size is not 2");
    }

    DataItem languageDataItem = array.getDataItems().get(0);

    if (!(languageDataItem instanceof UnicodeString)) {
        throw new CborException("Error decoding LanguageTaggedString: first data item is not an UnicodeString");
    }

    DataItem stringDataItem = array.getDataItems().get(1);

    if (!(stringDataItem instanceof UnicodeString)) {
        throw new CborException("Error decoding LanguageTaggedString: second data item is not an UnicodeString");
    }

    UnicodeString language = (UnicodeString) languageDataItem;
    UnicodeString string = (UnicodeString) stringDataItem;

    return new LanguageTaggedString(language, string);
}
 
Example #7
Source Project: cbor-java   Author: c-rack   File: CborDecoder.java    License: Apache License 2.0 5 votes vote down vote up
private DataItem decodeRationalNumber(DataItem dataItem) throws CborException {
    if (!(dataItem instanceof Array)) {
        throw new CborException("Error decoding RationalNumber: not an array");
    }

    Array array = (Array) dataItem;

    if (array.getDataItems().size() != 2) {
        throw new CborException("Error decoding RationalNumber: array size is not 2");
    }

    DataItem numeratorDataItem = array.getDataItems().get(0);

    if (!(numeratorDataItem instanceof Number)) {
        throw new CborException("Error decoding RationalNumber: first data item is not a number");
    }

    DataItem denominatorDataItem = array.getDataItems().get(1);

    if (!(denominatorDataItem instanceof Number)) {
        throw new CborException("Error decoding RationalNumber: second data item is not a number");
    }

    Number numerator = (Number) numeratorDataItem;
    Number denominator = (Number) denominatorDataItem;

    return new RationalNumber(numerator, denominator);
}
 
Example #8
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 #9
Source Project: cbor-java   Author: c-rack   File: ArrayDecoder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Array decode(int initialByte) throws CborException {
    long length = getLength(initialByte);
    if (length == INFINITY) {
        return decodeInfinitiveLength();
    } else {
        return decodeFixedLength(length);
    }
}
 
Example #10
Source Project: cbor-java   Author: c-rack   File: ArrayDecoder.java    License: Apache License 2.0 5 votes vote down vote up
private Array decodeFixedLength(long length) throws CborException {
    Array array = new Array(getPreallocationSize(length));
    for (long i = 0; i < length; i++) {
        DataItem dataItem = decoder.decodeNext();
        if (dataItem == null) {
            throw new CborException("Unexpected end of stream");
        }
        array.add(dataItem);
    }
    return array;
}
 
Example #11
Source Project: cbor-java   Author: c-rack   File: Example63Test.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 Array);
    Array array = (Array) dataItem;
    Assert.assertTrue(array.getDataItems().isEmpty());
}
 
Example #12
Source Project: cbor-java   Author: c-rack   File: Example64Test.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 Array);
    Array array = (Array) dataItem;
    Assert.assertEquals(3, array.getDataItems().size());
    Assert.assertEquals(1, ((Number) array.getDataItems().get(0)).getValue().intValue());
    Assert.assertEquals(2, ((Number) array.getDataItems().get(1)).getValue().intValue());
    Assert.assertEquals(3, ((Number) array.getDataItems().get(2)).getValue().intValue());
}
 
Example #13
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 #14
Source Project: cbor-java   Author: c-rack   File: ArrayBuilderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAddFloat() {
    CborBuilder builder = new CborBuilder();
    List<DataItem> dataItems = builder.addArray().add(1.0f).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 HalfPrecisionFloat);
}
 
Example #15
Source Project: cbor-java   Author: c-rack   File: ArrayBuilderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAddDouble() {
    CborBuilder builder = new CborBuilder();
    List<DataItem> dataItems = builder.addArray().add(1.0d).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 DoublePrecisionFloat);
}
 
Example #16
Source Project: cbor-java   Author: c-rack   File: ArrayBuilderTest.java    License: Apache License 2.0 5 votes vote down vote up
@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 #17
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 #18
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 #19
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 #20
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 #21
Source Project: cbor-java   Author: c-rack   File: CborBuilder.java    License: Apache License 2.0 4 votes vote down vote up
public ArrayBuilder<CborBuilder> startArray() {
    Array array = new Array();
    array.setChunked(true);
    add(array);
    return new ArrayBuilder<CborBuilder>(this, array);
}
 
Example #22
Source Project: cbor-java   Author: c-rack   File: CborBuilder.java    License: Apache License 2.0 4 votes vote down vote up
public ArrayBuilder<CborBuilder> addArray() {
    Array array = new Array();
    add(array);
    return new ArrayBuilder<CborBuilder>(this, array);
}
 
Example #23
Source Project: cbor-java   Author: c-rack   File: MapBuilder.java    License: Apache License 2.0 4 votes vote down vote up
public ArrayBuilder<MapBuilder<T>> putArray(DataItem key) {
    Array array = new Array();
    put(key, array);
    return new ArrayBuilder<>(this, array);
}
 
Example #24
Source Project: cbor-java   Author: c-rack   File: MapBuilder.java    License: Apache License 2.0 4 votes vote down vote up
public ArrayBuilder<MapBuilder<T>> putArray(long key) {
    Array array = new Array();
    put(convert(key), array);
    return new ArrayBuilder<>(this, array);
}
 
Example #25
Source Project: cbor-java   Author: c-rack   File: MapBuilder.java    License: Apache License 2.0 4 votes vote down vote up
public ArrayBuilder<MapBuilder<T>> putArray(String key) {
    Array array = new Array();
    put(convert(key), array);
    return new ArrayBuilder<>(this, array);
}
 
Example #26
Source Project: cbor-java   Author: c-rack   File: MapBuilder.java    License: Apache License 2.0 4 votes vote down vote up
public ArrayBuilder<MapBuilder<T>> startArray(DataItem key) {
    Array array = new Array();
    array.setChunked(true);
    put(key, array);
    return new ArrayBuilder<>(this, array);
}
 
Example #27
Source Project: cbor-java   Author: c-rack   File: MapBuilder.java    License: Apache License 2.0 4 votes vote down vote up
public ArrayBuilder<MapBuilder<T>> startArray(String key) {
    Array array = new Array();
    array.setChunked(true);
    put(convert(key), array);
    return new ArrayBuilder<>(this, array);
}
 
Example #28
Source Project: cbor-java   Author: c-rack   File: ArrayBuilder.java    License: Apache License 2.0 4 votes vote down vote up
public ArrayBuilder(T parent, Array array) {
    super(parent);
    this.array = array;
}
 
Example #29
Source Project: cbor-java   Author: c-rack   File: ArrayBuilder.java    License: Apache License 2.0 4 votes vote down vote up
public ArrayBuilder<ArrayBuilder<T>> addArray() {
    Array nestedArray = new Array();
    add(nestedArray);
    return new ArrayBuilder<ArrayBuilder<T>>(this, nestedArray);
}
 
Example #30
Source Project: cbor-java   Author: c-rack   File: ArrayBuilder.java    License: Apache License 2.0 4 votes vote down vote up
public ArrayBuilder<ArrayBuilder<T>> startArray() {
    Array nestedArray = new Array();
    nestedArray.setChunked(true);
    add(nestedArray);
    return new ArrayBuilder<ArrayBuilder<T>>(this, nestedArray);
}