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

The following examples show how to use co.nstant.in.cbor.model.Array. 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: PackedAttestationStatement.java    From webauthndemo with 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 File: RationalNumberEncoderTest.java    From cbor-java with 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 #3
Source File: ArrayDecoder.java    From cbor-java with 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 #4
Source File: FidoU2fAttestationStatement.java    From webauthndemo with 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 File: ArrayBuilderTest.java    From cbor-java with 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 #6
Source File: ArrayBuilderTest.java    From cbor-java with 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 #7
Source File: ArrayBuilderTest.java    From cbor-java with 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 #8
Source File: ArrayBuilderTest.java    From cbor-java with 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 #9
Source File: Example64Test.java    From cbor-java with 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 #10
Source File: Example63Test.java    From cbor-java with 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 #11
Source File: ArrayDecoder.java    From cbor-java with 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 #12
Source File: ArrayDecoder.java    From cbor-java with 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 #13
Source File: CborDecoder.java    From cbor-java with 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 #14
Source File: CborDecoder.java    From cbor-java with 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 #15
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 #16
Source File: ArrayEncoder.java    From cbor-java with 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 #17
Source File: CBORTest.java    From SI with 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 File: Utils.java    From SI with 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 File: CBORTest.java    From SI with 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 File: Utils.java    From SI with 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 File: Example65Test.java    From cbor-java with Apache License 2.0 4 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());

    DataItem dataItem1 = array.getDataItems().get(0);
    DataItem dataItem2 = array.getDataItems().get(1);
    DataItem dataItem3 = array.getDataItems().get(2);

    Assert.assertTrue(dataItem1 instanceof Number);
    Assert.assertTrue(dataItem2 instanceof Array);
    Assert.assertTrue(dataItem3 instanceof Array);

    Number number = (Number) dataItem1;
    Array array1 = (Array) dataItem2;
    Array array2 = (Array) dataItem3;

    Assert.assertEquals(1, number.getValue().intValue());
    Assert.assertEquals(2, array1.getDataItems().size());
    Assert.assertEquals(2, array2.getDataItems().size());

    DataItem array1item1 = array1.getDataItems().get(0);
    DataItem array1item2 = array1.getDataItems().get(1);

    Assert.assertTrue(array1item1 instanceof Number);
    Assert.assertTrue(array1item2 instanceof Number);

    Number array1number1 = (Number) array1item1;
    Number array1number2 = (Number) array1item2;

    Assert.assertEquals(2, array1number1.getValue().intValue());
    Assert.assertEquals(3, array1number2.getValue().intValue());

    DataItem array2item1 = array2.getDataItems().get(0);
    DataItem array2item2 = array2.getDataItems().get(1);

    Assert.assertTrue(array2item1 instanceof Number);
    Assert.assertTrue(array2item2 instanceof Number);

    Number array2number1 = (Number) array2item1;
    Number array2number2 = (Number) array2item2;

    Assert.assertEquals(4, array2number1.getValue().intValue());
    Assert.assertEquals(5, array2number2.getValue().intValue());
}
 
Example #22
Source File: CborBuilder.java    From cbor-java with 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 #23
Source File: CborBuilder.java    From cbor-java with 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 #24
Source File: ArrayBuilder.java    From cbor-java with 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);
}
 
Example #25
Source File: ArrayBuilder.java    From cbor-java with 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 #26
Source File: ArrayBuilder.java    From cbor-java with Apache License 2.0 4 votes vote down vote up
public ArrayBuilder(T parent, Array array) {
    super(parent);
    this.array = array;
}
 
Example #27
Source File: MapBuilder.java    From cbor-java with 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 File: MapBuilder.java    From cbor-java with 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 #29
Source File: MapBuilder.java    From cbor-java with 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 #30
Source File: MapBuilder.java    From cbor-java with 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);
}