@Test public void testToBytes() throws Exception { assertArrayEquals(new byte[] {8, 7, 0, 24, 6, 0, 0, -1, -1, -1, 0, 0, 0, 0}, attributes.toBytes()); attributes.clear(); attributes.add(getAttribute(AttributeType.Unspecified, AttributeVariant.None, new FlagsFields.Flags(false, false, false, false, false))); exception.expect(AttributeVariantException.class); attributes.toBytes(); }
private static byte[] toBytes(Attribute attribute) throws AttributeVariantException { byte flags = ArraysUtil.convertBits(attribute.getFlags().isOptional(), attribute.getFlags().isNonTransitive(), attribute.getFlags().isPartial(), attribute.getFlags().isCompact(), attribute.getFlags().isExtendedLength(), false, false, false); byte[] value = attribute.getValue(); if (attribute.getAttributeVariant().equals(AttributeVariant.Compact)) { byte type = ArraysUtil.int2bytes(attribute.getType().getIntValue())[3]; byte length = ArraysUtil.int2bytes(attribute.getLength())[3]; if (Configuration.SET_COMPOSITION_ATTRIBUTE_COMPACT_NO_RESERVED_FIELDS) { return ArraysUtil.combine(new byte[] {flags, type, length}, value); } return ArraysUtil.combine(new byte[] {flags, type, length, 0x00}, value); } else if (attribute.getAttributeVariant().equals(AttributeVariant.CompactExtendedLength)) { byte type = ArraysUtil.int2bytes(attribute.getType().getIntValue())[3]; byte[] length = ArraysUtil.int2bytesCropp(attribute.getLength(), 2); return ArraysUtil.combine(new byte[] {flags, type}, length, value); } else if (attribute.getAttributeVariant().equals(AttributeVariant.NonCompact)) { byte[] type = ArraysUtil.int2bytesCropp(attribute.getType().getIntValue(), 1); byte[] length = ArraysUtil.int2bytes(attribute.getLength()); return ArraysUtil.combine(type, length, value); } else { throw new AttributeVariantException(); } }
AttributeList extends ArrayList<Attribute> { private static byte[] toBytes(Attribute attribute) throws AttributeVariantException { byte flags = ArraysUtil.convertBits(attribute.getFlags().isOptional(), attribute.getFlags().isNonTransitive(), attribute.getFlags().isPartial(), attribute.getFlags().isCompact(), attribute.getFlags().isExtendedLength(), false, false, false); byte[] value = attribute.getValue(); if (attribute.getAttributeVariant().equals(AttributeVariant.Compact)) { byte type = ArraysUtil.int2bytes(attribute.getType().getIntValue())[3]; byte length = ArraysUtil.int2bytes(attribute.getLength())[3]; if (Configuration.SET_COMPOSITION_ATTRIBUTE_COMPACT_NO_RESERVED_FIELDS) { return ArraysUtil.combine(new byte[] {flags, type, length}, value); } return ArraysUtil.combine(new byte[] {flags, type, length, 0x00}, value); } else if (attribute.getAttributeVariant().equals(AttributeVariant.CompactExtendedLength)) { byte type = ArraysUtil.int2bytes(attribute.getType().getIntValue())[3]; byte[] length = ArraysUtil.int2bytesCropp(attribute.getLength(), 2); return ArraysUtil.combine(new byte[] {flags, type}, length, value); } else if (attribute.getAttributeVariant().equals(AttributeVariant.NonCompact)) { byte[] type = ArraysUtil.int2bytesCropp(attribute.getType().getIntValue(), 1); byte[] length = ArraysUtil.int2bytes(attribute.getLength()); return ArraysUtil.combine(type, length, value); } else { throw new AttributeVariantException(); } } }
AttributeList extends ArrayList<Attribute> { private static byte[] toBytes(Attribute attribute) throws AttributeVariantException { byte flags = ArraysUtil.convertBits(attribute.getFlags().isOptional(), attribute.getFlags().isNonTransitive(), attribute.getFlags().isPartial(), attribute.getFlags().isCompact(), attribute.getFlags().isExtendedLength(), false, false, false); byte[] value = attribute.getValue(); if (attribute.getAttributeVariant().equals(AttributeVariant.Compact)) { byte type = ArraysUtil.int2bytes(attribute.getType().getIntValue())[3]; byte length = ArraysUtil.int2bytes(attribute.getLength())[3]; if (Configuration.SET_COMPOSITION_ATTRIBUTE_COMPACT_NO_RESERVED_FIELDS) { return ArraysUtil.combine(new byte[] {flags, type, length}, value); } return ArraysUtil.combine(new byte[] {flags, type, length, 0x00}, value); } else if (attribute.getAttributeVariant().equals(AttributeVariant.CompactExtendedLength)) { byte type = ArraysUtil.int2bytes(attribute.getType().getIntValue())[3]; byte[] length = ArraysUtil.int2bytesCropp(attribute.getLength(), 2); return ArraysUtil.combine(new byte[] {flags, type}, length, value); } else if (attribute.getAttributeVariant().equals(AttributeVariant.NonCompact)) { byte[] type = ArraysUtil.int2bytesCropp(attribute.getType().getIntValue(), 1); byte[] length = ArraysUtil.int2bytes(attribute.getLength()); return ArraysUtil.combine(type, length, value); } else { throw new AttributeVariantException(); } } AttributeList(); private  AttributeList(List<Attribute> attributes); }
AttributeList extends ArrayList<Attribute> { private static byte[] toBytes(Attribute attribute) throws AttributeVariantException { byte flags = ArraysUtil.convertBits(attribute.getFlags().isOptional(), attribute.getFlags().isNonTransitive(), attribute.getFlags().isPartial(), attribute.getFlags().isCompact(), attribute.getFlags().isExtendedLength(), false, false, false); byte[] value = attribute.getValue(); if (attribute.getAttributeVariant().equals(AttributeVariant.Compact)) { byte type = ArraysUtil.int2bytes(attribute.getType().getIntValue())[3]; byte length = ArraysUtil.int2bytes(attribute.getLength())[3]; if (Configuration.SET_COMPOSITION_ATTRIBUTE_COMPACT_NO_RESERVED_FIELDS) { return ArraysUtil.combine(new byte[] {flags, type, length}, value); } return ArraysUtil.combine(new byte[] {flags, type, length, 0x00}, value); } else if (attribute.getAttributeVariant().equals(AttributeVariant.CompactExtendedLength)) { byte type = ArraysUtil.int2bytes(attribute.getType().getIntValue())[3]; byte[] length = ArraysUtil.int2bytesCropp(attribute.getLength(), 2); return ArraysUtil.combine(new byte[] {flags, type}, length, value); } else if (attribute.getAttributeVariant().equals(AttributeVariant.NonCompact)) { byte[] type = ArraysUtil.int2bytesCropp(attribute.getType().getIntValue(), 1); byte[] length = ArraysUtil.int2bytes(attribute.getLength()); return ArraysUtil.combine(type, length, value); } else { throw new AttributeVariantException(); } } AttributeList(); private  AttributeList(List<Attribute> attributes); static AttributeList create(List<Attribute> list); static AttributeList decode(byte[] array); static AttributeOptionalFields get(List<Attribute> attributes, AttributeType type); AttributeOptionalFields get(AttributeType type); byte[] toBytes(); }
AttributeList extends ArrayList<Attribute> { private static byte[] toBytes(Attribute attribute) throws AttributeVariantException { byte flags = ArraysUtil.convertBits(attribute.getFlags().isOptional(), attribute.getFlags().isNonTransitive(), attribute.getFlags().isPartial(), attribute.getFlags().isCompact(), attribute.getFlags().isExtendedLength(), false, false, false); byte[] value = attribute.getValue(); if (attribute.getAttributeVariant().equals(AttributeVariant.Compact)) { byte type = ArraysUtil.int2bytes(attribute.getType().getIntValue())[3]; byte length = ArraysUtil.int2bytes(attribute.getLength())[3]; if (Configuration.SET_COMPOSITION_ATTRIBUTE_COMPACT_NO_RESERVED_FIELDS) { return ArraysUtil.combine(new byte[] {flags, type, length}, value); } return ArraysUtil.combine(new byte[] {flags, type, length, 0x00}, value); } else if (attribute.getAttributeVariant().equals(AttributeVariant.CompactExtendedLength)) { byte type = ArraysUtil.int2bytes(attribute.getType().getIntValue())[3]; byte[] length = ArraysUtil.int2bytesCropp(attribute.getLength(), 2); return ArraysUtil.combine(new byte[] {flags, type}, length, value); } else if (attribute.getAttributeVariant().equals(AttributeVariant.NonCompact)) { byte[] type = ArraysUtil.int2bytesCropp(attribute.getType().getIntValue(), 1); byte[] length = ArraysUtil.int2bytes(attribute.getLength()); return ArraysUtil.combine(type, length, value); } else { throw new AttributeVariantException(); } } AttributeList(); private  AttributeList(List<Attribute> attributes); static AttributeList create(List<Attribute> list); static AttributeList decode(byte[] array); static AttributeOptionalFields get(List<Attribute> attributes, AttributeType type); AttributeOptionalFields get(AttributeType type); byte[] toBytes();  }