Following is sample code that illustrates how you can process multiple PDUs and skip invalid PDUs. Decoding is done in two passes. In the first pass, the entire BER-encoded PDU is buffered into byte[] using the readTLV() method of the com.oss.util.BERTool.BERReader utility class. In the second pass, the valid PDU is decoded. If errors occur during the second pass, the code simply discards the incorrectly encoded PDU that is buffered into byte[]:
BERTool.BERReader berReader = new BERTool.BERReader(); FileInputStream file = new FileInputStream("encoding.ber"); try (InputStream in = new BufferedInputStream(file, 4096)) { while (in.available() > 0) { // Read the encoded PDU into buffer // and advance input stream to the // beginning of the next encoded PDU. byte[] berMessage = berReader.readTLV(in); try { ByteBuffer in1 = ByteBuffer.wrap(berMessage); AbstractData data = coder.decode(in1, new SomePDU()); } catch (Exception e) { // Report that PDU cannot be decoded // due to some error. } } } catch (Exception e) { // Fatal error. BER encoding has invalid // tag/length header. Error recovery is // not possible. }
The samples included with some of the Knowledge Center answers are meant for your general understanding of the OSS products. Different versions of the products might produce slightly different outputs. Consult the products documentation and samples for the most up-to-date products information and code examples.
Test drive the OSS Nokalva ASN.1, LTE, and XML Tools now! Your trial includes complete software, documentation, sample programs, free 24x7 technical support and more.
Our expert personnel can help you learn ASN.1!
We offer 4-day ASN.1 courses at our headquarters or your premises.