target
stringlengths
20
113k
src_fm
stringlengths
11
86.3k
src_fm_fc
stringlengths
21
86.4k
src_fm_fc_co
stringlengths
30
86.4k
src_fm_fc_ms
stringlengths
42
86.8k
src_fm_fc_ms_ff
stringlengths
43
86.8k
@Test( expected = IllegalStateException.class ) public void testNoEnumConstantsFail() throws Exception { mapperFactory.mapType( ValuesEmpty.class, null ); fail( "Must fail" ); }
@SuppressWarnings( "unchecked" ) @Override public TypeMapper mapType( Type type, TypeMetadata metadata ) { if( !isSupportedFor( type ) ) throw new IllegalArgumentException( "Only enum types allowed" ); return mapEnum( (Class<Enum<?>>)type ); }
EnumTypeMapperFactory implements TypeMapperFactory { @SuppressWarnings( "unchecked" ) @Override public TypeMapper mapType( Type type, TypeMetadata metadata ) { if( !isSupportedFor( type ) ) throw new IllegalArgumentException( "Only enum types allowed" ); return mapEnum( (Class<Enum<?>>)type ); } }
EnumTypeMapperFactory implements TypeMapperFactory { @SuppressWarnings( "unchecked" ) @Override public TypeMapper mapType( Type type, TypeMetadata metadata ) { if( !isSupportedFor( type ) ) throw new IllegalArgumentException( "Only enum types allowed" ); return mapEnum( (Class<Enum<?>>)type ); } EnumTypeMapperFactory( TypeMapperContext context, Asn1Factory factory ); }
EnumTypeMapperFactory implements TypeMapperFactory { @SuppressWarnings( "unchecked" ) @Override public TypeMapper mapType( Type type, TypeMetadata metadata ) { if( !isSupportedFor( type ) ) throw new IllegalArgumentException( "Only enum types allowed" ); return mapEnum( (Class<Enum<?>>)type ); } EnumTypeMapperFactory( TypeMapperContext context, Asn1Factory factory ); @Override int getPriority(); @Override boolean isSupportedFor( Type type ); @SuppressWarnings( "unchecked" ) @Override TypeMapper mapType( Type type, TypeMetadata metadata ); }
EnumTypeMapperFactory implements TypeMapperFactory { @SuppressWarnings( "unchecked" ) @Override public TypeMapper mapType( Type type, TypeMetadata metadata ) { if( !isSupportedFor( type ) ) throw new IllegalArgumentException( "Only enum types allowed" ); return mapEnum( (Class<Enum<?>>)type ); } EnumTypeMapperFactory( TypeMapperContext context, Asn1Factory factory ); @Override int getPriority(); @Override boolean isSupportedFor( Type type ); @SuppressWarnings( "unchecked" ) @Override TypeMapper mapType( Type type, TypeMetadata metadata ); }
@Test public void testHugeTagNumber() throws Exception { TypeFactory factory = new CoreTypeFactory(); Module module = factory.dummyModule(); Scope scope = module.createScope(); Type tagged = factory.tagged( TagEncoding.application( 2048 ), UniversalType.INTEGER.ref() ); Type defined = factory.define( "MyTagged", tagged, null ); module.validate(); Value expected = new IntegerValueInt( 0 ); byte[] result = InputUtils.writeValue( scope, defined, expected ); try( ByteArrayInputStream is = new ByteArrayInputStream( result ); AbstractBerReader reader = new DefaultBerReader( is, new CoreValueFactory() ) ) { Value value = reader.read( scope, defined ); Assert.assertEquals( "Values are not equal", expected, value ); } }
@SuppressWarnings( "NumericCastThatLosesPrecision" ) @Override public byte read() throws IOException { int read = is.read(); if( read != -1 ) position++; return (byte)read; }
DefaultBerReader extends AbstractBerReader { @SuppressWarnings( "NumericCastThatLosesPrecision" ) @Override public byte read() throws IOException { int read = is.read(); if( read != -1 ) position++; return (byte)read; } }
DefaultBerReader extends AbstractBerReader { @SuppressWarnings( "NumericCastThatLosesPrecision" ) @Override public byte read() throws IOException { int read = is.read(); if( read != -1 ) position++; return (byte)read; } DefaultBerReader( InputStream is, ValueFactory valueFactory ); }
DefaultBerReader extends AbstractBerReader { @SuppressWarnings( "NumericCastThatLosesPrecision" ) @Override public byte read() throws IOException { int read = is.read(); if( read != -1 ) position++; return (byte)read; } DefaultBerReader( InputStream is, ValueFactory valueFactory ); @Override int position(); @Override void skip( int amount ); @Override void skipToEoc(); @SuppressWarnings( "NumericCastThatLosesPrecision" ) @Override byte read(); @Override int read( byte[] buffer ); @Override void close(); }
DefaultBerReader extends AbstractBerReader { @SuppressWarnings( "NumericCastThatLosesPrecision" ) @Override public byte read() throws IOException { int read = is.read(); if( read != -1 ) position++; return (byte)read; } DefaultBerReader( InputStream is, ValueFactory valueFactory ); @Override int position(); @Override void skip( int amount ); @Override void skipToEoc(); @SuppressWarnings( "NumericCastThatLosesPrecision" ) @Override byte read(); @Override int read( byte[] buffer ); @Override void close(); }
@Test( expected = IllegalStateException.class ) public void testIllegalIndexes() throws Exception { mapperFactory.mapType( IllegalEnum.class, null ); fail( "Must fail" ); }
@SuppressWarnings( "unchecked" ) @Override public TypeMapper mapType( Type type, TypeMetadata metadata ) { if( !isSupportedFor( type ) ) throw new IllegalArgumentException( "Only enum types allowed" ); return mapEnum( (Class<Enum<?>>)type ); }
EnumTypeMapperFactory implements TypeMapperFactory { @SuppressWarnings( "unchecked" ) @Override public TypeMapper mapType( Type type, TypeMetadata metadata ) { if( !isSupportedFor( type ) ) throw new IllegalArgumentException( "Only enum types allowed" ); return mapEnum( (Class<Enum<?>>)type ); } }
EnumTypeMapperFactory implements TypeMapperFactory { @SuppressWarnings( "unchecked" ) @Override public TypeMapper mapType( Type type, TypeMetadata metadata ) { if( !isSupportedFor( type ) ) throw new IllegalArgumentException( "Only enum types allowed" ); return mapEnum( (Class<Enum<?>>)type ); } EnumTypeMapperFactory( TypeMapperContext context, Asn1Factory factory ); }
EnumTypeMapperFactory implements TypeMapperFactory { @SuppressWarnings( "unchecked" ) @Override public TypeMapper mapType( Type type, TypeMetadata metadata ) { if( !isSupportedFor( type ) ) throw new IllegalArgumentException( "Only enum types allowed" ); return mapEnum( (Class<Enum<?>>)type ); } EnumTypeMapperFactory( TypeMapperContext context, Asn1Factory factory ); @Override int getPriority(); @Override boolean isSupportedFor( Type type ); @SuppressWarnings( "unchecked" ) @Override TypeMapper mapType( Type type, TypeMetadata metadata ); }
EnumTypeMapperFactory implements TypeMapperFactory { @SuppressWarnings( "unchecked" ) @Override public TypeMapper mapType( Type type, TypeMetadata metadata ) { if( !isSupportedFor( type ) ) throw new IllegalArgumentException( "Only enum types allowed" ); return mapEnum( (Class<Enum<?>>)type ); } EnumTypeMapperFactory( TypeMapperContext context, Asn1Factory factory ); @Override int getPriority(); @Override boolean isSupportedFor( Type type ); @SuppressWarnings( "unchecked" ) @Override TypeMapper mapType( Type type, TypeMetadata metadata ); }
@Test( expected = IllegalStateException.class ) public void testIllegalIndexes2() throws Exception { mapperFactory.mapType( IllegalEnum2.class, null ); fail( "Must fail" ); }
@SuppressWarnings( "unchecked" ) @Override public TypeMapper mapType( Type type, TypeMetadata metadata ) { if( !isSupportedFor( type ) ) throw new IllegalArgumentException( "Only enum types allowed" ); return mapEnum( (Class<Enum<?>>)type ); }
EnumTypeMapperFactory implements TypeMapperFactory { @SuppressWarnings( "unchecked" ) @Override public TypeMapper mapType( Type type, TypeMetadata metadata ) { if( !isSupportedFor( type ) ) throw new IllegalArgumentException( "Only enum types allowed" ); return mapEnum( (Class<Enum<?>>)type ); } }
EnumTypeMapperFactory implements TypeMapperFactory { @SuppressWarnings( "unchecked" ) @Override public TypeMapper mapType( Type type, TypeMetadata metadata ) { if( !isSupportedFor( type ) ) throw new IllegalArgumentException( "Only enum types allowed" ); return mapEnum( (Class<Enum<?>>)type ); } EnumTypeMapperFactory( TypeMapperContext context, Asn1Factory factory ); }
EnumTypeMapperFactory implements TypeMapperFactory { @SuppressWarnings( "unchecked" ) @Override public TypeMapper mapType( Type type, TypeMetadata metadata ) { if( !isSupportedFor( type ) ) throw new IllegalArgumentException( "Only enum types allowed" ); return mapEnum( (Class<Enum<?>>)type ); } EnumTypeMapperFactory( TypeMapperContext context, Asn1Factory factory ); @Override int getPriority(); @Override boolean isSupportedFor( Type type ); @SuppressWarnings( "unchecked" ) @Override TypeMapper mapType( Type type, TypeMetadata metadata ); }
EnumTypeMapperFactory implements TypeMapperFactory { @SuppressWarnings( "unchecked" ) @Override public TypeMapper mapType( Type type, TypeMetadata metadata ) { if( !isSupportedFor( type ) ) throw new IllegalArgumentException( "Only enum types allowed" ); return mapEnum( (Class<Enum<?>>)type ); } EnumTypeMapperFactory( TypeMapperContext context, Asn1Factory factory ); @Override int getPriority(); @Override boolean isSupportedFor( Type type ); @SuppressWarnings( "unchecked" ) @Override TypeMapper mapType( Type type, TypeMetadata metadata ); }
@Test public void testIntrospection() throws Exception { Introspector introspector = new Introspector(); JavaType type = introspector.introspect( Element.class ); int k = 0; }
@NotNull public JavaType introspect( Type type ) { if( typeMap.containsKey( type.getTypeName() ) ) return typeMap.get( type.getTypeName() ); if( type instanceof Class<?> ) return forClass( (Class<?>)type ); if( type instanceof ParameterizedType ) return forParameterized( (ParameterizedType)type ); if( type instanceof TypeVariable<?> ) { JavaType javaType = new JavaType( type ); typeMap.put( type.getTypeName(), javaType ); return javaType; } throw new UnsupportedOperationException(); }
Introspector { @NotNull public JavaType introspect( Type type ) { if( typeMap.containsKey( type.getTypeName() ) ) return typeMap.get( type.getTypeName() ); if( type instanceof Class<?> ) return forClass( (Class<?>)type ); if( type instanceof ParameterizedType ) return forParameterized( (ParameterizedType)type ); if( type instanceof TypeVariable<?> ) { JavaType javaType = new JavaType( type ); typeMap.put( type.getTypeName(), javaType ); return javaType; } throw new UnsupportedOperationException(); } }
Introspector { @NotNull public JavaType introspect( Type type ) { if( typeMap.containsKey( type.getTypeName() ) ) return typeMap.get( type.getTypeName() ); if( type instanceof Class<?> ) return forClass( (Class<?>)type ); if( type instanceof ParameterizedType ) return forParameterized( (ParameterizedType)type ); if( type instanceof TypeVariable<?> ) { JavaType javaType = new JavaType( type ); typeMap.put( type.getTypeName(), javaType ); return javaType; } throw new UnsupportedOperationException(); } }
Introspector { @NotNull public JavaType introspect( Type type ) { if( typeMap.containsKey( type.getTypeName() ) ) return typeMap.get( type.getTypeName() ); if( type instanceof Class<?> ) return forClass( (Class<?>)type ); if( type instanceof ParameterizedType ) return forParameterized( (ParameterizedType)type ); if( type instanceof TypeVariable<?> ) { JavaType javaType = new JavaType( type ); typeMap.put( type.getTypeName(), javaType ); return javaType; } throw new UnsupportedOperationException(); } @NotNull JavaType introspect( Type type ); }
Introspector { @NotNull public JavaType introspect( Type type ) { if( typeMap.containsKey( type.getTypeName() ) ) return typeMap.get( type.getTypeName() ); if( type instanceof Class<?> ) return forClass( (Class<?>)type ); if( type instanceof ParameterizedType ) return forParameterized( (ParameterizedType)type ); if( type instanceof TypeVariable<?> ) { JavaType javaType = new JavaType( type ); typeMap.put( type.getTypeName(), javaType ); return javaType; } throw new UnsupportedOperationException(); } @NotNull JavaType introspect( Type type ); }
@Test( expected = IllegalArgumentException.class ) public void toAsn1Fails() throws Exception { TypeMapper mapper = new RealTypeMapper( double.class, REAL ); mapper.toAsn1( FACTORY, true ); fail( "Must fail" ); }
@NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( isFloat() && isAssignableToFloat( value ) ) return factory.real( (Float)value ); if( isDouble() && isAssignableToDouble( value ) ) return factory.real( (Double)value ); if( isBigDecimal() && Objects.equals( value.getClass(), BigDecimal.class ) ) return factory.real( (BigDecimal)value ); throw new IllegalArgumentException( "Unable to convert value: " + value ); }
RealTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( isFloat() && isAssignableToFloat( value ) ) return factory.real( (Float)value ); if( isDouble() && isAssignableToDouble( value ) ) return factory.real( (Double)value ); if( isBigDecimal() && Objects.equals( value.getClass(), BigDecimal.class ) ) return factory.real( (BigDecimal)value ); throw new IllegalArgumentException( "Unable to convert value: " + value ); } }
RealTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( isFloat() && isAssignableToFloat( value ) ) return factory.real( (Float)value ); if( isDouble() && isAssignableToDouble( value ) ) return factory.real( (Double)value ); if( isBigDecimal() && Objects.equals( value.getClass(), BigDecimal.class ) ) return factory.real( (BigDecimal)value ); throw new IllegalArgumentException( "Unable to convert value: " + value ); } RealTypeMapper( Class<?> realClass, NamedType asnType ); }
RealTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( isFloat() && isAssignableToFloat( value ) ) return factory.real( (Float)value ); if( isDouble() && isAssignableToDouble( value ) ) return factory.real( (Double)value ); if( isBigDecimal() && Objects.equals( value.getClass(), BigDecimal.class ) ) return factory.real( (BigDecimal)value ); throw new IllegalArgumentException( "Unable to convert value: " + value ); } RealTypeMapper( Class<?> realClass, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
RealTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( isFloat() && isAssignableToFloat( value ) ) return factory.real( (Float)value ); if( isDouble() && isAssignableToDouble( value ) ) return factory.real( (Double)value ); if( isBigDecimal() && Objects.equals( value.getClass(), BigDecimal.class ) ) return factory.real( (BigDecimal)value ); throw new IllegalArgumentException( "Unable to convert value: " + value ); } RealTypeMapper( Class<?> realClass, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
@Test( expected = IllegalArgumentException.class ) public void toJavaFails() throws Exception { TypeMapper mapper = new RealTypeMapper( double.class, REAL ); mapper.toJava( BooleanValue.TRUE ); fail( "Must fail" ); }
@NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.REAL ) throw new IllegalArgumentException( "Unable to handle value of kind: " + value.getKind() ); RealValue rv = value.toRealValue(); if( isFloat() ) return rv.asFloat(); if( isDouble() ) return rv.asDouble(); if( isBigDecimal() ) return rv.asBigDecimal(); throw new UnsupportedOperationException(); }
RealTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.REAL ) throw new IllegalArgumentException( "Unable to handle value of kind: " + value.getKind() ); RealValue rv = value.toRealValue(); if( isFloat() ) return rv.asFloat(); if( isDouble() ) return rv.asDouble(); if( isBigDecimal() ) return rv.asBigDecimal(); throw new UnsupportedOperationException(); } }
RealTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.REAL ) throw new IllegalArgumentException( "Unable to handle value of kind: " + value.getKind() ); RealValue rv = value.toRealValue(); if( isFloat() ) return rv.asFloat(); if( isDouble() ) return rv.asDouble(); if( isBigDecimal() ) return rv.asBigDecimal(); throw new UnsupportedOperationException(); } RealTypeMapper( Class<?> realClass, NamedType asnType ); }
RealTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.REAL ) throw new IllegalArgumentException( "Unable to handle value of kind: " + value.getKind() ); RealValue rv = value.toRealValue(); if( isFloat() ) return rv.asFloat(); if( isDouble() ) return rv.asDouble(); if( isBigDecimal() ) return rv.asBigDecimal(); throw new UnsupportedOperationException(); } RealTypeMapper( Class<?> realClass, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
RealTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.REAL ) throw new IllegalArgumentException( "Unable to handle value of kind: " + value.getKind() ); RealValue rv = value.toRealValue(); if( isFloat() ) return rv.asFloat(); if( isDouble() ) return rv.asDouble(); if( isBigDecimal() ) return rv.asBigDecimal(); throw new UnsupportedOperationException(); } RealTypeMapper( Class<?> realClass, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
@Test( expected = IllegalArgumentException.class ) public void toAsn1Fails() throws Exception { TypeMapper mapper = new StringTypeMapper( String.class, UTF8_STRING ); mapper.toAsn1( FACTORY, 1L ); fail( "Must fail" ); }
@NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !javaType.isAssignableFrom( value.getClass() ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); return factory.cString( (String)value ); }
StringTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !javaType.isAssignableFrom( value.getClass() ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); return factory.cString( (String)value ); } }
StringTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !javaType.isAssignableFrom( value.getClass() ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); return factory.cString( (String)value ); } StringTypeMapper( Class<?> javaType, NamedType asnType ); }
StringTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !javaType.isAssignableFrom( value.getClass() ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); return factory.cString( (String)value ); } StringTypeMapper( Class<?> javaType, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
StringTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !javaType.isAssignableFrom( value.getClass() ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); return factory.cString( (String)value ); } StringTypeMapper( Class<?> javaType, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
@Test( expected = IllegalArgumentException.class ) public void toJavaFails() throws Exception { TypeMapper mapper = new StringTypeMapper( String.class, UTF8_STRING ); mapper.toJava( BooleanValue.TRUE ); fail( "Must fail" ); }
@NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.C_STRING ) throw new IllegalArgumentException( "Unable to convert value of kind: " + value.getKind() ); return value.toStringValue().asString(); }
StringTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.C_STRING ) throw new IllegalArgumentException( "Unable to convert value of kind: " + value.getKind() ); return value.toStringValue().asString(); } }
StringTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.C_STRING ) throw new IllegalArgumentException( "Unable to convert value of kind: " + value.getKind() ); return value.toStringValue().asString(); } StringTypeMapper( Class<?> javaType, NamedType asnType ); }
StringTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.C_STRING ) throw new IllegalArgumentException( "Unable to convert value of kind: " + value.getKind() ); return value.toStringValue().asString(); } StringTypeMapper( Class<?> javaType, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
StringTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.C_STRING ) throw new IllegalArgumentException( "Unable to convert value of kind: " + value.getKind() ); return value.toStringValue().asString(); } StringTypeMapper( Class<?> javaType, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
@Test( expected = IllegalArgumentException.class ) public void toAsn1Fails() throws Exception { TypeMapper mapper = new IntegerTypeMapper( Long.class, INTEGER ); mapper.toAsn1( FACTORY, true ); fail( "Must fail" ); }
@NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( isByte() && isAssignableToByte( value ) ) return factory.integer( (Byte)value ); if( isShort() && isAssignableToShort( value ) ) return factory.integer( (Short)value ); if( isInteger() && isAssignableToInt( value ) ) return factory.integer( (Integer)value ); if( isLong() && isAssignableToLong( value ) ) return factory.integer( (Long)value ); if( isBigInteger() && Objects.equals( value.getClass(), BigInteger.class ) ) return factory.integer( (BigInteger)value ); throw new IllegalArgumentException( "Unable to convert value: " + value ); }
IntegerTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( isByte() && isAssignableToByte( value ) ) return factory.integer( (Byte)value ); if( isShort() && isAssignableToShort( value ) ) return factory.integer( (Short)value ); if( isInteger() && isAssignableToInt( value ) ) return factory.integer( (Integer)value ); if( isLong() && isAssignableToLong( value ) ) return factory.integer( (Long)value ); if( isBigInteger() && Objects.equals( value.getClass(), BigInteger.class ) ) return factory.integer( (BigInteger)value ); throw new IllegalArgumentException( "Unable to convert value: " + value ); } }
IntegerTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( isByte() && isAssignableToByte( value ) ) return factory.integer( (Byte)value ); if( isShort() && isAssignableToShort( value ) ) return factory.integer( (Short)value ); if( isInteger() && isAssignableToInt( value ) ) return factory.integer( (Integer)value ); if( isLong() && isAssignableToLong( value ) ) return factory.integer( (Long)value ); if( isBigInteger() && Objects.equals( value.getClass(), BigInteger.class ) ) return factory.integer( (BigInteger)value ); throw new IllegalArgumentException( "Unable to convert value: " + value ); } IntegerTypeMapper( Class<?> integerClass, NamedType asnType ); }
IntegerTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( isByte() && isAssignableToByte( value ) ) return factory.integer( (Byte)value ); if( isShort() && isAssignableToShort( value ) ) return factory.integer( (Short)value ); if( isInteger() && isAssignableToInt( value ) ) return factory.integer( (Integer)value ); if( isLong() && isAssignableToLong( value ) ) return factory.integer( (Long)value ); if( isBigInteger() && Objects.equals( value.getClass(), BigInteger.class ) ) return factory.integer( (BigInteger)value ); throw new IllegalArgumentException( "Unable to convert value: " + value ); } IntegerTypeMapper( Class<?> integerClass, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
IntegerTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( isByte() && isAssignableToByte( value ) ) return factory.integer( (Byte)value ); if( isShort() && isAssignableToShort( value ) ) return factory.integer( (Short)value ); if( isInteger() && isAssignableToInt( value ) ) return factory.integer( (Integer)value ); if( isLong() && isAssignableToLong( value ) ) return factory.integer( (Long)value ); if( isBigInteger() && Objects.equals( value.getClass(), BigInteger.class ) ) return factory.integer( (BigInteger)value ); throw new IllegalArgumentException( "Unable to convert value: " + value ); } IntegerTypeMapper( Class<?> integerClass, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
@Test( expected = IllegalArgumentException.class ) public void toJavaFails() throws Exception { TypeMapper mapper = new IntegerTypeMapper( Long.class, INTEGER ); mapper.toJava( BooleanValue.TRUE ); fail( "Must fail" ); }
@NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.INTEGER ) throw new IllegalArgumentException( "Unable to convert to integer value of kind: " + value.getKind() ); IntegerValue iv = value.toIntegerValue(); if( isByte() ) return (byte)iv.asInt(); if( isShort() ) return (short)iv.asInt(); if( isInteger() ) return iv.asInt(); if( isLong() ) return iv.asLong(); if( isBigInteger() ) return iv.asBigInteger(); throw new UnsupportedOperationException(); }
IntegerTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.INTEGER ) throw new IllegalArgumentException( "Unable to convert to integer value of kind: " + value.getKind() ); IntegerValue iv = value.toIntegerValue(); if( isByte() ) return (byte)iv.asInt(); if( isShort() ) return (short)iv.asInt(); if( isInteger() ) return iv.asInt(); if( isLong() ) return iv.asLong(); if( isBigInteger() ) return iv.asBigInteger(); throw new UnsupportedOperationException(); } }
IntegerTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.INTEGER ) throw new IllegalArgumentException( "Unable to convert to integer value of kind: " + value.getKind() ); IntegerValue iv = value.toIntegerValue(); if( isByte() ) return (byte)iv.asInt(); if( isShort() ) return (short)iv.asInt(); if( isInteger() ) return iv.asInt(); if( isLong() ) return iv.asLong(); if( isBigInteger() ) return iv.asBigInteger(); throw new UnsupportedOperationException(); } IntegerTypeMapper( Class<?> integerClass, NamedType asnType ); }
IntegerTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.INTEGER ) throw new IllegalArgumentException( "Unable to convert to integer value of kind: " + value.getKind() ); IntegerValue iv = value.toIntegerValue(); if( isByte() ) return (byte)iv.asInt(); if( isShort() ) return (short)iv.asInt(); if( isInteger() ) return iv.asInt(); if( isLong() ) return iv.asLong(); if( isBigInteger() ) return iv.asBigInteger(); throw new UnsupportedOperationException(); } IntegerTypeMapper( Class<?> integerClass, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
IntegerTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.INTEGER ) throw new IllegalArgumentException( "Unable to convert to integer value of kind: " + value.getKind() ); IntegerValue iv = value.toIntegerValue(); if( isByte() ) return (byte)iv.asInt(); if( isShort() ) return (short)iv.asInt(); if( isInteger() ) return iv.asInt(); if( isLong() ) return iv.asLong(); if( isBigInteger() ) return iv.asBigInteger(); throw new UnsupportedOperationException(); } IntegerTypeMapper( Class<?> integerClass, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
@Test( expected = IllegalArgumentException.class ) public void toAsn1Fails() throws Exception { TypeMapper mapper = new ByteArrayTypeMapper( byte[].class, OCTET_STRING ); mapper.toAsn1( FACTORY, 1L ); fail( "Must fail" ); }
@NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !byte[].class.isAssignableFrom( value.getClass() ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); byte[] array = (byte[])value; return factory.byteArrayValue( array.length * 8, array ); }
ByteArrayTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !byte[].class.isAssignableFrom( value.getClass() ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); byte[] array = (byte[])value; return factory.byteArrayValue( array.length * 8, array ); } }
ByteArrayTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !byte[].class.isAssignableFrom( value.getClass() ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); byte[] array = (byte[])value; return factory.byteArrayValue( array.length * 8, array ); } ByteArrayTypeMapper( Class<?> javaType, NamedType asnType ); }
ByteArrayTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !byte[].class.isAssignableFrom( value.getClass() ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); byte[] array = (byte[])value; return factory.byteArrayValue( array.length * 8, array ); } ByteArrayTypeMapper( Class<?> javaType, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
ByteArrayTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !byte[].class.isAssignableFrom( value.getClass() ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); byte[] array = (byte[])value; return factory.byteArrayValue( array.length * 8, array ); } ByteArrayTypeMapper( Class<?> javaType, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
@Test( expected = AssertionError.class ) public void testDecode_fail_type() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.INTEGER.ref().resolve( scope ); try( AbstractBerReader reader = mock( DefaultBerReader.class ) ) { Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); new BitStringBerDecoder().decode( new ReaderContext( reader, scope, type, tag, -1, false ) ); fail( "Must fail" ); } }
@Override public Value decode( @NotNull ReaderContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.BIT_STRING; assert !context.getTag().isConstructed(); if( context.getLength() == 0 ) return context.getValueFactory().emptyByteArray(); byte unusedBits = context.read(); if( unusedBits < 0 || unusedBits > 7 ) throw new IllegalValueException( "Unused bits must be in range: [0,7]" ); if( context.getLength() == -1 ) return OctetStringBerDecoder.readByteArrayValueIndefinite( context.getReader(), unusedBits ); return OctetStringBerDecoder.readByteArrayValue( context.getReader(), context.getLength() - 1, unusedBits ); }
BitStringBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.BIT_STRING; assert !context.getTag().isConstructed(); if( context.getLength() == 0 ) return context.getValueFactory().emptyByteArray(); byte unusedBits = context.read(); if( unusedBits < 0 || unusedBits > 7 ) throw new IllegalValueException( "Unused bits must be in range: [0,7]" ); if( context.getLength() == -1 ) return OctetStringBerDecoder.readByteArrayValueIndefinite( context.getReader(), unusedBits ); return OctetStringBerDecoder.readByteArrayValue( context.getReader(), context.getLength() - 1, unusedBits ); } }
BitStringBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.BIT_STRING; assert !context.getTag().isConstructed(); if( context.getLength() == 0 ) return context.getValueFactory().emptyByteArray(); byte unusedBits = context.read(); if( unusedBits < 0 || unusedBits > 7 ) throw new IllegalValueException( "Unused bits must be in range: [0,7]" ); if( context.getLength() == -1 ) return OctetStringBerDecoder.readByteArrayValueIndefinite( context.getReader(), unusedBits ); return OctetStringBerDecoder.readByteArrayValue( context.getReader(), context.getLength() - 1, unusedBits ); } }
BitStringBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.BIT_STRING; assert !context.getTag().isConstructed(); if( context.getLength() == 0 ) return context.getValueFactory().emptyByteArray(); byte unusedBits = context.read(); if( unusedBits < 0 || unusedBits > 7 ) throw new IllegalValueException( "Unused bits must be in range: [0,7]" ); if( context.getLength() == -1 ) return OctetStringBerDecoder.readByteArrayValueIndefinite( context.getReader(), unusedBits ); return OctetStringBerDecoder.readByteArrayValue( context.getReader(), context.getLength() - 1, unusedBits ); } @Override Value decode( @NotNull ReaderContext context ); }
BitStringBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.BIT_STRING; assert !context.getTag().isConstructed(); if( context.getLength() == 0 ) return context.getValueFactory().emptyByteArray(); byte unusedBits = context.read(); if( unusedBits < 0 || unusedBits > 7 ) throw new IllegalValueException( "Unused bits must be in range: [0,7]" ); if( context.getLength() == -1 ) return OctetStringBerDecoder.readByteArrayValueIndefinite( context.getReader(), unusedBits ); return OctetStringBerDecoder.readByteArrayValue( context.getReader(), context.getLength() - 1, unusedBits ); } @Override Value decode( @NotNull ReaderContext context ); }
@Test( expected = IllegalArgumentException.class ) public void toJavaFails() throws Exception { TypeMapper mapper = new ByteArrayTypeMapper( byte[].class, OCTET_STRING ); mapper.toJava( BooleanValue.TRUE ); fail( "Must fail" ); }
@NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.BYTE_ARRAY ) throw new IllegalArgumentException( "Unable to handle value of kind: " + value.getKind() ); return value.toByteArrayValue().asByteArray(); }
ByteArrayTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.BYTE_ARRAY ) throw new IllegalArgumentException( "Unable to handle value of kind: " + value.getKind() ); return value.toByteArrayValue().asByteArray(); } }
ByteArrayTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.BYTE_ARRAY ) throw new IllegalArgumentException( "Unable to handle value of kind: " + value.getKind() ); return value.toByteArrayValue().asByteArray(); } ByteArrayTypeMapper( Class<?> javaType, NamedType asnType ); }
ByteArrayTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.BYTE_ARRAY ) throw new IllegalArgumentException( "Unable to handle value of kind: " + value.getKind() ); return value.toByteArrayValue().asByteArray(); } ByteArrayTypeMapper( Class<?> javaType, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
ByteArrayTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.BYTE_ARRAY ) throw new IllegalArgumentException( "Unable to handle value of kind: " + value.getKind() ); return value.toByteArrayValue().asByteArray(); } ByteArrayTypeMapper( Class<?> javaType, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
@Test( expected = IllegalArgumentException.class ) public void toAsn1Fails() throws Exception { TypeMapper mapper = new DateTypeMapper( Instant.class, G_TIME ); mapper.toAsn1( FACTORY, 1L ); fail( "Must fail" ); }
@NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !javaType.isAssignableFrom( value.getClass() ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); if( isDate() ) return factory.timeValue( ( (Date)value ).toInstant() ); if( isInstant() ) return factory.timeValue( (Instant)value ); throw new UnsupportedOperationException(); }
DateTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !javaType.isAssignableFrom( value.getClass() ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); if( isDate() ) return factory.timeValue( ( (Date)value ).toInstant() ); if( isInstant() ) return factory.timeValue( (Instant)value ); throw new UnsupportedOperationException(); } }
DateTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !javaType.isAssignableFrom( value.getClass() ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); if( isDate() ) return factory.timeValue( ( (Date)value ).toInstant() ); if( isInstant() ) return factory.timeValue( (Instant)value ); throw new UnsupportedOperationException(); } DateTypeMapper( Class<?> javaType, NamedType asnType ); }
DateTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !javaType.isAssignableFrom( value.getClass() ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); if( isDate() ) return factory.timeValue( ( (Date)value ).toInstant() ); if( isInstant() ) return factory.timeValue( (Instant)value ); throw new UnsupportedOperationException(); } DateTypeMapper( Class<?> javaType, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
DateTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !javaType.isAssignableFrom( value.getClass() ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); if( isDate() ) return factory.timeValue( ( (Date)value ).toInstant() ); if( isInstant() ) return factory.timeValue( (Instant)value ); throw new UnsupportedOperationException(); } DateTypeMapper( Class<?> javaType, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
@Test( expected = IllegalArgumentException.class ) public void toJavaFails() throws Exception { TypeMapper mapper = new DateTypeMapper( Instant.class, G_TIME ); mapper.toJava( BooleanValue.TRUE ); fail( "Must fail" ); }
@NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.TIME ) throw new IllegalArgumentException( "Unable to convert values of kind: " + value.getKind() ); Instant instant = value.toDateValue().asInstant(); if( isInstant() ) return instant; if( isDate() ) return Date.from( instant ); throw new UnsupportedOperationException(); }
DateTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.TIME ) throw new IllegalArgumentException( "Unable to convert values of kind: " + value.getKind() ); Instant instant = value.toDateValue().asInstant(); if( isInstant() ) return instant; if( isDate() ) return Date.from( instant ); throw new UnsupportedOperationException(); } }
DateTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.TIME ) throw new IllegalArgumentException( "Unable to convert values of kind: " + value.getKind() ); Instant instant = value.toDateValue().asInstant(); if( isInstant() ) return instant; if( isDate() ) return Date.from( instant ); throw new UnsupportedOperationException(); } DateTypeMapper( Class<?> javaType, NamedType asnType ); }
DateTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.TIME ) throw new IllegalArgumentException( "Unable to convert values of kind: " + value.getKind() ); Instant instant = value.toDateValue().asInstant(); if( isInstant() ) return instant; if( isDate() ) return Date.from( instant ); throw new UnsupportedOperationException(); } DateTypeMapper( Class<?> javaType, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
DateTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.TIME ) throw new IllegalArgumentException( "Unable to convert values of kind: " + value.getKind() ); Instant instant = value.toDateValue().asInstant(); if( isInstant() ) return instant; if( isDate() ) return Date.from( instant ); throw new UnsupportedOperationException(); } DateTypeMapper( Class<?> javaType, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
@Test public void toAsn1() throws Exception { BooleanTypeMapper mapper = new BooleanTypeMapper( boolean.class, BOOLEAN ); assertEquals( "Not equals", BooleanValue.TRUE, mapper.toAsn1( FACTORY, true ) ); assertEquals( "Not equals", BooleanValue.FALSE, mapper.toAsn1( FACTORY, false ) ); }
@NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !isBooleanClass( value ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); if( Boolean.TRUE.equals( value ) ) return BooleanValue.TRUE; if( Boolean.FALSE.equals( value ) ) return BooleanValue.FALSE; throw new UnsupportedOperationException(); }
BooleanTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !isBooleanClass( value ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); if( Boolean.TRUE.equals( value ) ) return BooleanValue.TRUE; if( Boolean.FALSE.equals( value ) ) return BooleanValue.FALSE; throw new UnsupportedOperationException(); } }
BooleanTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !isBooleanClass( value ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); if( Boolean.TRUE.equals( value ) ) return BooleanValue.TRUE; if( Boolean.FALSE.equals( value ) ) return BooleanValue.FALSE; throw new UnsupportedOperationException(); } BooleanTypeMapper( Class<?> booleanClass, NamedType asnType ); }
BooleanTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !isBooleanClass( value ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); if( Boolean.TRUE.equals( value ) ) return BooleanValue.TRUE; if( Boolean.FALSE.equals( value ) ) return BooleanValue.FALSE; throw new UnsupportedOperationException(); } BooleanTypeMapper( Class<?> booleanClass, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
BooleanTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !isBooleanClass( value ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); if( Boolean.TRUE.equals( value ) ) return BooleanValue.TRUE; if( Boolean.FALSE.equals( value ) ) return BooleanValue.FALSE; throw new UnsupportedOperationException(); } BooleanTypeMapper( Class<?> booleanClass, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
@Test( expected = IllegalArgumentException.class ) public void toAsn1Fails() throws Exception { BooleanTypeMapper mapper = new BooleanTypeMapper( boolean.class, BOOLEAN ); mapper.toAsn1( FACTORY, 1L ); fail( "Must fail" ); }
@NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !isBooleanClass( value ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); if( Boolean.TRUE.equals( value ) ) return BooleanValue.TRUE; if( Boolean.FALSE.equals( value ) ) return BooleanValue.FALSE; throw new UnsupportedOperationException(); }
BooleanTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !isBooleanClass( value ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); if( Boolean.TRUE.equals( value ) ) return BooleanValue.TRUE; if( Boolean.FALSE.equals( value ) ) return BooleanValue.FALSE; throw new UnsupportedOperationException(); } }
BooleanTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !isBooleanClass( value ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); if( Boolean.TRUE.equals( value ) ) return BooleanValue.TRUE; if( Boolean.FALSE.equals( value ) ) return BooleanValue.FALSE; throw new UnsupportedOperationException(); } BooleanTypeMapper( Class<?> booleanClass, NamedType asnType ); }
BooleanTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !isBooleanClass( value ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); if( Boolean.TRUE.equals( value ) ) return BooleanValue.TRUE; if( Boolean.FALSE.equals( value ) ) return BooleanValue.FALSE; throw new UnsupportedOperationException(); } BooleanTypeMapper( Class<?> booleanClass, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
BooleanTypeMapper implements TypeMapper { @NotNull @Override public Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ) { if( !isBooleanClass( value ) ) throw new IllegalArgumentException( "Unable to convert value: " + value ); if( Boolean.TRUE.equals( value ) ) return BooleanValue.TRUE; if( Boolean.FALSE.equals( value ) ) return BooleanValue.FALSE; throw new UnsupportedOperationException(); } BooleanTypeMapper( Class<?> booleanClass, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
@Test public void toJava() throws Exception { BooleanTypeMapper mapper = new BooleanTypeMapper( boolean.class, BOOLEAN ); assertEquals( "Not equals", Boolean.TRUE, mapper.toJava( BooleanValue.TRUE ) ); assertEquals( "Not equals", Boolean.FALSE, mapper.toJava( BooleanValue.FALSE ) ); }
@NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.BOOLEAN ) throw new IllegalArgumentException( "Unable to handle value of kind: " + value.getKind() ); if( Objects.equals( value, BooleanValue.TRUE ) ) return Boolean.TRUE; if( Objects.equals( value, BooleanValue.FALSE ) ) return Boolean.FALSE; throw new UnsupportedOperationException(); }
BooleanTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.BOOLEAN ) throw new IllegalArgumentException( "Unable to handle value of kind: " + value.getKind() ); if( Objects.equals( value, BooleanValue.TRUE ) ) return Boolean.TRUE; if( Objects.equals( value, BooleanValue.FALSE ) ) return Boolean.FALSE; throw new UnsupportedOperationException(); } }
BooleanTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.BOOLEAN ) throw new IllegalArgumentException( "Unable to handle value of kind: " + value.getKind() ); if( Objects.equals( value, BooleanValue.TRUE ) ) return Boolean.TRUE; if( Objects.equals( value, BooleanValue.FALSE ) ) return Boolean.FALSE; throw new UnsupportedOperationException(); } BooleanTypeMapper( Class<?> booleanClass, NamedType asnType ); }
BooleanTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.BOOLEAN ) throw new IllegalArgumentException( "Unable to handle value of kind: " + value.getKind() ); if( Objects.equals( value, BooleanValue.TRUE ) ) return Boolean.TRUE; if( Objects.equals( value, BooleanValue.FALSE ) ) return Boolean.FALSE; throw new UnsupportedOperationException(); } BooleanTypeMapper( Class<?> booleanClass, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
BooleanTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.BOOLEAN ) throw new IllegalArgumentException( "Unable to handle value of kind: " + value.getKind() ); if( Objects.equals( value, BooleanValue.TRUE ) ) return Boolean.TRUE; if( Objects.equals( value, BooleanValue.FALSE ) ) return Boolean.FALSE; throw new UnsupportedOperationException(); } BooleanTypeMapper( Class<?> booleanClass, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
@Test( expected = IllegalArgumentException.class ) public void toJavaFails() throws Exception { BooleanTypeMapper mapper = new BooleanTypeMapper( boolean.class, BOOLEAN ); mapper.toJava( FACTORY.integer( 1 ) ); fail( "Must fail" ); }
@NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.BOOLEAN ) throw new IllegalArgumentException( "Unable to handle value of kind: " + value.getKind() ); if( Objects.equals( value, BooleanValue.TRUE ) ) return Boolean.TRUE; if( Objects.equals( value, BooleanValue.FALSE ) ) return Boolean.FALSE; throw new UnsupportedOperationException(); }
BooleanTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.BOOLEAN ) throw new IllegalArgumentException( "Unable to handle value of kind: " + value.getKind() ); if( Objects.equals( value, BooleanValue.TRUE ) ) return Boolean.TRUE; if( Objects.equals( value, BooleanValue.FALSE ) ) return Boolean.FALSE; throw new UnsupportedOperationException(); } }
BooleanTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.BOOLEAN ) throw new IllegalArgumentException( "Unable to handle value of kind: " + value.getKind() ); if( Objects.equals( value, BooleanValue.TRUE ) ) return Boolean.TRUE; if( Objects.equals( value, BooleanValue.FALSE ) ) return Boolean.FALSE; throw new UnsupportedOperationException(); } BooleanTypeMapper( Class<?> booleanClass, NamedType asnType ); }
BooleanTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.BOOLEAN ) throw new IllegalArgumentException( "Unable to handle value of kind: " + value.getKind() ); if( Objects.equals( value, BooleanValue.TRUE ) ) return Boolean.TRUE; if( Objects.equals( value, BooleanValue.FALSE ) ) return Boolean.FALSE; throw new UnsupportedOperationException(); } BooleanTypeMapper( Class<?> booleanClass, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
BooleanTypeMapper implements TypeMapper { @NotNull @Override public Object toJava( @NotNull Value value ) { if( value.getKind() != Kind.BOOLEAN ) throw new IllegalArgumentException( "Unable to handle value of kind: " + value.getKind() ); if( Objects.equals( value, BooleanValue.TRUE ) ) return Boolean.TRUE; if( Objects.equals( value, BooleanValue.FALSE ) ) return Boolean.FALSE; throw new UnsupportedOperationException(); } BooleanTypeMapper( Class<?> booleanClass, NamedType asnType ); @Override Type getJavaType(); @Override NamedType getAsn1Type(); @NotNull @Override Value toAsn1( @NotNull ValueFactory factory, @NotNull Object value ); @NotNull @Override Object toJava( @NotNull Value value ); }
@Test public void testWrite_Der() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.GENERALIZED_TIME.ref().resolve( scope ); Value value = new DateValueImpl( TimeUtils.parseGeneralizedTime( TIME_VALUE ) ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { when( writer.getRules() ).thenReturn( BerRules.DER ); new GeneralizedTimeBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).getRules(); verify( writer ).writeHeader( TAG, 15 ); verify( writer ).write( new byte[]{0x32, 0x30, 0x31, 0x37, 0x30, 0x36, 0x30, 0x31, 0x31, 0x31, 0x35, 0x37, 0x30, 0x30, 0x5A} ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.GENERALIZED_TIME; assert context.getValue().getKind() == Kind.TIME; Instant instant = context.getValue().toDateValue().asInstant(); boolean optimize = context.getRules() != BerRules.DER; String content = TimeUtils.formatInstant( instant, TimeUtils.GENERALIZED_TIME_FORMAT, optimize ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); }
GeneralizedTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.GENERALIZED_TIME; assert context.getValue().getKind() == Kind.TIME; Instant instant = context.getValue().toDateValue().asInstant(); boolean optimize = context.getRules() != BerRules.DER; String content = TimeUtils.formatInstant( instant, TimeUtils.GENERALIZED_TIME_FORMAT, optimize ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } }
GeneralizedTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.GENERALIZED_TIME; assert context.getValue().getKind() == Kind.TIME; Instant instant = context.getValue().toDateValue().asInstant(); boolean optimize = context.getRules() != BerRules.DER; String content = TimeUtils.formatInstant( instant, TimeUtils.GENERALIZED_TIME_FORMAT, optimize ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } }
GeneralizedTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.GENERALIZED_TIME; assert context.getValue().getKind() == Kind.TIME; Instant instant = context.getValue().toDateValue().asInstant(); boolean optimize = context.getRules() != BerRules.DER; String content = TimeUtils.formatInstant( instant, TimeUtils.GENERALIZED_TIME_FORMAT, optimize ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
GeneralizedTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.GENERALIZED_TIME; assert context.getValue().getKind() == Kind.TIME; Instant instant = context.getValue().toDateValue().asInstant(); boolean optimize = context.getRules() != BerRules.DER; String content = TimeUtils.formatInstant( instant, TimeUtils.GENERALIZED_TIME_FORMAT, optimize ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWrite_Ber() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.GENERALIZED_TIME.ref().resolve( scope ); Value value = new DateValueImpl( TimeUtils.parseGeneralizedTime( TIME_VALUE ) ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { when( writer.getRules() ).thenReturn( BerRules.BER ); new GeneralizedTimeBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).getRules(); verify( writer ).writeHeader( TAG, 13 ); verify( writer ).write( new byte[]{0x32, 0x30, 0x31, 0x37, 0x30, 0x36, 0x30, 0x31, 0x31, 0x31, 0x35, 0x37, 0x5A} ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.GENERALIZED_TIME; assert context.getValue().getKind() == Kind.TIME; Instant instant = context.getValue().toDateValue().asInstant(); boolean optimize = context.getRules() != BerRules.DER; String content = TimeUtils.formatInstant( instant, TimeUtils.GENERALIZED_TIME_FORMAT, optimize ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); }
GeneralizedTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.GENERALIZED_TIME; assert context.getValue().getKind() == Kind.TIME; Instant instant = context.getValue().toDateValue().asInstant(); boolean optimize = context.getRules() != BerRules.DER; String content = TimeUtils.formatInstant( instant, TimeUtils.GENERALIZED_TIME_FORMAT, optimize ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } }
GeneralizedTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.GENERALIZED_TIME; assert context.getValue().getKind() == Kind.TIME; Instant instant = context.getValue().toDateValue().asInstant(); boolean optimize = context.getRules() != BerRules.DER; String content = TimeUtils.formatInstant( instant, TimeUtils.GENERALIZED_TIME_FORMAT, optimize ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } }
GeneralizedTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.GENERALIZED_TIME; assert context.getValue().getKind() == Kind.TIME; Instant instant = context.getValue().toDateValue().asInstant(); boolean optimize = context.getRules() != BerRules.DER; String content = TimeUtils.formatInstant( instant, TimeUtils.GENERALIZED_TIME_FORMAT, optimize ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
GeneralizedTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.GENERALIZED_TIME; assert context.getValue().getKind() == Kind.TIME; Instant instant = context.getValue().toDateValue().asInstant(); boolean optimize = context.getRules() != BerRules.DER; String content = TimeUtils.formatInstant( instant, TimeUtils.GENERALIZED_TIME_FORMAT, optimize ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testEncode_fail_type() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.INTEGER.ref().resolve( scope ); Value value = new DateValueImpl( TimeUtils.parseGeneralizedTime( TIME_VALUE ) ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new GeneralizedTimeBerEncoder().encode( new WriterContext( writer, scope, type, value, false ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.GENERALIZED_TIME; assert context.getValue().getKind() == Kind.TIME; Instant instant = context.getValue().toDateValue().asInstant(); boolean optimize = context.getRules() != BerRules.DER; String content = TimeUtils.formatInstant( instant, TimeUtils.GENERALIZED_TIME_FORMAT, optimize ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); }
GeneralizedTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.GENERALIZED_TIME; assert context.getValue().getKind() == Kind.TIME; Instant instant = context.getValue().toDateValue().asInstant(); boolean optimize = context.getRules() != BerRules.DER; String content = TimeUtils.formatInstant( instant, TimeUtils.GENERALIZED_TIME_FORMAT, optimize ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } }
GeneralizedTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.GENERALIZED_TIME; assert context.getValue().getKind() == Kind.TIME; Instant instant = context.getValue().toDateValue().asInstant(); boolean optimize = context.getRules() != BerRules.DER; String content = TimeUtils.formatInstant( instant, TimeUtils.GENERALIZED_TIME_FORMAT, optimize ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } }
GeneralizedTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.GENERALIZED_TIME; assert context.getValue().getKind() == Kind.TIME; Instant instant = context.getValue().toDateValue().asInstant(); boolean optimize = context.getRules() != BerRules.DER; String content = TimeUtils.formatInstant( instant, TimeUtils.GENERALIZED_TIME_FORMAT, optimize ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
GeneralizedTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.GENERALIZED_TIME; assert context.getValue().getKind() == Kind.TIME; Instant instant = context.getValue().toDateValue().asInstant(); boolean optimize = context.getRules() != BerRules.DER; String content = TimeUtils.formatInstant( instant, TimeUtils.GENERALIZED_TIME_FORMAT, optimize ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testEncode_fail_value() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.GENERALIZED_TIME.ref().resolve( scope ); Value value = BooleanValue.TRUE; try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new GeneralizedTimeBerEncoder().encode( new WriterContext( writer, scope, type, value, false ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.GENERALIZED_TIME; assert context.getValue().getKind() == Kind.TIME; Instant instant = context.getValue().toDateValue().asInstant(); boolean optimize = context.getRules() != BerRules.DER; String content = TimeUtils.formatInstant( instant, TimeUtils.GENERALIZED_TIME_FORMAT, optimize ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); }
GeneralizedTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.GENERALIZED_TIME; assert context.getValue().getKind() == Kind.TIME; Instant instant = context.getValue().toDateValue().asInstant(); boolean optimize = context.getRules() != BerRules.DER; String content = TimeUtils.formatInstant( instant, TimeUtils.GENERALIZED_TIME_FORMAT, optimize ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } }
GeneralizedTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.GENERALIZED_TIME; assert context.getValue().getKind() == Kind.TIME; Instant instant = context.getValue().toDateValue().asInstant(); boolean optimize = context.getRules() != BerRules.DER; String content = TimeUtils.formatInstant( instant, TimeUtils.GENERALIZED_TIME_FORMAT, optimize ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } }
GeneralizedTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.GENERALIZED_TIME; assert context.getValue().getKind() == Kind.TIME; Instant instant = context.getValue().toDateValue().asInstant(); boolean optimize = context.getRules() != BerRules.DER; String content = TimeUtils.formatInstant( instant, TimeUtils.GENERALIZED_TIME_FORMAT, optimize ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
GeneralizedTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.GENERALIZED_TIME; assert context.getValue().getKind() == Kind.TIME; Instant instant = context.getValue().toDateValue().asInstant(); boolean optimize = context.getRules() != BerRules.DER; String content = TimeUtils.formatInstant( instant, TimeUtils.GENERALIZED_TIME_FORMAT, optimize ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testEncode_0() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.UTF8_STRING.ref().resolve( scope ); Value value = new StringValueImpl( "A" ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new StringBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).writeHeader( TAG, 1 ); verify( writer ).write( new byte[]{65} ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.RESTRICTED_STRING; assert context.getValue().getKind() == Kind.C_STRING; Type type = context.getType(); while( !( type instanceof StringType ) ) { assert type != null; type = type.getSibling(); } Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); byte[] bytes = context.getValue().toStringValue().asString().getBytes( ( (StringType)type ).getCharset() ); context.writeHeader( tag, bytes.length ); context.write( bytes ); }
StringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.RESTRICTED_STRING; assert context.getValue().getKind() == Kind.C_STRING; Type type = context.getType(); while( !( type instanceof StringType ) ) { assert type != null; type = type.getSibling(); } Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); byte[] bytes = context.getValue().toStringValue().asString().getBytes( ( (StringType)type ).getCharset() ); context.writeHeader( tag, bytes.length ); context.write( bytes ); } }
StringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.RESTRICTED_STRING; assert context.getValue().getKind() == Kind.C_STRING; Type type = context.getType(); while( !( type instanceof StringType ) ) { assert type != null; type = type.getSibling(); } Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); byte[] bytes = context.getValue().toStringValue().asString().getBytes( ( (StringType)type ).getCharset() ); context.writeHeader( tag, bytes.length ); context.write( bytes ); } }
StringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.RESTRICTED_STRING; assert context.getValue().getKind() == Kind.C_STRING; Type type = context.getType(); while( !( type instanceof StringType ) ) { assert type != null; type = type.getSibling(); } Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); byte[] bytes = context.getValue().toStringValue().asString().getBytes( ( (StringType)type ).getCharset() ); context.writeHeader( tag, bytes.length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
StringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.RESTRICTED_STRING; assert context.getValue().getKind() == Kind.C_STRING; Type type = context.getType(); while( !( type instanceof StringType ) ) { assert type != null; type = type.getSibling(); } Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); byte[] bytes = context.getValue().toStringValue().asString().getBytes( ( (StringType)type ).getCharset() ); context.writeHeader( tag, bytes.length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testEncode_fail_type() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.INTEGER.ref().resolve( scope ); Value value = new StringValueImpl( "Value" ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new StringBerEncoder().encode( new WriterContext( writer, scope, type, value, false ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.RESTRICTED_STRING; assert context.getValue().getKind() == Kind.C_STRING; Type type = context.getType(); while( !( type instanceof StringType ) ) { assert type != null; type = type.getSibling(); } Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); byte[] bytes = context.getValue().toStringValue().asString().getBytes( ( (StringType)type ).getCharset() ); context.writeHeader( tag, bytes.length ); context.write( bytes ); }
StringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.RESTRICTED_STRING; assert context.getValue().getKind() == Kind.C_STRING; Type type = context.getType(); while( !( type instanceof StringType ) ) { assert type != null; type = type.getSibling(); } Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); byte[] bytes = context.getValue().toStringValue().asString().getBytes( ( (StringType)type ).getCharset() ); context.writeHeader( tag, bytes.length ); context.write( bytes ); } }
StringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.RESTRICTED_STRING; assert context.getValue().getKind() == Kind.C_STRING; Type type = context.getType(); while( !( type instanceof StringType ) ) { assert type != null; type = type.getSibling(); } Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); byte[] bytes = context.getValue().toStringValue().asString().getBytes( ( (StringType)type ).getCharset() ); context.writeHeader( tag, bytes.length ); context.write( bytes ); } }
StringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.RESTRICTED_STRING; assert context.getValue().getKind() == Kind.C_STRING; Type type = context.getType(); while( !( type instanceof StringType ) ) { assert type != null; type = type.getSibling(); } Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); byte[] bytes = context.getValue().toStringValue().asString().getBytes( ( (StringType)type ).getCharset() ); context.writeHeader( tag, bytes.length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
StringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.RESTRICTED_STRING; assert context.getValue().getKind() == Kind.C_STRING; Type type = context.getType(); while( !( type instanceof StringType ) ) { assert type != null; type = type.getSibling(); } Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); byte[] bytes = context.getValue().toStringValue().asString().getBytes( ( (StringType)type ).getCharset() ); context.writeHeader( tag, bytes.length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testDecode_fail_type() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.INTEGER.ref().resolve( scope ); try( AbstractBerReader reader = mock( DefaultBerReader.class ) ) { Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); new ObjectIDBerDecoder().decode( new ReaderContext( reader, scope, type, tag, -1, false ) ); fail( "Must fail" ); } }
@Override public Value decode( @NotNull ReaderContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getLength() > 0; List<Ref<Value>> list = new ArrayList<>(); int length = context.getLength(); while( length > 0 ) length = readObjectIDItem( context.getReader(), length, list ); ObjectIdentifierValue objectIdentifierValue = context.getValueFactory().objectIdentifier( list ); return context.getType().optimize( context.getScope(), objectIdentifierValue ); }
ObjectIDBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getLength() > 0; List<Ref<Value>> list = new ArrayList<>(); int length = context.getLength(); while( length > 0 ) length = readObjectIDItem( context.getReader(), length, list ); ObjectIdentifierValue objectIdentifierValue = context.getValueFactory().objectIdentifier( list ); return context.getType().optimize( context.getScope(), objectIdentifierValue ); } }
ObjectIDBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getLength() > 0; List<Ref<Value>> list = new ArrayList<>(); int length = context.getLength(); while( length > 0 ) length = readObjectIDItem( context.getReader(), length, list ); ObjectIdentifierValue objectIdentifierValue = context.getValueFactory().objectIdentifier( list ); return context.getType().optimize( context.getScope(), objectIdentifierValue ); } }
ObjectIDBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getLength() > 0; List<Ref<Value>> list = new ArrayList<>(); int length = context.getLength(); while( length > 0 ) length = readObjectIDItem( context.getReader(), length, list ); ObjectIdentifierValue objectIdentifierValue = context.getValueFactory().objectIdentifier( list ); return context.getType().optimize( context.getScope(), objectIdentifierValue ); } @Override Value decode( @NotNull ReaderContext context ); }
ObjectIDBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getLength() > 0; List<Ref<Value>> list = new ArrayList<>(); int length = context.getLength(); while( length > 0 ) length = readObjectIDItem( context.getReader(), length, list ); ObjectIdentifierValue objectIdentifierValue = context.getValueFactory().objectIdentifier( list ); return context.getType().optimize( context.getScope(), objectIdentifierValue ); } @Override Value decode( @NotNull ReaderContext context ); }
@Test( expected = AssertionError.class ) public void testEncode_fail_value() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.UTF8_STRING.ref().resolve( scope ); Value value = BooleanValue.TRUE; try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new StringBerEncoder().encode( new WriterContext( writer, scope, type, value, false ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.RESTRICTED_STRING; assert context.getValue().getKind() == Kind.C_STRING; Type type = context.getType(); while( !( type instanceof StringType ) ) { assert type != null; type = type.getSibling(); } Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); byte[] bytes = context.getValue().toStringValue().asString().getBytes( ( (StringType)type ).getCharset() ); context.writeHeader( tag, bytes.length ); context.write( bytes ); }
StringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.RESTRICTED_STRING; assert context.getValue().getKind() == Kind.C_STRING; Type type = context.getType(); while( !( type instanceof StringType ) ) { assert type != null; type = type.getSibling(); } Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); byte[] bytes = context.getValue().toStringValue().asString().getBytes( ( (StringType)type ).getCharset() ); context.writeHeader( tag, bytes.length ); context.write( bytes ); } }
StringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.RESTRICTED_STRING; assert context.getValue().getKind() == Kind.C_STRING; Type type = context.getType(); while( !( type instanceof StringType ) ) { assert type != null; type = type.getSibling(); } Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); byte[] bytes = context.getValue().toStringValue().asString().getBytes( ( (StringType)type ).getCharset() ); context.writeHeader( tag, bytes.length ); context.write( bytes ); } }
StringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.RESTRICTED_STRING; assert context.getValue().getKind() == Kind.C_STRING; Type type = context.getType(); while( !( type instanceof StringType ) ) { assert type != null; type = type.getSibling(); } Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); byte[] bytes = context.getValue().toStringValue().asString().getBytes( ( (StringType)type ).getCharset() ); context.writeHeader( tag, bytes.length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
StringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.RESTRICTED_STRING; assert context.getValue().getKind() == Kind.C_STRING; Type type = context.getType(); while( !( type instanceof StringType ) ) { assert type != null; type = type.getSibling(); } Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); byte[] bytes = context.getValue().toStringValue().asString().getBytes( ( (StringType)type ).getCharset() ); context.writeHeader( tag, bytes.length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWriteSequenceOf_Buffered() throws Exception { Scope scope = CoreModule.getInstance().createScope(); SequenceOfType type = new SequenceOfType(); type.setComponent( "a", UniversalType.INTEGER.ref() ); type.validate( scope ); ComponentType component = type.getComponentType(); Assert.assertNotNull( "No component a", component ); ValueCollection value = new ValueCollectionImpl( false ); Value valueInt = new IntegerValueInt( 0 ); value.add( valueInt ); try( AbstractBerWriter writer = new DefaultBerWriter( BerRules.DER ) ) { new SequenceOfBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); byte[] bytes = writer.toByteArray(); Assert.assertArrayEquals( "Arrays are not equal", new byte[]{0x30, 0x03, 0x02, 0x01, 0x00}, bytes ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWriteSequenceOf_NonBuffered() throws Exception { Scope scope = CoreModule.getInstance().createScope(); SequenceOfType type = new SequenceOfType(); type.setComponent( "a", UniversalType.INTEGER.ref() ); type.validate( scope ); ComponentType component = type.getComponentType(); Assert.assertNotNull( "No component a", component ); ValueCollection value = new ValueCollectionImpl( false ); Value valueInt = new IntegerValueInt( 0 ); value.add( valueInt ); try( AbstractBerWriter writer = new DefaultBerWriter( BerRules.BER ) ) { new SequenceOfBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) { @Override public boolean isBufferingAvailable() { return false; } } ); byte[] bytes = writer.toByteArray(); Assert.assertArrayEquals( "Arrays are not equal", new byte[]{0x30, (byte)0x80, 0x02, 0x01, 0x00, 0x00, 0x00}, bytes ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = Asn1Exception.class ) public void testWriteSequenceOf_NonBuffered_Der() throws Exception { Scope scope = CoreModule.getInstance().createScope(); SequenceOfType type = new SequenceOfType(); type.setComponent( "a", UniversalType.INTEGER.ref() ); type.validate( scope ); ComponentType component = type.getComponentType(); Assert.assertNotNull( "No component a", component ); ValueCollection value = new ValueCollectionImpl( false ); Value valueInt = new IntegerValueInt( 0 ); value.add( valueInt ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { when( writer.isBufferingAvailable() ).thenReturn( false ); when( writer.getRules() ).thenReturn( BerRules.DER ); new SequenceOfBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWriteSequenceOf_NoHeader() throws Exception { Scope scope = CoreModule.getInstance().createScope(); SequenceOfType type = new SequenceOfType(); type.setComponent( "a", UniversalType.INTEGER.ref() ); type.validate( scope ); ComponentType component = type.getComponentType(); Assert.assertNotNull( "No component a", component ); ValueCollection value = new ValueCollectionImpl( false ); Value valueInt = new IntegerValueInt( 0 ); value.add( valueInt ); try( AbstractBerWriter writer = new DefaultBerWriter( BerRules.DER ) ) { new SequenceOfBerEncoder().encode( new WriterContext( writer, scope, type, value, false ) ); byte[] bytes = writer.toByteArray(); Assert.assertArrayEquals( "Arrays are not equal", new byte[]{0x02, 0x01, 0x00}, bytes ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testEncode_fail_type() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.INTEGER.ref().resolve( scope ); Value value = new ValueCollectionImpl( true ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new SequenceOfBerEncoder().encode( new WriterContext( writer, scope, type, value, false ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testEncode_fail_value() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = new SequenceOfType(); Value value = BooleanValue.TRUE; try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new SequenceOfBerEncoder().encode( new WriterContext( writer, scope, type, value, false ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
SequenceOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SEQUENCE_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeCollection( context ); context.stopBuffer( SequenceBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SequenceBerEncoder.TAG, -1 ); writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWrite_0101_Der() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.BIT_STRING.ref().resolve( scope ); Value value = CoreUtils.byteArrayFromBitString( VALUE_0101B ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { when( writer.getRules() ).thenReturn( BerRules.DER ); new BitStringBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).getRules(); verify( writer ).writeHeader( TAG, 2 ); verify( writer ).write( 4 ); verify( writer ).write( new byte[]{0x50} ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } @Override void encode( @NotNull WriterContext context ); }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWrite_0101_Ber() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.BIT_STRING.ref().resolve( scope ); Value value = CoreUtils.byteArrayFromBitString( VALUE_0101B ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { when( writer.getRules() ).thenReturn( BerRules.BER ); new BitStringBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).getRules(); verify( writer ).writeHeader( TAG, 2 ); verify( writer ).write( 4 ); verify( writer ).write( new byte[]{0x50} ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } @Override void encode( @NotNull WriterContext context ); }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWrite_01010000000000_Der_noNamed() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.BIT_STRING.ref().resolve( scope ); Value value = CoreUtils.byteArrayFromBitString( VALUE_01010000000000B ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { when( writer.getRules() ).thenReturn( BerRules.DER ); new BitStringBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).getRules(); verify( writer ).writeHeader( TAG, 3 ); verify( writer ).write( 2 ); verify( writer ).write( new byte[]{0x50, 0x0} ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } @Override void encode( @NotNull WriterContext context ); }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testDecode_fail_type() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.INTEGER.ref().resolve( scope ); try( AbstractBerReader reader = mock( DefaultBerReader.class ) ) { Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); new BooleanBerDecoder().decode( new ReaderContext( reader, scope, type, tag, 1, false ) ); fail( "Must fail" ); } }
@Override public Value decode( @NotNull ReaderContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getLength() == 1; byte content = context.read(); return content == BerUtils.BOOLEAN_FALSE ? BooleanValue.FALSE : BooleanValue.TRUE; }
BooleanBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getLength() == 1; byte content = context.read(); return content == BerUtils.BOOLEAN_FALSE ? BooleanValue.FALSE : BooleanValue.TRUE; } }
BooleanBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getLength() == 1; byte content = context.read(); return content == BerUtils.BOOLEAN_FALSE ? BooleanValue.FALSE : BooleanValue.TRUE; } }
BooleanBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getLength() == 1; byte content = context.read(); return content == BerUtils.BOOLEAN_FALSE ? BooleanValue.FALSE : BooleanValue.TRUE; } @Override Value decode( @NotNull ReaderContext context ); }
BooleanBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getLength() == 1; byte content = context.read(); return content == BerUtils.BOOLEAN_FALSE ? BooleanValue.FALSE : BooleanValue.TRUE; } @Override Value decode( @NotNull ReaderContext context ); }
@Test public void testWrite_01010000000000_Der_hasNamed() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = new BitStringType( Arrays.asList( new NamedValueImpl( "a", new IntegerValueInt( 1 ) ), new NamedValueImpl( "b", new IntegerValueInt( 2 ) ), new NamedValueImpl( "c", new IntegerValueInt( 3 ) ), new NamedValueImpl( "d", new IntegerValueInt( 4 ) ) ) ); type.validate( scope ); Value value = CoreUtils.byteArrayFromBitString( VALUE_01010000000000B ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { when( writer.getRules() ).thenReturn( BerRules.DER ); new BitStringBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).getRules(); verify( writer ).writeHeader( TAG, 2 ); verify( writer ).write( 4 ); verify( writer ).write( new byte[]{0x50} ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } @Override void encode( @NotNull WriterContext context ); }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWrite_0000_Der_hasNamed() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = new BitStringType( Arrays.asList( new NamedValueImpl( "a", new IntegerValueInt( 1 ) ), new NamedValueImpl( "b", new IntegerValueInt( 2 ) ), new NamedValueImpl( "c", new IntegerValueInt( 3 ) ), new NamedValueImpl( "d", new IntegerValueInt( 4 ) ) ) ); type.validate( scope ); Value value = CoreUtils.byteArrayFromBitString( VALUE_0000B ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { when( writer.getRules() ).thenReturn( BerRules.DER ); new BitStringBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).getRules(); verify( writer ).writeHeader( TAG, 2 ); verify( writer ).write( 1 ); verify( writer ).write( new byte[]{0x0} ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } @Override void encode( @NotNull WriterContext context ); }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWrite_0000_Ber() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.BIT_STRING.ref().resolve( scope ); Value value = CoreUtils.byteArrayFromBitString( VALUE_0000B ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { when( writer.getRules() ).thenReturn( BerRules.BER ); new BitStringBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).getRules(); verify( writer ).writeHeader( TAG, 2 ); verify( writer ).write( 4 ); verify( writer ).write( new byte[]{0x0} ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } @Override void encode( @NotNull WriterContext context ); }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWrite_Empty_Ber() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.BIT_STRING.ref().resolve( scope ); Value value = CoreUtils.byteArrayFromBitString( "''B" ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { when( writer.getRules() ).thenReturn( BerRules.BER ); new BitStringBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).getRules(); verify( writer ).writeHeader( TAG, 1 ); verify( writer ).write( 0 ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } @Override void encode( @NotNull WriterContext context ); }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWrite_01010000000000_Ber() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.BIT_STRING.ref().resolve( scope ); Value value = CoreUtils.byteArrayFromBitString( VALUE_01010000000000B ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { when( writer.getRules() ).thenReturn( BerRules.BER ); new BitStringBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).getRules(); verify( writer ).writeHeader( TAG, 3 ); verify( writer ).write( 2 ); verify( writer ).write( new byte[]{0x50, 0x0} ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } @Override void encode( @NotNull WriterContext context ); }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testEncode_fail_type() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.INTEGER.ref().resolve( scope ); Value value = CoreUtils.byteArrayFromBitString( VALUE_0101B ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new BitStringBerEncoder().encode( new WriterContext( writer, scope, type, value, false ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } @Override void encode( @NotNull WriterContext context ); }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testEncode_fail_value() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.BIT_STRING.ref().resolve( scope ); Value value = BooleanValue.TRUE; try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new BitStringBerEncoder().encode( new WriterContext( writer, scope, type, value, false ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } @Override void encode( @NotNull WriterContext context ); }
BitStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BIT_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; ByteArrayValue arrayValue = context.getValue().toByteArrayValue(); boolean hasNamedValues = !context.getType().getNamedValues().isEmpty(); byte[] bytes = arrayValue.asByteArray(); int usedBits = arrayValue.getUsedBits(); int emptyBits = bytes.length * 8 - usedBits; if( context.getRules() == BerRules.DER ) { boolean hasSizeConstraint = Boolean.TRUE.equals( context.getScope().getScopeOption( ConstraintUtils.OPTION_HAS_SIZE_CONSTRAINT ) ); if( !hasSizeConstraint && hasNamedValues ) { if( hasEmptyBytesAtEnd( bytes ) ) bytes = removeZerosFromEnd( bytes ); if( bytes.length > 0 ) emptyBits = getTrailingZerosCount( bytes[bytes.length - 1] ); } } int length = bytes.length == 0 ? 1 : 1 + bytes.length; context.writeHeader( TAG, length ); if( length > 1 ) { context.write( emptyBits ); context.write( bytes ); } else context.write( 0 ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testEncode_true() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.BOOLEAN.ref().resolve( scope ); Value value = BooleanValue.TRUE; try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new BooleanBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).writeHeader( TAG, 1 ); verify( writer ).write( BerUtils.BOOLEAN_TRUE ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getValue().getKind() == Kind.BOOLEAN; context.writeHeader( TAG, 1 ); boolean value = context.getValue().toBooleanValue().asBoolean(); context.write( value ? BerUtils.BOOLEAN_TRUE : BerUtils.BOOLEAN_FALSE ); }
BooleanBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getValue().getKind() == Kind.BOOLEAN; context.writeHeader( TAG, 1 ); boolean value = context.getValue().toBooleanValue().asBoolean(); context.write( value ? BerUtils.BOOLEAN_TRUE : BerUtils.BOOLEAN_FALSE ); } }
BooleanBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getValue().getKind() == Kind.BOOLEAN; context.writeHeader( TAG, 1 ); boolean value = context.getValue().toBooleanValue().asBoolean(); context.write( value ? BerUtils.BOOLEAN_TRUE : BerUtils.BOOLEAN_FALSE ); } }
BooleanBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getValue().getKind() == Kind.BOOLEAN; context.writeHeader( TAG, 1 ); boolean value = context.getValue().toBooleanValue().asBoolean(); context.write( value ? BerUtils.BOOLEAN_TRUE : BerUtils.BOOLEAN_FALSE ); } @Override void encode( @NotNull WriterContext context ); }
BooleanBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getValue().getKind() == Kind.BOOLEAN; context.writeHeader( TAG, 1 ); boolean value = context.getValue().toBooleanValue().asBoolean(); context.write( value ? BerUtils.BOOLEAN_TRUE : BerUtils.BOOLEAN_FALSE ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testEncode_false() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.BOOLEAN.ref().resolve( scope ); Value value = BooleanValue.FALSE; try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new BooleanBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).writeHeader( TAG, 1 ); verify( writer ).write( BerUtils.BOOLEAN_FALSE ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getValue().getKind() == Kind.BOOLEAN; context.writeHeader( TAG, 1 ); boolean value = context.getValue().toBooleanValue().asBoolean(); context.write( value ? BerUtils.BOOLEAN_TRUE : BerUtils.BOOLEAN_FALSE ); }
BooleanBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getValue().getKind() == Kind.BOOLEAN; context.writeHeader( TAG, 1 ); boolean value = context.getValue().toBooleanValue().asBoolean(); context.write( value ? BerUtils.BOOLEAN_TRUE : BerUtils.BOOLEAN_FALSE ); } }
BooleanBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getValue().getKind() == Kind.BOOLEAN; context.writeHeader( TAG, 1 ); boolean value = context.getValue().toBooleanValue().asBoolean(); context.write( value ? BerUtils.BOOLEAN_TRUE : BerUtils.BOOLEAN_FALSE ); } }
BooleanBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getValue().getKind() == Kind.BOOLEAN; context.writeHeader( TAG, 1 ); boolean value = context.getValue().toBooleanValue().asBoolean(); context.write( value ? BerUtils.BOOLEAN_TRUE : BerUtils.BOOLEAN_FALSE ); } @Override void encode( @NotNull WriterContext context ); }
BooleanBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getValue().getKind() == Kind.BOOLEAN; context.writeHeader( TAG, 1 ); boolean value = context.getValue().toBooleanValue().asBoolean(); context.write( value ? BerUtils.BOOLEAN_TRUE : BerUtils.BOOLEAN_FALSE ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testEncode_fail_type() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.INTEGER.ref().resolve( scope ); Value value = BooleanValue.TRUE; try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new BooleanBerEncoder().encode( new WriterContext( writer, scope, type, value, false ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getValue().getKind() == Kind.BOOLEAN; context.writeHeader( TAG, 1 ); boolean value = context.getValue().toBooleanValue().asBoolean(); context.write( value ? BerUtils.BOOLEAN_TRUE : BerUtils.BOOLEAN_FALSE ); }
BooleanBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getValue().getKind() == Kind.BOOLEAN; context.writeHeader( TAG, 1 ); boolean value = context.getValue().toBooleanValue().asBoolean(); context.write( value ? BerUtils.BOOLEAN_TRUE : BerUtils.BOOLEAN_FALSE ); } }
BooleanBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getValue().getKind() == Kind.BOOLEAN; context.writeHeader( TAG, 1 ); boolean value = context.getValue().toBooleanValue().asBoolean(); context.write( value ? BerUtils.BOOLEAN_TRUE : BerUtils.BOOLEAN_FALSE ); } }
BooleanBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getValue().getKind() == Kind.BOOLEAN; context.writeHeader( TAG, 1 ); boolean value = context.getValue().toBooleanValue().asBoolean(); context.write( value ? BerUtils.BOOLEAN_TRUE : BerUtils.BOOLEAN_FALSE ); } @Override void encode( @NotNull WriterContext context ); }
BooleanBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getValue().getKind() == Kind.BOOLEAN; context.writeHeader( TAG, 1 ); boolean value = context.getValue().toBooleanValue().asBoolean(); context.write( value ? BerUtils.BOOLEAN_TRUE : BerUtils.BOOLEAN_FALSE ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testDecode_fail_length() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.BOOLEAN.ref().resolve( scope ); try( AbstractBerReader reader = mock( DefaultBerReader.class ) ) { Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); new BooleanBerDecoder().decode( new ReaderContext( reader, scope, type, tag, 2, false ) ); fail( "Must fail" ); } }
@Override public Value decode( @NotNull ReaderContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getLength() == 1; byte content = context.read(); return content == BerUtils.BOOLEAN_FALSE ? BooleanValue.FALSE : BooleanValue.TRUE; }
BooleanBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getLength() == 1; byte content = context.read(); return content == BerUtils.BOOLEAN_FALSE ? BooleanValue.FALSE : BooleanValue.TRUE; } }
BooleanBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getLength() == 1; byte content = context.read(); return content == BerUtils.BOOLEAN_FALSE ? BooleanValue.FALSE : BooleanValue.TRUE; } }
BooleanBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getLength() == 1; byte content = context.read(); return content == BerUtils.BOOLEAN_FALSE ? BooleanValue.FALSE : BooleanValue.TRUE; } @Override Value decode( @NotNull ReaderContext context ); }
BooleanBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getLength() == 1; byte content = context.read(); return content == BerUtils.BOOLEAN_FALSE ? BooleanValue.FALSE : BooleanValue.TRUE; } @Override Value decode( @NotNull ReaderContext context ); }
@Test( expected = AssertionError.class ) public void testEncode_fail_value() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.BOOLEAN.ref().resolve( scope ); Value value = NullValue.INSTANCE; try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new BooleanBerEncoder().encode( new WriterContext( writer, scope, type, value, false ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getValue().getKind() == Kind.BOOLEAN; context.writeHeader( TAG, 1 ); boolean value = context.getValue().toBooleanValue().asBoolean(); context.write( value ? BerUtils.BOOLEAN_TRUE : BerUtils.BOOLEAN_FALSE ); }
BooleanBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getValue().getKind() == Kind.BOOLEAN; context.writeHeader( TAG, 1 ); boolean value = context.getValue().toBooleanValue().asBoolean(); context.write( value ? BerUtils.BOOLEAN_TRUE : BerUtils.BOOLEAN_FALSE ); } }
BooleanBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getValue().getKind() == Kind.BOOLEAN; context.writeHeader( TAG, 1 ); boolean value = context.getValue().toBooleanValue().asBoolean(); context.write( value ? BerUtils.BOOLEAN_TRUE : BerUtils.BOOLEAN_FALSE ); } }
BooleanBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getValue().getKind() == Kind.BOOLEAN; context.writeHeader( TAG, 1 ); boolean value = context.getValue().toBooleanValue().asBoolean(); context.write( value ? BerUtils.BOOLEAN_TRUE : BerUtils.BOOLEAN_FALSE ); } @Override void encode( @NotNull WriterContext context ); }
BooleanBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.BOOLEAN; assert context.getValue().getKind() == Kind.BOOLEAN; context.writeHeader( TAG, 1 ); boolean value = context.getValue().toBooleanValue().asBoolean(); context.write( value ? BerUtils.BOOLEAN_TRUE : BerUtils.BOOLEAN_FALSE ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWriteSetOf_Buffered() throws Exception { Scope scope = CoreModule.getInstance().createScope(); SetOfType type = new SetOfType(); type.setComponent( "a", UniversalType.INTEGER.ref() ); type.validate( scope ); ComponentType component = type.getComponentType(); Assert.assertNotNull( "No component a", component ); ValueCollection value = new ValueCollectionImpl( false ); Value valueInt = new IntegerValueInt( 0 ); value.add( valueInt ); try( AbstractBerWriter writer = new DefaultBerWriter( BerRules.DER ) ) { new SetOfBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); byte[] bytes = writer.toByteArray(); Assert.assertArrayEquals( "Arrays are not equal", new byte[]{0x31, 0x03, 0x02, 0x01, 0x00}, bytes ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWriteSetOf_NonBuffered() throws Exception { Scope scope = CoreModule.getInstance().createScope(); SetOfType type = new SetOfType(); type.setComponent( "a", UniversalType.INTEGER.ref() ); type.validate( scope ); ComponentType component = type.getComponentType(); Assert.assertNotNull( "No component a", component ); ValueCollection value = new ValueCollectionImpl( false ); Value valueInt = new IntegerValueInt( 0 ); value.add( valueInt ); try( AbstractBerWriter writer = new DefaultBerWriter( BerRules.BER ) ) { new SetOfBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) { @Override public boolean isBufferingAvailable() { return false; } } ); byte[] bytes = writer.toByteArray(); Assert.assertArrayEquals( "Arrays are not equal", new byte[]{0x31, (byte)0x80, 0x02, 0x01, 0x00, 0x00, 0x00}, bytes ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = Asn1Exception.class ) public void testWriteSetOf_NonBuffered_Der() throws Exception { Scope scope = CoreModule.getInstance().createScope(); SetOfType type = new SetOfType(); type.setComponent( "a", UniversalType.INTEGER.ref() ); type.validate( scope ); ComponentType component = type.getComponentType(); Assert.assertNotNull( "No component a", component ); ValueCollection value = new ValueCollectionImpl( false ); Value valueInt = new IntegerValueInt( 0 ); value.add( valueInt ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { when( writer.isBufferingAvailable() ).thenReturn( false ); when( writer.getRules() ).thenReturn( BerRules.DER ); new SetOfBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWriteSetOf_NoHeader() throws Exception { Scope scope = CoreModule.getInstance().createScope(); SetOfType type = new SetOfType(); type.setComponent( "a", UniversalType.INTEGER.ref() ); type.validate( scope ); ComponentType component = type.getComponentType(); Assert.assertNotNull( "No component a", component ); ValueCollection value = new ValueCollectionImpl( false ); Value valueInt = new IntegerValueInt( 0 ); value.add( valueInt ); try( AbstractBerWriter writer = new DefaultBerWriter( BerRules.DER ) ) { new SetOfBerEncoder().encode( new WriterContext( writer, scope, type, value, false ) ); byte[] bytes = writer.toByteArray(); Assert.assertArrayEquals( "Arrays are not equal", new byte[]{0x02, 0x01, 0x00}, bytes ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testEncode_fail_type() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.INTEGER.ref().resolve( scope ); Value value = new ValueCollectionImpl( true ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new SetOfBerEncoder().encode( new WriterContext( writer, scope, type, value, false ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testEncode_fail_value() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = new SetOfType(); Value value = BooleanValue.TRUE; try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new SetOfBerEncoder().encode( new WriterContext( writer, scope, type, value, false ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
SetOfBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.SET_OF; assert context.getValue().getKind() == Kind.COLLECTION || context.getValue().getKind() == Kind.NAMED_COLLECTION; if( !context.isWriteHeader() ) SequenceOfBerEncoder.writeCollection( context ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); SequenceOfBerEncoder.writeCollection( context ); context.stopBuffer( SetBerEncoder.TAG ); } else if( context.getRules() == BerRules.DER ) throw new Asn1Exception( "Buffering is required for DER rules" ); else { context.writeHeader( SetBerEncoder.TAG, -1 ); SequenceOfBerEncoder.writeCollection( context ); context.write( 0 ); context.write( 0 ); } } @Override void encode( @NotNull WriterContext context ); }
@Test public void testEncode_Empty() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.OCTET_STRING.ref().resolve( scope ); Value value = CoreUtils.byteArrayFromHexString( "''H" ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new OctetStringBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).writeHeader( TAG, 0 ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.OCTET_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; byte[] bytes = context.getValue().toByteArrayValue().asByteArray(); int length = bytes == null ? 0 : bytes.length; context.writeHeader( TAG, length ); context.write( bytes ); }
OctetStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.OCTET_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; byte[] bytes = context.getValue().toByteArrayValue().asByteArray(); int length = bytes == null ? 0 : bytes.length; context.writeHeader( TAG, length ); context.write( bytes ); } }
OctetStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.OCTET_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; byte[] bytes = context.getValue().toByteArrayValue().asByteArray(); int length = bytes == null ? 0 : bytes.length; context.writeHeader( TAG, length ); context.write( bytes ); } }
OctetStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.OCTET_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; byte[] bytes = context.getValue().toByteArrayValue().asByteArray(); int length = bytes == null ? 0 : bytes.length; context.writeHeader( TAG, length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
OctetStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.OCTET_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; byte[] bytes = context.getValue().toByteArrayValue().asByteArray(); int length = bytes == null ? 0 : bytes.length; context.writeHeader( TAG, length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testEncode_AF() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.OCTET_STRING.ref().resolve( scope ); Value value = CoreUtils.byteArrayFromHexString( "'AF'H" ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new OctetStringBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).writeHeader( TAG, 1 ); verify( writer ).write( new byte[]{(byte)0xAF} ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.OCTET_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; byte[] bytes = context.getValue().toByteArrayValue().asByteArray(); int length = bytes == null ? 0 : bytes.length; context.writeHeader( TAG, length ); context.write( bytes ); }
OctetStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.OCTET_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; byte[] bytes = context.getValue().toByteArrayValue().asByteArray(); int length = bytes == null ? 0 : bytes.length; context.writeHeader( TAG, length ); context.write( bytes ); } }
OctetStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.OCTET_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; byte[] bytes = context.getValue().toByteArrayValue().asByteArray(); int length = bytes == null ? 0 : bytes.length; context.writeHeader( TAG, length ); context.write( bytes ); } }
OctetStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.OCTET_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; byte[] bytes = context.getValue().toByteArrayValue().asByteArray(); int length = bytes == null ? 0 : bytes.length; context.writeHeader( TAG, length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
OctetStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.OCTET_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; byte[] bytes = context.getValue().toByteArrayValue().asByteArray(); int length = bytes == null ? 0 : bytes.length; context.writeHeader( TAG, length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testEncode_fail_type() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.INTEGER.ref().resolve( scope ); Value value = CoreUtils.byteArrayFromHexString( "'AF'H" ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new OctetStringBerEncoder().encode( new WriterContext( writer, scope, type, value, false ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.OCTET_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; byte[] bytes = context.getValue().toByteArrayValue().asByteArray(); int length = bytes == null ? 0 : bytes.length; context.writeHeader( TAG, length ); context.write( bytes ); }
OctetStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.OCTET_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; byte[] bytes = context.getValue().toByteArrayValue().asByteArray(); int length = bytes == null ? 0 : bytes.length; context.writeHeader( TAG, length ); context.write( bytes ); } }
OctetStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.OCTET_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; byte[] bytes = context.getValue().toByteArrayValue().asByteArray(); int length = bytes == null ? 0 : bytes.length; context.writeHeader( TAG, length ); context.write( bytes ); } }
OctetStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.OCTET_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; byte[] bytes = context.getValue().toByteArrayValue().asByteArray(); int length = bytes == null ? 0 : bytes.length; context.writeHeader( TAG, length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
OctetStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.OCTET_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; byte[] bytes = context.getValue().toByteArrayValue().asByteArray(); int length = bytes == null ? 0 : bytes.length; context.writeHeader( TAG, length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testDecode_fail_type() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.INTEGER.ref().resolve( scope ); try( AbstractBerReader reader = mock( DefaultBerReader.class ) ) { Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); new OctetStringBerDecoder().decode( new ReaderContext( reader, scope, type, tag, -1, false ) ); fail( "Must fail" ); } }
@Override public Value decode( @NotNull ReaderContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OCTET_STRING; assert !context.getTag().isConstructed(); if( context.getLength() == -1 ) return readByteArrayValueIndefinite( context.getReader(), 0 ); if( context.getLength() == 0 ) return context.getValueFactory().emptyByteArray(); return readByteArrayValue( context.getReader(), context.getLength(), 0 ); }
OctetStringBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OCTET_STRING; assert !context.getTag().isConstructed(); if( context.getLength() == -1 ) return readByteArrayValueIndefinite( context.getReader(), 0 ); if( context.getLength() == 0 ) return context.getValueFactory().emptyByteArray(); return readByteArrayValue( context.getReader(), context.getLength(), 0 ); } }
OctetStringBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OCTET_STRING; assert !context.getTag().isConstructed(); if( context.getLength() == -1 ) return readByteArrayValueIndefinite( context.getReader(), 0 ); if( context.getLength() == 0 ) return context.getValueFactory().emptyByteArray(); return readByteArrayValue( context.getReader(), context.getLength(), 0 ); } }
OctetStringBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OCTET_STRING; assert !context.getTag().isConstructed(); if( context.getLength() == -1 ) return readByteArrayValueIndefinite( context.getReader(), 0 ); if( context.getLength() == 0 ) return context.getValueFactory().emptyByteArray(); return readByteArrayValue( context.getReader(), context.getLength(), 0 ); } @Override Value decode( @NotNull ReaderContext context ); }
OctetStringBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OCTET_STRING; assert !context.getTag().isConstructed(); if( context.getLength() == -1 ) return readByteArrayValueIndefinite( context.getReader(), 0 ); if( context.getLength() == 0 ) return context.getValueFactory().emptyByteArray(); return readByteArrayValue( context.getReader(), context.getLength(), 0 ); } @Override Value decode( @NotNull ReaderContext context ); }
@Test( expected = AssertionError.class ) public void testEncode_fail_value() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.OCTET_STRING.ref().resolve( scope ); Value value = BooleanValue.TRUE; try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new OctetStringBerEncoder().encode( new WriterContext( writer, scope, type, value, false ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.OCTET_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; byte[] bytes = context.getValue().toByteArrayValue().asByteArray(); int length = bytes == null ? 0 : bytes.length; context.writeHeader( TAG, length ); context.write( bytes ); }
OctetStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.OCTET_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; byte[] bytes = context.getValue().toByteArrayValue().asByteArray(); int length = bytes == null ? 0 : bytes.length; context.writeHeader( TAG, length ); context.write( bytes ); } }
OctetStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.OCTET_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; byte[] bytes = context.getValue().toByteArrayValue().asByteArray(); int length = bytes == null ? 0 : bytes.length; context.writeHeader( TAG, length ); context.write( bytes ); } }
OctetStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.OCTET_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; byte[] bytes = context.getValue().toByteArrayValue().asByteArray(); int length = bytes == null ? 0 : bytes.length; context.writeHeader( TAG, length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
OctetStringBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.OCTET_STRING; assert context.getValue().getKind() == Kind.BYTE_ARRAY; byte[] bytes = context.getValue().toByteArrayValue().asByteArray(); int length = bytes == null ? 0 : bytes.length; context.writeHeader( TAG, length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testWrite_fail_type() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.INTEGER.ref().resolve( scope ); Value value = new IntegerValueInt( 0 ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new RealBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testWrite_fail_value() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.INTEGER.ref().resolve( scope ); Value value = new StringValueImpl( "Value" ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new RealBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWriteInteger_0() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.REAL.ref().resolve( scope ); Value value = new IntegerValueInt( 0 ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new RealBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).writeHeader( TAG, 0 ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWriteReal_neg_0() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.REAL.ref().resolve( scope ); Value value = new RealValueFloat( -0.0f ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new RealBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).writeHeader( TAG, 1 ); verify( writer ).write( 67 ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWriteReal_neg_inf() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.REAL.ref().resolve( scope ); Value value = new RealValueFloat( Float.NEGATIVE_INFINITY ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new RealBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).writeHeader( TAG, 1 ); verify( writer ).write( 65 ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWriteReal_pos_inf() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.REAL.ref().resolve( scope ); Value value = new RealValueFloat( Float.POSITIVE_INFINITY ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new RealBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).writeHeader( TAG, 1 ); verify( writer ).write( 64 ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWriteReal_nan() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.REAL.ref().resolve( scope ); Value value = new RealValueFloat( Float.NaN ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new RealBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).writeHeader( TAG, 1 ); verify( writer ).write( 66 ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWriteReal_big() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.REAL.ref().resolve( scope ); BigDecimal decimal = new BigDecimal( BigInteger.valueOf( 1023234L ).pow( 5223 ) ); byte[] bytes = NRxUtils.toCanonicalNR3( decimal.toString() ).getBytes( "UTF-8" ); Value value = new RealValueBig( decimal ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new RealBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).writeHeader( TAG, 31396 ); verify( writer ).write( 3 ); verify( writer ).write( bytes ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWriteInteger_big() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.REAL.ref().resolve( scope ); BigInteger bigInteger = BigInteger.valueOf( 1023234L ).pow( 5223 ); byte[] bytes = NRxUtils.toCanonicalNR3( new BigDecimal( bigInteger ).toString() ).getBytes( "UTF-8" ); Value value = new IntegerValueBig( bigInteger ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new RealBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).writeHeader( TAG, 31396 ); verify( writer ).write( 3 ); verify( writer ).write( bytes ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testDecode_fail_type() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.INTEGER.ref().resolve( scope ); try( AbstractBerReader reader = mock( DefaultBerReader.class ) ) { Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); new RealBerDecoder().decode( new ReaderContext( reader, scope, type, tag, -1, false ) ); fail( "Must fail" ); } }
@Override public Value decode( @NotNull ReaderContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert !context.getTag().isConstructed(); if( context.getLength() == 0 ) return context.getValueFactory().rZero(); byte first = context.read(); if( first == 0 ) return context.getValueFactory().rZero(); if( ( first & BerUtils.BYTE_SIGN_MASK ) != 0 ) return readBinary( context.getReader(), first, context.getLength() ); switch( first ) { case BerUtils.REAL_ISO_6093_NR1: case BerUtils.REAL_ISO_6093_NR2: case BerUtils.REAL_ISO_6093_NR3: return context.getValueFactory().real( readString( context.getReader(), context.getLength() - 1 ) ); case BerUtils.REAL_NEGATIVE_INF: return context.getValueFactory().rNegativeInfinity(); case BerUtils.REAL_POSITIVE_INF: return context.getValueFactory().rPositiveInfinity(); case BerUtils.REAL_NAN: return context.getValueFactory().rNan(); case BerUtils.REAL_MINUS_ZERO: return context.getValueFactory().rNegativeZero(); default: throw new IllegalStateException( String.format( "Illegal real configuration byte: %02X", first ) ); } }
RealBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert !context.getTag().isConstructed(); if( context.getLength() == 0 ) return context.getValueFactory().rZero(); byte first = context.read(); if( first == 0 ) return context.getValueFactory().rZero(); if( ( first & BerUtils.BYTE_SIGN_MASK ) != 0 ) return readBinary( context.getReader(), first, context.getLength() ); switch( first ) { case BerUtils.REAL_ISO_6093_NR1: case BerUtils.REAL_ISO_6093_NR2: case BerUtils.REAL_ISO_6093_NR3: return context.getValueFactory().real( readString( context.getReader(), context.getLength() - 1 ) ); case BerUtils.REAL_NEGATIVE_INF: return context.getValueFactory().rNegativeInfinity(); case BerUtils.REAL_POSITIVE_INF: return context.getValueFactory().rPositiveInfinity(); case BerUtils.REAL_NAN: return context.getValueFactory().rNan(); case BerUtils.REAL_MINUS_ZERO: return context.getValueFactory().rNegativeZero(); default: throw new IllegalStateException( String.format( "Illegal real configuration byte: %02X", first ) ); } } }
RealBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert !context.getTag().isConstructed(); if( context.getLength() == 0 ) return context.getValueFactory().rZero(); byte first = context.read(); if( first == 0 ) return context.getValueFactory().rZero(); if( ( first & BerUtils.BYTE_SIGN_MASK ) != 0 ) return readBinary( context.getReader(), first, context.getLength() ); switch( first ) { case BerUtils.REAL_ISO_6093_NR1: case BerUtils.REAL_ISO_6093_NR2: case BerUtils.REAL_ISO_6093_NR3: return context.getValueFactory().real( readString( context.getReader(), context.getLength() - 1 ) ); case BerUtils.REAL_NEGATIVE_INF: return context.getValueFactory().rNegativeInfinity(); case BerUtils.REAL_POSITIVE_INF: return context.getValueFactory().rPositiveInfinity(); case BerUtils.REAL_NAN: return context.getValueFactory().rNan(); case BerUtils.REAL_MINUS_ZERO: return context.getValueFactory().rNegativeZero(); default: throw new IllegalStateException( String.format( "Illegal real configuration byte: %02X", first ) ); } } }
RealBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert !context.getTag().isConstructed(); if( context.getLength() == 0 ) return context.getValueFactory().rZero(); byte first = context.read(); if( first == 0 ) return context.getValueFactory().rZero(); if( ( first & BerUtils.BYTE_SIGN_MASK ) != 0 ) return readBinary( context.getReader(), first, context.getLength() ); switch( first ) { case BerUtils.REAL_ISO_6093_NR1: case BerUtils.REAL_ISO_6093_NR2: case BerUtils.REAL_ISO_6093_NR3: return context.getValueFactory().real( readString( context.getReader(), context.getLength() - 1 ) ); case BerUtils.REAL_NEGATIVE_INF: return context.getValueFactory().rNegativeInfinity(); case BerUtils.REAL_POSITIVE_INF: return context.getValueFactory().rPositiveInfinity(); case BerUtils.REAL_NAN: return context.getValueFactory().rNan(); case BerUtils.REAL_MINUS_ZERO: return context.getValueFactory().rNegativeZero(); default: throw new IllegalStateException( String.format( "Illegal real configuration byte: %02X", first ) ); } } @Override Value decode( @NotNull ReaderContext context ); }
RealBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert !context.getTag().isConstructed(); if( context.getLength() == 0 ) return context.getValueFactory().rZero(); byte first = context.read(); if( first == 0 ) return context.getValueFactory().rZero(); if( ( first & BerUtils.BYTE_SIGN_MASK ) != 0 ) return readBinary( context.getReader(), first, context.getLength() ); switch( first ) { case BerUtils.REAL_ISO_6093_NR1: case BerUtils.REAL_ISO_6093_NR2: case BerUtils.REAL_ISO_6093_NR3: return context.getValueFactory().real( readString( context.getReader(), context.getLength() - 1 ) ); case BerUtils.REAL_NEGATIVE_INF: return context.getValueFactory().rNegativeInfinity(); case BerUtils.REAL_POSITIVE_INF: return context.getValueFactory().rPositiveInfinity(); case BerUtils.REAL_NAN: return context.getValueFactory().rNan(); case BerUtils.REAL_MINUS_ZERO: return context.getValueFactory().rNegativeZero(); default: throw new IllegalStateException( String.format( "Illegal real configuration byte: %02X", first ) ); } } @Override Value decode( @NotNull ReaderContext context ); }
@Test public void testWriteReal_1_0() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.REAL.ref().resolve( scope ); Value value = new RealValueFloat( 1.0f ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new RealBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).writeHeader( TAG, 3 ); verify( writer ).write( -128 ); verify( writer ).write( new byte[]{-51} ); verify( writer ).write( new byte[]{0} ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWriteReal_0_15625d() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.REAL.ref().resolve( scope ); Value value = new RealValueDouble( 0.15625d ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new RealBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).writeHeader( TAG, 3 ); verify( writer ).write( -128 ); verify( writer ).write( new byte[]{(byte)0xFB} ); verify( writer ).write( new byte[]{0x05} ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
RealBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.REAL; assert context.getValue().getKind() == Kind.REAL || context.getValue().getKind() == Kind.INTEGER; if( context.getValue().getKind() == Kind.REAL ) writeRealValue( context ); else writeIntegerValue( context ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWriteLong_0() throws Exception { try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { IntegerBerEncoder.writeLong( writer, 0L, null, false ); verify( writer ).write( 0 ); verifyNoMoreInteractions( writer ); } }
static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); }
IntegerBerEncoder implements BerEncoder { static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); } }
IntegerBerEncoder implements BerEncoder { static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); } }
IntegerBerEncoder implements BerEncoder { static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); } @Override void encode( @NotNull WriterContext context ); }
IntegerBerEncoder implements BerEncoder { static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWriteLong_minus_1() throws Exception { try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { IntegerBerEncoder.writeLong( writer, -1L, null, false ); verify( writer ).write( -1 ); verifyNoMoreInteractions( writer ); } }
static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); }
IntegerBerEncoder implements BerEncoder { static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); } }
IntegerBerEncoder implements BerEncoder { static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); } }
IntegerBerEncoder implements BerEncoder { static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); } @Override void encode( @NotNull WriterContext context ); }
IntegerBerEncoder implements BerEncoder { static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWriteLong_256() throws Exception { try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { IntegerBerEncoder.writeLong( writer, 256L, null, false ); verify( writer ).write( 1 ); verify( writer ).write( 0 ); verifyNoMoreInteractions( writer ); } }
static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); }
IntegerBerEncoder implements BerEncoder { static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); } }
IntegerBerEncoder implements BerEncoder { static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); } }
IntegerBerEncoder implements BerEncoder { static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); } @Override void encode( @NotNull WriterContext context ); }
IntegerBerEncoder implements BerEncoder { static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWriteLong_300000() throws Exception { try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { IntegerBerEncoder.writeLong( writer, 300000L, null, false ); verify( writer ).write( (byte)0x04 ); verify( writer ).write( (byte)0x93 ); verify( writer ).write( (byte)0xE0 ); verifyNoMoreInteractions( writer ); } }
static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); }
IntegerBerEncoder implements BerEncoder { static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); } }
IntegerBerEncoder implements BerEncoder { static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); } }
IntegerBerEncoder implements BerEncoder { static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); } @Override void encode( @NotNull WriterContext context ); }
IntegerBerEncoder implements BerEncoder { static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = IOException.class ) public void testWriteLong_fail() throws Exception { try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { IntegerBerEncoder.writeLong( writer, 0L, null, true ); fail( "This method must fail." ); } }
static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); }
IntegerBerEncoder implements BerEncoder { static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); } }
IntegerBerEncoder implements BerEncoder { static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); } }
IntegerBerEncoder implements BerEncoder { static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); } @Override void encode( @NotNull WriterContext context ); }
IntegerBerEncoder implements BerEncoder { static void writeLong( @NotNull AbstractBerWriter os, long value, @Nullable Tag tag, boolean writeHeader ) throws IOException { if( tag == null && writeHeader ) throw new IOException( "Unable to write header: tag is unavailable." ); int size = calculateByteCount( value ); if( writeHeader ) os.writeHeader( tag, size ); for( int i = size - 1; i >= 0; i-- ) os.write( getByteByIndex( value, i ) ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testToByteArray_0() throws Exception { byte[] array = IntegerBerEncoder.toByteArray( 0L ); Assert.assertEquals( "Illegal array size", 1, array.length ); Assert.assertArrayEquals( "Arrays are different", new byte[]{0}, array ); }
static byte[] toByteArray( long value ) { int size = calculateByteCount( value ); byte[] result = new byte[size]; for( int i = size - 1, position = 0; i >= 0; i--, position++ ) result[position] = getByteByIndex( value, i ); return result; }
IntegerBerEncoder implements BerEncoder { static byte[] toByteArray( long value ) { int size = calculateByteCount( value ); byte[] result = new byte[size]; for( int i = size - 1, position = 0; i >= 0; i--, position++ ) result[position] = getByteByIndex( value, i ); return result; } }
IntegerBerEncoder implements BerEncoder { static byte[] toByteArray( long value ) { int size = calculateByteCount( value ); byte[] result = new byte[size]; for( int i = size - 1, position = 0; i >= 0; i--, position++ ) result[position] = getByteByIndex( value, i ); return result; } }
IntegerBerEncoder implements BerEncoder { static byte[] toByteArray( long value ) { int size = calculateByteCount( value ); byte[] result = new byte[size]; for( int i = size - 1, position = 0; i >= 0; i--, position++ ) result[position] = getByteByIndex( value, i ); return result; } @Override void encode( @NotNull WriterContext context ); }
IntegerBerEncoder implements BerEncoder { static byte[] toByteArray( long value ) { int size = calculateByteCount( value ); byte[] result = new byte[size]; for( int i = size - 1, position = 0; i >= 0; i--, position++ ) result[position] = getByteByIndex( value, i ); return result; } @Override void encode( @NotNull WriterContext context ); }
@Test public void testToByteArray_minus_1() throws Exception { byte[] array = IntegerBerEncoder.toByteArray( -1L ); Assert.assertEquals( "Illegal array size", 1, array.length ); Assert.assertArrayEquals( "Arrays are different", new byte[]{-1}, array ); }
static byte[] toByteArray( long value ) { int size = calculateByteCount( value ); byte[] result = new byte[size]; for( int i = size - 1, position = 0; i >= 0; i--, position++ ) result[position] = getByteByIndex( value, i ); return result; }
IntegerBerEncoder implements BerEncoder { static byte[] toByteArray( long value ) { int size = calculateByteCount( value ); byte[] result = new byte[size]; for( int i = size - 1, position = 0; i >= 0; i--, position++ ) result[position] = getByteByIndex( value, i ); return result; } }
IntegerBerEncoder implements BerEncoder { static byte[] toByteArray( long value ) { int size = calculateByteCount( value ); byte[] result = new byte[size]; for( int i = size - 1, position = 0; i >= 0; i--, position++ ) result[position] = getByteByIndex( value, i ); return result; } }
IntegerBerEncoder implements BerEncoder { static byte[] toByteArray( long value ) { int size = calculateByteCount( value ); byte[] result = new byte[size]; for( int i = size - 1, position = 0; i >= 0; i--, position++ ) result[position] = getByteByIndex( value, i ); return result; } @Override void encode( @NotNull WriterContext context ); }
IntegerBerEncoder implements BerEncoder { static byte[] toByteArray( long value ) { int size = calculateByteCount( value ); byte[] result = new byte[size]; for( int i = size - 1, position = 0; i >= 0; i--, position++ ) result[position] = getByteByIndex( value, i ); return result; } @Override void encode( @NotNull WriterContext context ); }
@Test public void testToByteArray_256() throws Exception { byte[] array = IntegerBerEncoder.toByteArray( 256L ); Assert.assertEquals( "Illegal array size", 2, array.length ); Assert.assertArrayEquals( "Arrays are different", new byte[]{1, 0}, array ); }
static byte[] toByteArray( long value ) { int size = calculateByteCount( value ); byte[] result = new byte[size]; for( int i = size - 1, position = 0; i >= 0; i--, position++ ) result[position] = getByteByIndex( value, i ); return result; }
IntegerBerEncoder implements BerEncoder { static byte[] toByteArray( long value ) { int size = calculateByteCount( value ); byte[] result = new byte[size]; for( int i = size - 1, position = 0; i >= 0; i--, position++ ) result[position] = getByteByIndex( value, i ); return result; } }
IntegerBerEncoder implements BerEncoder { static byte[] toByteArray( long value ) { int size = calculateByteCount( value ); byte[] result = new byte[size]; for( int i = size - 1, position = 0; i >= 0; i--, position++ ) result[position] = getByteByIndex( value, i ); return result; } }
IntegerBerEncoder implements BerEncoder { static byte[] toByteArray( long value ) { int size = calculateByteCount( value ); byte[] result = new byte[size]; for( int i = size - 1, position = 0; i >= 0; i--, position++ ) result[position] = getByteByIndex( value, i ); return result; } @Override void encode( @NotNull WriterContext context ); }
IntegerBerEncoder implements BerEncoder { static byte[] toByteArray( long value ) { int size = calculateByteCount( value ); byte[] result = new byte[size]; for( int i = size - 1, position = 0; i >= 0; i--, position++ ) result[position] = getByteByIndex( value, i ); return result; } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testDecode_fail_type() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.REAL.ref().resolve( scope ); try( AbstractBerReader reader = mock( DefaultBerReader.class ) ) { Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); new IntegerBerDecoder().decode( new ReaderContext( reader, scope, type, tag, -1, false ) ); fail( "Must fail" ); } }
@Override public Value decode( @NotNull ReaderContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getLength() >= 0; return readInteger( context.getReader(), context.getLength() ); }
IntegerBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getLength() >= 0; return readInteger( context.getReader(), context.getLength() ); } }
IntegerBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getLength() >= 0; return readInteger( context.getReader(), context.getLength() ); } }
IntegerBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getLength() >= 0; return readInteger( context.getReader(), context.getLength() ); } @Override Value decode( @NotNull ReaderContext context ); }
IntegerBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getLength() >= 0; return readInteger( context.getReader(), context.getLength() ); } @Override Value decode( @NotNull ReaderContext context ); }
@Test public void testToByteArray_300000() throws Exception { byte[] array = IntegerBerEncoder.toByteArray( 300000L ); Assert.assertEquals( "Illegal array size", 3, array.length ); Assert.assertArrayEquals( "Arrays are different", new byte[]{0x04, (byte)0x93, (byte)0xE0}, array ); }
static byte[] toByteArray( long value ) { int size = calculateByteCount( value ); byte[] result = new byte[size]; for( int i = size - 1, position = 0; i >= 0; i--, position++ ) result[position] = getByteByIndex( value, i ); return result; }
IntegerBerEncoder implements BerEncoder { static byte[] toByteArray( long value ) { int size = calculateByteCount( value ); byte[] result = new byte[size]; for( int i = size - 1, position = 0; i >= 0; i--, position++ ) result[position] = getByteByIndex( value, i ); return result; } }
IntegerBerEncoder implements BerEncoder { static byte[] toByteArray( long value ) { int size = calculateByteCount( value ); byte[] result = new byte[size]; for( int i = size - 1, position = 0; i >= 0; i--, position++ ) result[position] = getByteByIndex( value, i ); return result; } }
IntegerBerEncoder implements BerEncoder { static byte[] toByteArray( long value ) { int size = calculateByteCount( value ); byte[] result = new byte[size]; for( int i = size - 1, position = 0; i >= 0; i--, position++ ) result[position] = getByteByIndex( value, i ); return result; } @Override void encode( @NotNull WriterContext context ); }
IntegerBerEncoder implements BerEncoder { static byte[] toByteArray( long value ) { int size = calculateByteCount( value ); byte[] result = new byte[size]; for( int i = size - 1, position = 0; i >= 0; i--, position++ ) result[position] = getByteByIndex( value, i ); return result; } @Override void encode( @NotNull WriterContext context ); }
@Test public void testEncode_0() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.INTEGER.ref().resolve( scope ); Value value = new IntegerValueInt( 0 ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new IntegerBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).writeHeader( TAG, 1 ); verify( writer ).write( 0 ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); }
IntegerBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); } }
IntegerBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); } }
IntegerBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); } @Override void encode( @NotNull WriterContext context ); }
IntegerBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testEncode_minus_1() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.INTEGER.ref().resolve( scope ); Value value = new IntegerValueInt( -1 ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new IntegerBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).writeHeader( TAG, 1 ); verify( writer ).write( -1 ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); }
IntegerBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); } }
IntegerBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); } }
IntegerBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); } @Override void encode( @NotNull WriterContext context ); }
IntegerBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testEncode_256() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.INTEGER.ref().resolve( scope ); Value value = new IntegerValueInt( 256 ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new IntegerBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).writeHeader( TAG, 2 ); verify( writer ).write( 1 ); verify( writer ).write( 0 ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); }
IntegerBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); } }
IntegerBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); } }
IntegerBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); } @Override void encode( @NotNull WriterContext context ); }
IntegerBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testEncode_fail_type() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.REAL.ref().resolve( scope ); Value value = new IntegerValueInt( 256 ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new IntegerBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); }
IntegerBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); } }
IntegerBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); } }
IntegerBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); } @Override void encode( @NotNull WriterContext context ); }
IntegerBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testEncode_fail_value() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.INTEGER.ref().resolve( scope ); Value value = new RealValueFloat( 0.0f ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new IntegerBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); }
IntegerBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); } }
IntegerBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); } }
IntegerBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); } @Override void encode( @NotNull WriterContext context ); }
IntegerBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.INTEGER; assert context.getValue().getKind() == Kind.INTEGER; writeLong( context.getWriter(), context.getValue().toIntegerValue().asLong(), TAG, context.isWriteHeader() ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testEncode_NoHeader() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.OBJECT_IDENTIFIER.ref().resolve( scope ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new ObjectIDBerEncoder().encode( new WriterContext( writer, scope, type, OPTIMIZED_OID_VALUE, false ) ); verify( writer, times( 2 ) ).write( 0 ); verify( writer ).write( 1 ); verify( writer ).write( (byte)0x82 ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getValue().getKind() == Kind.OID; if( !context.isWriteHeader() ) writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); context.stopBuffer( TAG ); } else throw new Asn1Exception( "Buffering is required for ObjectIdentifier" ); }
ObjectIDBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getValue().getKind() == Kind.OID; if( !context.isWriteHeader() ) writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); context.stopBuffer( TAG ); } else throw new Asn1Exception( "Buffering is required for ObjectIdentifier" ); } }
ObjectIDBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getValue().getKind() == Kind.OID; if( !context.isWriteHeader() ) writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); context.stopBuffer( TAG ); } else throw new Asn1Exception( "Buffering is required for ObjectIdentifier" ); } }
ObjectIDBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getValue().getKind() == Kind.OID; if( !context.isWriteHeader() ) writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); context.stopBuffer( TAG ); } else throw new Asn1Exception( "Buffering is required for ObjectIdentifier" ); } @Override void encode( @NotNull WriterContext context ); }
ObjectIDBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getValue().getKind() == Kind.OID; if( !context.isWriteHeader() ) writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); context.stopBuffer( TAG ); } else throw new Asn1Exception( "Buffering is required for ObjectIdentifier" ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testEncode_Buffered() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.OBJECT_IDENTIFIER.ref().resolve( scope ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { when( writer.isBufferingAvailable() ).thenReturn( true ); new ObjectIDBerEncoder().encode( new WriterContext( writer, scope, type, OPTIMIZED_OID_VALUE, true ) ); verify( writer ).isBufferingAvailable(); verify( writer ).startBuffer( -1 ); verify( writer, times( 2 ) ).write( 0 ); verify( writer ).write( 1 ); verify( writer ).write( (byte)0x82 ); verify( writer ).stopBuffer( TAG ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getValue().getKind() == Kind.OID; if( !context.isWriteHeader() ) writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); context.stopBuffer( TAG ); } else throw new Asn1Exception( "Buffering is required for ObjectIdentifier" ); }
ObjectIDBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getValue().getKind() == Kind.OID; if( !context.isWriteHeader() ) writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); context.stopBuffer( TAG ); } else throw new Asn1Exception( "Buffering is required for ObjectIdentifier" ); } }
ObjectIDBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getValue().getKind() == Kind.OID; if( !context.isWriteHeader() ) writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); context.stopBuffer( TAG ); } else throw new Asn1Exception( "Buffering is required for ObjectIdentifier" ); } }
ObjectIDBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getValue().getKind() == Kind.OID; if( !context.isWriteHeader() ) writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); context.stopBuffer( TAG ); } else throw new Asn1Exception( "Buffering is required for ObjectIdentifier" ); } @Override void encode( @NotNull WriterContext context ); }
ObjectIDBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getValue().getKind() == Kind.OID; if( !context.isWriteHeader() ) writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); context.stopBuffer( TAG ); } else throw new Asn1Exception( "Buffering is required for ObjectIdentifier" ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testEncode_fail_type() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.INTEGER.ref().resolve( scope ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new ObjectIDBerEncoder().encode( new WriterContext( writer, scope, type, OPTIMIZED_OID_VALUE, false ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getValue().getKind() == Kind.OID; if( !context.isWriteHeader() ) writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); context.stopBuffer( TAG ); } else throw new Asn1Exception( "Buffering is required for ObjectIdentifier" ); }
ObjectIDBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getValue().getKind() == Kind.OID; if( !context.isWriteHeader() ) writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); context.stopBuffer( TAG ); } else throw new Asn1Exception( "Buffering is required for ObjectIdentifier" ); } }
ObjectIDBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getValue().getKind() == Kind.OID; if( !context.isWriteHeader() ) writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); context.stopBuffer( TAG ); } else throw new Asn1Exception( "Buffering is required for ObjectIdentifier" ); } }
ObjectIDBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getValue().getKind() == Kind.OID; if( !context.isWriteHeader() ) writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); context.stopBuffer( TAG ); } else throw new Asn1Exception( "Buffering is required for ObjectIdentifier" ); } @Override void encode( @NotNull WriterContext context ); }
ObjectIDBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getValue().getKind() == Kind.OID; if( !context.isWriteHeader() ) writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); context.stopBuffer( TAG ); } else throw new Asn1Exception( "Buffering is required for ObjectIdentifier" ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testEncode_fail_value() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.OBJECT_IDENTIFIER.ref().resolve( scope ); Value value = BooleanValue.TRUE; try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new ObjectIDBerEncoder().encode( new WriterContext( writer, scope, type, value, false ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getValue().getKind() == Kind.OID; if( !context.isWriteHeader() ) writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); context.stopBuffer( TAG ); } else throw new Asn1Exception( "Buffering is required for ObjectIdentifier" ); }
ObjectIDBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getValue().getKind() == Kind.OID; if( !context.isWriteHeader() ) writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); context.stopBuffer( TAG ); } else throw new Asn1Exception( "Buffering is required for ObjectIdentifier" ); } }
ObjectIDBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getValue().getKind() == Kind.OID; if( !context.isWriteHeader() ) writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); context.stopBuffer( TAG ); } else throw new Asn1Exception( "Buffering is required for ObjectIdentifier" ); } }
ObjectIDBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getValue().getKind() == Kind.OID; if( !context.isWriteHeader() ) writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); context.stopBuffer( TAG ); } else throw new Asn1Exception( "Buffering is required for ObjectIdentifier" ); } @Override void encode( @NotNull WriterContext context ); }
ObjectIDBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.OID; assert context.getValue().getKind() == Kind.OID; if( !context.isWriteHeader() ) writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); else if( context.isBufferingAvailable() ) { context.startBuffer( -1 ); writeObjectIDImpl( context.getWriter(), context.getValue().toObjectIdentifierValue() ); context.stopBuffer( TAG ); } else throw new Asn1Exception( "Buffering is required for ObjectIdentifier" ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testDecode() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.UTF8_STRING.ref().resolve( scope ); Value value = new StringValueImpl( "Example" ); byte[] result = InputUtils.writeValue( scope, type, value ); int totalWritten = result.length - 2; byte[] noHeader = new byte[totalWritten]; System.arraycopy( result, 2, noHeader, 0, noHeader.length ); try( AbstractBerReader reader = mock( AbstractBerReader.class ) ) { ValueFactory factory = mock( ValueFactory.class ); when( reader.getValueFactory() ).thenReturn( factory ); when( reader.read( any() ) ).then( invocationOnMock -> { System.arraycopy( result, 2, invocationOnMock.getArguments()[0], 0, totalWritten ); return totalWritten; } ); Tag tag = ( (TagEncoding)type.getEncoding( EncodingInstructions.TAG ) ).toTag( false ); new StringBerDecoder().decode( new ReaderContext( reader, scope, type, tag, totalWritten, false ) ); verify( reader ).getValueFactory(); verify( reader ).read( any( byte[].class ) ); verifyNoMoreInteractions( reader ); } }
@Override public Value decode( @NotNull ReaderContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.RESTRICTED_STRING; Type type = context.getType(); while( !( type instanceof StringType ) ) { assert type != null; type = type.getSibling(); } byte[] content = BerDecoderUtils.readString( context.getReader(), context.getLength() ); return context.getValueFactory().cString( new String( content, ( (StringType)type ).getCharset() ) ); }
StringBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.RESTRICTED_STRING; Type type = context.getType(); while( !( type instanceof StringType ) ) { assert type != null; type = type.getSibling(); } byte[] content = BerDecoderUtils.readString( context.getReader(), context.getLength() ); return context.getValueFactory().cString( new String( content, ( (StringType)type ).getCharset() ) ); } }
StringBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.RESTRICTED_STRING; Type type = context.getType(); while( !( type instanceof StringType ) ) { assert type != null; type = type.getSibling(); } byte[] content = BerDecoderUtils.readString( context.getReader(), context.getLength() ); return context.getValueFactory().cString( new String( content, ( (StringType)type ).getCharset() ) ); } }
StringBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.RESTRICTED_STRING; Type type = context.getType(); while( !( type instanceof StringType ) ) { assert type != null; type = type.getSibling(); } byte[] content = BerDecoderUtils.readString( context.getReader(), context.getLength() ); return context.getValueFactory().cString( new String( content, ( (StringType)type ).getCharset() ) ); } @Override Value decode( @NotNull ReaderContext context ); }
StringBerDecoder implements BerDecoder { @Override public Value decode( @NotNull ReaderContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.RESTRICTED_STRING; Type type = context.getType(); while( !( type instanceof StringType ) ) { assert type != null; type = type.getSibling(); } byte[] content = BerDecoderUtils.readString( context.getReader(), context.getLength() ); return context.getValueFactory().cString( new String( content, ( (StringType)type ).getCharset() ) ); } @Override Value decode( @NotNull ReaderContext context ); }
@Test public void testEncode_value_no_header() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.NULL.ref().resolve( scope ); Value value = NullValue.INSTANCE; try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new NullBerEncoder().encode( new WriterContext( writer, scope, type, value, false ) ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.NULL; assert context.getValue().getKind() == Kind.NULL; context.writeHeader( TAG, 0 ); }
NullBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.NULL; assert context.getValue().getKind() == Kind.NULL; context.writeHeader( TAG, 0 ); } }
NullBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.NULL; assert context.getValue().getKind() == Kind.NULL; context.writeHeader( TAG, 0 ); } }
NullBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.NULL; assert context.getValue().getKind() == Kind.NULL; context.writeHeader( TAG, 0 ); } @Override void encode( @NotNull WriterContext context ); }
NullBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.NULL; assert context.getValue().getKind() == Kind.NULL; context.writeHeader( TAG, 0 ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testEncode_value_with_header() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.NULL.ref().resolve( scope ); Value value = NullValue.INSTANCE; try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new NullBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).writeHeader( TAG, 0 ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.NULL; assert context.getValue().getKind() == Kind.NULL; context.writeHeader( TAG, 0 ); }
NullBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.NULL; assert context.getValue().getKind() == Kind.NULL; context.writeHeader( TAG, 0 ); } }
NullBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.NULL; assert context.getValue().getKind() == Kind.NULL; context.writeHeader( TAG, 0 ); } }
NullBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.NULL; assert context.getValue().getKind() == Kind.NULL; context.writeHeader( TAG, 0 ); } @Override void encode( @NotNull WriterContext context ); }
NullBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.NULL; assert context.getValue().getKind() == Kind.NULL; context.writeHeader( TAG, 0 ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testEncode_fail_type() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.INTEGER.ref().resolve( scope ); Value value = NullValue.INSTANCE; try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new NullBerEncoder().encode( new WriterContext( writer, scope, type, value, false ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.NULL; assert context.getValue().getKind() == Kind.NULL; context.writeHeader( TAG, 0 ); }
NullBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.NULL; assert context.getValue().getKind() == Kind.NULL; context.writeHeader( TAG, 0 ); } }
NullBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.NULL; assert context.getValue().getKind() == Kind.NULL; context.writeHeader( TAG, 0 ); } }
NullBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.NULL; assert context.getValue().getKind() == Kind.NULL; context.writeHeader( TAG, 0 ); } @Override void encode( @NotNull WriterContext context ); }
NullBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.NULL; assert context.getValue().getKind() == Kind.NULL; context.writeHeader( TAG, 0 ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testEncode_fail_value() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.NULL.ref().resolve( scope ); Value value = BooleanValue.TRUE; try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new NullBerEncoder().encode( new WriterContext( writer, scope, type, value, false ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.NULL; assert context.getValue().getKind() == Kind.NULL; context.writeHeader( TAG, 0 ); }
NullBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.NULL; assert context.getValue().getKind() == Kind.NULL; context.writeHeader( TAG, 0 ); } }
NullBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.NULL; assert context.getValue().getKind() == Kind.NULL; context.writeHeader( TAG, 0 ); } }
NullBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.NULL; assert context.getValue().getKind() == Kind.NULL; context.writeHeader( TAG, 0 ); } @Override void encode( @NotNull WriterContext context ); }
NullBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException { assert context.getType().getFamily() == Family.NULL; assert context.getValue().getKind() == Kind.NULL; context.writeHeader( TAG, 0 ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWrite_Der() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.UTC_TIME.ref().resolve( scope ); Value value = new DateValueImpl( TimeUtils.parseUTCTime( TIME_VALUE ) ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { when( writer.getRules() ).thenReturn( BerRules.DER ); new UTCTimeBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).getRules(); verify( writer ).writeHeader( TAG, 13 ); verify( writer ).write( new byte[]{0x31, 0x37, 0x30, 0x36, 0x30, 0x31, 0x31, 0x31, 0x35, 0x37, 0x30, 0x30, 0x5A} ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.UTC_TIME; assert context.getValue().getKind() == Kind.TIME; String content = TimeUtils.formatInstant( context.getValue().toDateValue().asInstant(), TimeUtils.UTC_TIME_FORMAT, context.getRules() != BerRules.DER ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); }
UTCTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.UTC_TIME; assert context.getValue().getKind() == Kind.TIME; String content = TimeUtils.formatInstant( context.getValue().toDateValue().asInstant(), TimeUtils.UTC_TIME_FORMAT, context.getRules() != BerRules.DER ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } }
UTCTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.UTC_TIME; assert context.getValue().getKind() == Kind.TIME; String content = TimeUtils.formatInstant( context.getValue().toDateValue().asInstant(), TimeUtils.UTC_TIME_FORMAT, context.getRules() != BerRules.DER ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } }
UTCTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.UTC_TIME; assert context.getValue().getKind() == Kind.TIME; String content = TimeUtils.formatInstant( context.getValue().toDateValue().asInstant(), TimeUtils.UTC_TIME_FORMAT, context.getRules() != BerRules.DER ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
UTCTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.UTC_TIME; assert context.getValue().getKind() == Kind.TIME; String content = TimeUtils.formatInstant( context.getValue().toDateValue().asInstant(), TimeUtils.UTC_TIME_FORMAT, context.getRules() != BerRules.DER ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
@Test public void testWrite_Ber() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.UTC_TIME.ref().resolve( scope ); Value value = new DateValueImpl( TimeUtils.parseUTCTime( TIME_VALUE ) ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { when( writer.getRules() ).thenReturn( BerRules.BER ); new UTCTimeBerEncoder().encode( new WriterContext( writer, scope, type, value, true ) ); verify( writer ).getRules(); verify( writer ).writeHeader( TAG, 11 ); verify( writer ).write( new byte[]{0x31, 0x37, 0x30, 0x36, 0x30, 0x31, 0x31, 0x31, 0x35, 0x37, 0x5A} ); verifyNoMoreInteractions( writer ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.UTC_TIME; assert context.getValue().getKind() == Kind.TIME; String content = TimeUtils.formatInstant( context.getValue().toDateValue().asInstant(), TimeUtils.UTC_TIME_FORMAT, context.getRules() != BerRules.DER ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); }
UTCTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.UTC_TIME; assert context.getValue().getKind() == Kind.TIME; String content = TimeUtils.formatInstant( context.getValue().toDateValue().asInstant(), TimeUtils.UTC_TIME_FORMAT, context.getRules() != BerRules.DER ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } }
UTCTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.UTC_TIME; assert context.getValue().getKind() == Kind.TIME; String content = TimeUtils.formatInstant( context.getValue().toDateValue().asInstant(), TimeUtils.UTC_TIME_FORMAT, context.getRules() != BerRules.DER ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } }
UTCTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.UTC_TIME; assert context.getValue().getKind() == Kind.TIME; String content = TimeUtils.formatInstant( context.getValue().toDateValue().asInstant(), TimeUtils.UTC_TIME_FORMAT, context.getRules() != BerRules.DER ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
UTCTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.UTC_TIME; assert context.getValue().getKind() == Kind.TIME; String content = TimeUtils.formatInstant( context.getValue().toDateValue().asInstant(), TimeUtils.UTC_TIME_FORMAT, context.getRules() != BerRules.DER ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
@Test( expected = AssertionError.class ) public void testEncode_fail_type() throws Exception { Scope scope = CoreModule.getInstance().createScope(); Type type = UniversalType.INTEGER.ref().resolve( scope ); Value value = new DateValueImpl( TimeUtils.parseUTCTime( TIME_VALUE ) ); try( AbstractBerWriter writer = mock( AbstractBerWriter.class ) ) { new UTCTimeBerEncoder().encode( new WriterContext( writer, scope, type, value, false ) ); fail( "Must fail" ); } }
@Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.UTC_TIME; assert context.getValue().getKind() == Kind.TIME; String content = TimeUtils.formatInstant( context.getValue().toDateValue().asInstant(), TimeUtils.UTC_TIME_FORMAT, context.getRules() != BerRules.DER ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); }
UTCTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.UTC_TIME; assert context.getValue().getKind() == Kind.TIME; String content = TimeUtils.formatInstant( context.getValue().toDateValue().asInstant(), TimeUtils.UTC_TIME_FORMAT, context.getRules() != BerRules.DER ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } }
UTCTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.UTC_TIME; assert context.getValue().getKind() == Kind.TIME; String content = TimeUtils.formatInstant( context.getValue().toDateValue().asInstant(), TimeUtils.UTC_TIME_FORMAT, context.getRules() != BerRules.DER ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } }
UTCTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.UTC_TIME; assert context.getValue().getKind() == Kind.TIME; String content = TimeUtils.formatInstant( context.getValue().toDateValue().asInstant(), TimeUtils.UTC_TIME_FORMAT, context.getRules() != BerRules.DER ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }
UTCTimeBerEncoder implements BerEncoder { @Override public void encode( @NotNull WriterContext context ) throws IOException, Asn1Exception { assert context.getType().getFamily() == Family.UTC_TIME; assert context.getValue().getKind() == Kind.TIME; String content = TimeUtils.formatInstant( context.getValue().toDateValue().asInstant(), TimeUtils.UTC_TIME_FORMAT, context.getRules() != BerRules.DER ); byte[] bytes = content.getBytes( TimeUtils.CHARSET ); context.writeHeader( TAG, bytes.length ); context.write( bytes ); } @Override void encode( @NotNull WriterContext context ); }