1 20 21 package org.snmp4j.asn1; 22 23 import java.io.*; 24 import java.nio.ByteBuffer ; 25 import java.nio.BufferUnderflowException ; 26 27 28 36 37 public class BERInputStream extends InputStream { 38 39 private ByteBuffer buffer; 40 41 public BERInputStream(ByteBuffer buffer) { 42 this.buffer = buffer; 43 buffer.mark(); 44 } 45 46 public ByteBuffer getBuffer() { 47 return buffer; 48 } 49 50 public void setBuffer(ByteBuffer buf) { 51 this.buffer = buf; 52 } 53 54 public int read() throws java.io.IOException { 55 try { 56 return (buffer.get() & 0xFF); 57 } 58 catch (BufferUnderflowException ex) { 59 throw new IOException("Unexpected end of input stream at position "+ 60 getPosition()); 61 } 62 } 63 64 73 public int available() throws IOException { 74 return buffer.remaining(); 75 } 76 77 83 public void close() throws IOException { 84 } 85 86 92 public synchronized void mark(int readlimit) { 93 buffer.mark(); 94 } 95 96 103 public boolean markSupported() { 104 return true; 105 } 106 107 116 public int read(byte[] b) throws IOException { 117 if (buffer.remaining() <= 0) { 118 return -1; 119 } 120 int read = Math.min(buffer.remaining(), b.length); 121 buffer.get(b, 0, read); 122 return read; 123 } 124 125 137 public int read(byte[] b, int off, int len) throws IOException { 138 if (buffer.remaining() <= 0) { 139 return -1; 140 } 141 int read = Math.min(buffer.remaining(), b.length); 142 buffer.get(b, off, len); 143 return read; 144 } 145 146 153 public synchronized void reset() throws IOException { 154 buffer.reset(); 155 } 156 157 164 public long skip(long n) throws IOException { 165 long skipped = Math.min(buffer.remaining(), n); 166 buffer.position((int)(buffer.position() + skipped)); 167 return skipped; 168 } 169 170 175 public long getPosition() { 176 return buffer.position(); 177 } 178 179 187 public boolean isMarked() { 188 return true; 189 } 190 191 196 public int getAvailableBytes() { 197 return buffer.limit(); 198 } 199 200 } 201 | Popular Tags |