Search in sources :

Example 31 with ReverseByteArrayOutputStream

use of com.beanit.asn1bean.ber.ReverseByteArrayOutputStream in project jasn1 by openmuc.

the class AnyTypeTest method encodingAny.

@Test
public void encodingAny() throws Exception {
    SimpleSeq simpleSeq = new SimpleSeq(new BerInteger(2));
    ReverseByteArrayOutputStream os = new ReverseByteArrayOutputStream(1000);
    simpleSeq.encode(os);
    System.out.println(HexString.fromBytes(os.getArray()));
    assertArrayEquals(toBytes("3003800102"), os.getArray());
    SeqContainingAnyDefinedBy seqContainingAnyDefinedBy = new SeqContainingAnyDefinedBy(new BerAny(os.getArray()));
    os.reset();
    seqContainingAnyDefinedBy.encode(os);
    System.out.println(HexString.fromBytes(os.getArray()));
    assertArrayEquals(toBytes("3007A0053003800102"), os.getArray());
}
Also used : BerInteger(com.beanit.asn1bean.ber.types.BerInteger) SeqContainingAnyDefinedBy(com.beanit.asn1bean.compiler.various_tests.SeqContainingAnyDefinedBy) BerAny(com.beanit.asn1bean.ber.types.BerAny) SimpleSeq(com.beanit.asn1bean.compiler.various_tests.SimpleSeq) ReverseByteArrayOutputStream(com.beanit.asn1bean.ber.ReverseByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 32 with ReverseByteArrayOutputStream

use of com.beanit.asn1bean.ber.ReverseByteArrayOutputStream in project jasn1 by openmuc.

the class ExtensibilityImpliedTest method setExtensionBytes.

@Test
void setExtensionBytes() throws IOException {
    NonExtensibleSequence nonExtensibleSequence = new NonExtensibleSequence();
    nonExtensibleSequence.setAge(new BerInteger(5));
    ExtensibleAccessSequence extensibleSequence = new ExtensibleAccessSequence();
    extensibleSequence.setAge(new BerInteger(5));
    extensibleSequence.setExtensionBytes(extensionCode);
    ExtensibleAccessSequenceAndMore extensibleSequenceAndMore = new ExtensibleAccessSequenceAndMore();
    extensibleSequenceAndMore.setExtensibleSequence(extensibleSequence);
    extensibleSequenceAndMore.setMore(new BerVisibleString("more"));
    ReverseByteArrayOutputStream os = new ReverseByteArrayOutputStream(100, true);
    extensibleSequenceAndMore.encode(os);
    Assertions.assertArrayEquals(extendedSequenceAndMoreCode, os.getArray());
}
Also used : ExtensibleAccessSequence(com.beanit.asn1bean.compiler.extension_test.extensible_with_access.ExtensibleAccessSequence) ExtensibleAccessSequenceAndMore(com.beanit.asn1bean.compiler.extension_test.extensible_with_access.ExtensibleAccessSequenceAndMore) NonExtensibleSequence(com.beanit.asn1bean.compiler.extension_test.non_extensible.NonExtensibleSequence) BerVisibleString(com.beanit.asn1bean.ber.types.string.BerVisibleString) BerInteger(com.beanit.asn1bean.ber.types.BerInteger) ReverseByteArrayOutputStream(com.beanit.asn1bean.ber.ReverseByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 33 with ReverseByteArrayOutputStream

use of com.beanit.asn1bean.ber.ReverseByteArrayOutputStream in project jasn1 by openmuc.

the class ExtensibilityImpliedTest method computeCode.

@BeforeAll
static void computeCode() throws IOException {
    ReverseByteArrayOutputStream os = new ReverseByteArrayOutputStream(100, true);
    NonExtensibleSequence nonExtensibleSequence = new NonExtensibleSequence();
    nonExtensibleSequence.setAge(new BerInteger(5));
    os.reset();
    nonExtensibleSequence.encode(os);
    byte[] nonExtensibleSequenceCode = os.getArray();
    ExtendedSequence extendedSequence = new ExtendedSequence();
    extendedSequence.setAge(new BerInteger(5));
    extendedSequence.setName(new BerVisibleString("name"));
    extendedSequence.setSubAge(nonExtensibleSequence);
    os.reset();
    extendedSequence.encode(os);
    byte[] extendedSequenceCode = os.getArray();
    extensionCode = Arrays.copyOfRange(extendedSequenceCode, nonExtensibleSequenceCode.length, extendedSequenceCode.length);
    ExtendedSequenceAndMore extendedSequenceAndMore = new ExtendedSequenceAndMore();
    extendedSequenceAndMore.setExtendedSequence(extendedSequence);
    extendedSequenceAndMore.setMore(new BerVisibleString("more"));
    os.reset();
    extendedSequenceAndMore.encode(os);
    extendedSequenceAndMoreCode = os.getArray();
}
Also used : NonExtensibleSequence(com.beanit.asn1bean.compiler.extension_test.non_extensible.NonExtensibleSequence) BerVisibleString(com.beanit.asn1bean.ber.types.string.BerVisibleString) ExtendedSequenceAndMore(com.beanit.asn1bean.compiler.extension_test.non_extensible.ExtendedSequenceAndMore) BerInteger(com.beanit.asn1bean.ber.types.BerInteger) ExtendedSequence(com.beanit.asn1bean.compiler.extension_test.non_extensible.ExtendedSequence) ReverseByteArrayOutputStream(com.beanit.asn1bean.ber.ReverseByteArrayOutputStream) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 34 with ReverseByteArrayOutputStream

use of com.beanit.asn1bean.ber.ReverseByteArrayOutputStream in project jasn1 by openmuc.

the class ASN1beanSample method main.

public static void main(String[] args) throws IOException {
    ReverseByteArrayOutputStream os = new ReverseByteArrayOutputStream(1000);
    // Name name = new Name(new BerVisibleString("John"), new
    // BerVisibleString("P"), new BerVisibleString("Smith"));
    // instead of creating the Name object as in previous statement you can
    // assign the byte code directly as in the following statement. The
    // encode function of the name object will then simply insert this byte
    // array in the OutputStream. This can speed up things if the code
    // for certain structures is known and does not change.
    Name name = new Name(HexString.toBytes("101A044a6f686e1A01501A05536d697468"));
    BerVisibleString title = new BerVisibleString("Director".getBytes(UTF_8));
    EmployeeNumber number = new EmployeeNumber(51);
    Date dateOfHire = new Date("19710917".getBytes(UTF_8));
    Name nameOfSpouse = new Name();
    nameOfSpouse.setGivenName(new BerVisibleString("Mary"));
    nameOfSpouse.setInitial(new BerVisibleString("T"));
    nameOfSpouse.setFamilyName(new BerVisibleString("Smith"));
    Name child1Name = new Name();
    child1Name.setGivenName(new BerVisibleString("Ralph"));
    child1Name.setInitial(new BerVisibleString("T"));
    child1Name.setFamilyName(new BerVisibleString("Smith"));
    ChildInformation child1 = new ChildInformation();
    child1.setName(child1Name);
    child1.setDateOfBirth(new Date("19571111".getBytes(UTF_8)));
    // encodeAndSave will start the encoding and save the result in
    // child1.code. This is useful if the same structure will have to be
    // encoded several times as part of different structures. Using this
    // function will make sure that the real encoding is only done once.
    child1.encodeAndSave(80);
    Name child2Name = new Name();
    child2Name.setGivenName(new BerVisibleString("Susan"));
    child2Name.setInitial(new BerVisibleString("B"));
    child2Name.setFamilyName(new BerVisibleString("Jones"));
    ChildInformation child2 = new ChildInformation();
    child2.setName(child2Name);
    child2.setDateOfBirth(new Date("19590717".getBytes(UTF_8)));
    PersonnelRecord.Children childrenSeq = new PersonnelRecord.Children();
    List<ChildInformation> childList = childrenSeq.getChildInformation();
    childList.add(child1);
    childList.add(child2);
    PersonnelRecord personnelRecord = new PersonnelRecord();
    personnelRecord.setName(name);
    personnelRecord.setTitle(title);
    personnelRecord.setNumber(number);
    personnelRecord.setDateOfHire(dateOfHire);
    personnelRecord.setNameOfSpouse(nameOfSpouse);
    personnelRecord.setChildren(childrenSeq);
    personnelRecord.encode(os);
    System.out.println("Encoded bytes:");
    System.out.println(HexString.fromBytes(os.getArray()));
    byte[] encodedBytes = os.getArray();
    InputStream is = new ByteArrayInputStream(encodedBytes);
    PersonnelRecord personnelRecord_decoded = new PersonnelRecord();
    personnelRecord_decoded.decode(is);
    System.out.println("\nDecoded structure:");
    System.out.println(personnelRecord_decoded);
    System.out.println("Given name = " + personnelRecord_decoded.getName().getGivenName());
}
Also used : ChildInformation(com.beanit.asn1bean.compiler.x690_ber_example.ChildInformation) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) EmployeeNumber(com.beanit.asn1bean.compiler.x690_ber_example.EmployeeNumber) PersonnelRecord(com.beanit.asn1bean.compiler.x690_ber_example.PersonnelRecord) Date(com.beanit.asn1bean.compiler.x690_ber_example.Date) ReverseByteArrayOutputStream(com.beanit.asn1bean.ber.ReverseByteArrayOutputStream) Name(com.beanit.asn1bean.compiler.x690_ber_example.Name) ByteArrayInputStream(java.io.ByteArrayInputStream) BerVisibleString(com.beanit.asn1bean.ber.types.string.BerVisibleString)

Example 35 with ReverseByteArrayOutputStream

use of com.beanit.asn1bean.ber.ReverseByteArrayOutputStream in project jasn1 by openmuc.

the class BerBoolean method encodeAndSave.

public void encodeAndSave(int encodingSizeGuess) throws IOException {
    ReverseByteArrayOutputStream os = new ReverseByteArrayOutputStream(encodingSizeGuess);
    encode(os, false);
    code = os.getArray();
}
Also used : ReverseByteArrayOutputStream(com.beanit.asn1bean.ber.ReverseByteArrayOutputStream)

Aggregations

ReverseByteArrayOutputStream (com.beanit.asn1bean.ber.ReverseByteArrayOutputStream)51 Test (org.junit.jupiter.api.Test)41 ByteArrayInputStream (java.io.ByteArrayInputStream)25 BerInteger (com.beanit.asn1bean.ber.types.BerInteger)14 BerVisibleString (com.beanit.asn1bean.ber.types.string.BerVisibleString)5 HexString (com.beanit.asn1bean.util.HexString)5 BerOctetString (com.beanit.asn1bean.ber.types.BerOctetString)4 BerUTF8String (com.beanit.asn1bean.ber.types.string.BerUTF8String)4 ProfileElement (com.beanit.asn1bean.compiler.pedefinitions.ProfileElement)4 BerAny (com.beanit.asn1bean.ber.types.BerAny)3 BerBoolean (com.beanit.asn1bean.ber.types.BerBoolean)3 BerNull (com.beanit.asn1bean.ber.types.BerNull)3 ImplicitlyRetaggedTaggedChoice (com.beanit.asn1bean.compiler.tagging_test.ImplicitlyRetaggedTaggedChoice)3 NonExtensibleSequence (com.beanit.asn1bean.compiler.extension_test.non_extensible.NonExtensibleSequence)2 PEHeader (com.beanit.asn1bean.compiler.pedefinitions.PEHeader)2 UInt15 (com.beanit.asn1bean.compiler.pedefinitions.UInt15)2 ArrayList (java.util.ArrayList)2 BerEmbeddedPdv (com.beanit.asn1bean.ber.types.BerEmbeddedPdv)1 BerObjectIdentifier (com.beanit.asn1bean.ber.types.BerObjectIdentifier)1 ExtensibleAccessSequence (com.beanit.asn1bean.compiler.extension_test.extensible_with_access.ExtensibleAccessSequence)1