Search in sources :

Example 41 with ReverseByteArrayOutputStream

use of org.openmuc.jasn1.ber.ReverseByteArrayOutputStream in project jasn1 by openmuc.

the class BerIntegerTest method implicitEncoding6.

@Test
public void implicitEncoding6() throws IOException {
    ReverseByteArrayOutputStream berBAOStream = new ReverseByteArrayOutputStream(50);
    IntegerUnivPrim testInteger = new IntegerUnivPrim(BigInteger.valueOf(-128));
    int length = testInteger.encode(berBAOStream, false);
    Assert.assertEquals(2, length);
    byte[] expectedBytes = new byte[] { 0x01, (byte) 0x80 };
    Assert.assertArrayEquals(expectedBytes, berBAOStream.getArray());
}
Also used : ReverseByteArrayOutputStream(org.openmuc.jasn1.ber.ReverseByteArrayOutputStream) Test(org.junit.Test)

Example 42 with ReverseByteArrayOutputStream

use of org.openmuc.jasn1.ber.ReverseByteArrayOutputStream in project jasn1 by openmuc.

the class BerRealTest method coding2dot0.

@Test
public void coding2dot0() throws IOException {
    final BerReal orig = new BerReal(2.0);
    final ReverseByteArrayOutputStream baos = new ReverseByteArrayOutputStream(100, true);
    orig.encode(baos, true);
    final BerReal decoded = new BerReal();
    decoded.decode(new ByteArrayInputStream(baos.getArray()), true);
    Assert.assertEquals(orig.value, decoded.value, 0.001);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ReverseByteArrayOutputStream(org.openmuc.jasn1.ber.ReverseByteArrayOutputStream) Test(org.junit.Test)

Example 43 with ReverseByteArrayOutputStream

use of org.openmuc.jasn1.ber.ReverseByteArrayOutputStream in project jasn1 by openmuc.

the class BerRealTest method coding1dot0.

@Test
public void coding1dot0() throws IOException {
    final BerReal orig = new BerReal(1.0);
    final ReverseByteArrayOutputStream baos = new ReverseByteArrayOutputStream(100, true);
    orig.encode(baos, true);
    final BerReal decoded = new BerReal();
    decoded.decode(new ByteArrayInputStream(baos.getArray()), true);
    Assert.assertEquals(orig.value, decoded.value, 0.001);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ReverseByteArrayOutputStream(org.openmuc.jasn1.ber.ReverseByteArrayOutputStream) Test(org.junit.Test)

Example 44 with ReverseByteArrayOutputStream

use of org.openmuc.jasn1.ber.ReverseByteArrayOutputStream in project jasn1 by openmuc.

the class Jasn1Sample 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 BerOutputStream. This can speed up things if the code
    // for certain structures is known and does not change.
    Name name = new Name();
    name.code = new byte[] { (byte) 0x10, (byte) 0x1A, (byte) 0x04, (byte) 0x4a, (byte) 0x6f, (byte) 0x68, (byte) 0x6e, (byte) 0x1A, (byte) 0x01, (byte) 0x50, (byte) 0x1A, (byte) 0x05, (byte) 0x53, (byte) 0x6d, (byte) 0x69, (byte) 0x74, (byte) 0x68 };
    BerVisibleString title = new BerVisibleString("Director".getBytes());
    EmployeeNumber number = new EmployeeNumber(51);
    Date dateOfHire = new Date("19710917".getBytes());
    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()));
    // 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()));
    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(DatatypeConverter.printHexBinary(os.getArray()));
    InputStream is = new ByteArrayInputStream(os.getArray());
    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(org.openmuc.jasn1.compiler.x690_ber_example.ChildInformation) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) EmployeeNumber(org.openmuc.jasn1.compiler.x690_ber_example.EmployeeNumber) BerVisibleString(org.openmuc.jasn1.ber.types.string.BerVisibleString) PersonnelRecord(org.openmuc.jasn1.compiler.x690_ber_example.PersonnelRecord) Date(org.openmuc.jasn1.compiler.x690_ber_example.Date) ReverseByteArrayOutputStream(org.openmuc.jasn1.ber.ReverseByteArrayOutputStream) Name(org.openmuc.jasn1.compiler.x690_ber_example.Name)

Aggregations

ReverseByteArrayOutputStream (org.openmuc.jasn1.ber.ReverseByteArrayOutputStream)44 Test (org.junit.Test)38 ByteArrayInputStream (java.io.ByteArrayInputStream)25 BerInteger (org.openmuc.jasn1.ber.types.BerInteger)11 BerOctetString (org.openmuc.jasn1.ber.types.BerOctetString)4 BerUTF8String (org.openmuc.jasn1.ber.types.string.BerUTF8String)4 ProfileElement (org.openmuc.jasn1.compiler.pedefinitions.ProfileElement)4 BerBoolean (org.openmuc.jasn1.ber.types.BerBoolean)3 BerNull (org.openmuc.jasn1.ber.types.BerNull)3 BerVisibleString (org.openmuc.jasn1.ber.types.string.BerVisibleString)3 ImplicitlyRetaggedTaggedChoice (org.openmuc.jasn1.compiler.tagging_test.ImplicitlyRetaggedTaggedChoice)3 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 BerAny (org.openmuc.jasn1.ber.types.BerAny)2 PEHeader (org.openmuc.jasn1.compiler.pedefinitions.PEHeader)2 UInt15 (org.openmuc.jasn1.compiler.pedefinitions.UInt15)2 RetaggedUntaggedChoice (org.openmuc.jasn1.compiler.tagging_test.RetaggedUntaggedChoice)2 ChildInformation (org.openmuc.jasn1.compiler.x690_ber_example.ChildInformation)2 Date (org.openmuc.jasn1.compiler.x690_ber_example.Date)2 EmployeeNumber (org.openmuc.jasn1.compiler.x690_ber_example.EmployeeNumber)2