1 19 20 package com.maverick.util; 21 22 import java.io.IOException ; 23 24 29 public class SimpleASNReader { 30 private byte[] data; 31 private int offset; 32 33 38 public SimpleASNReader(byte[] data) { 39 this.data = data; 40 this.offset = 0; 41 } 42 43 50 public void assertByte(int b) throws IOException { 51 int x = getByte(); 52 53 if (x != b) { 54 throw new IOException ("Assertion failed, next byte value is " 55 + Integer.toHexString(x) + " instead of asserted " 56 + Integer.toHexString(b)); 57 } 58 } 59 60 65 public int getByte() { 66 return data[offset++] & 0xff; 67 } 68 69 74 public byte[] getData() { 75 int length = getLength(); 76 77 return getData(length); 78 } 79 80 85 public int getLength() { 86 int b = data[offset++] & 0xff; 87 88 if ( (b & 0x80) != 0) { 89 int length = 0; 90 91 for (int bytes = b & 0x7f; bytes > 0; bytes--) { 92 length <<= 8; 93 length |= (data[offset++] & 0xff); 94 } 95 96 return length; 97 } 98 99 return b; 100 } 101 102 private byte[] getData(int length) { 103 byte[] result = new byte[length]; 104 System.arraycopy(data, offset, result, 0, length); 105 offset += length; 106 107 return result; 108 } 109 110 115 public boolean hasMoreData() { 116 return offset < data.length; 117 } 118 } 119 | Popular Tags |