KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > asn1 > BERInputStream


1 /*_############################################################################
2   _##
3   _## SNMP4J - BERInputStream.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21 package org.snmp4j.asn1;
22
23 import java.io.*;
24 import java.nio.ByteBuffer JavaDoc;
25 import java.nio.BufferUnderflowException JavaDoc;
26
27
28 /**
29  * The <code>BERInputStream</code> class wraps a <code>ByteBuffer</code> and
30  * implements the <code>InputStream</code> abstract class.
31  * positions in the input stream.
32  *
33  * @author Frank Fock
34  * @version 1.6.1
35  */

36
37 public class BERInputStream extends InputStream {
38
39   private ByteBuffer JavaDoc buffer;
40
41   public BERInputStream(ByteBuffer JavaDoc buffer) {
42     this.buffer = buffer;
43     buffer.mark();
44   }
45
46   public ByteBuffer JavaDoc getBuffer() {
47     return buffer;
48   }
49
50   public void setBuffer(ByteBuffer JavaDoc buf) {
51     this.buffer = buf;
52   }
53
54   public int read() throws java.io.IOException JavaDoc {
55     try {
56       return (buffer.get() & 0xFF);
57     }
58     catch (BufferUnderflowException JavaDoc ex) {
59       throw new IOException("Unexpected end of input stream at position "+
60                             getPosition());
61     }
62   }
63
64   /**
65    * Returns the number of bytes that can be read (or skipped over) from this
66    * input stream without blocking by the next caller of a method for this input
67    * stream.
68    *
69    * @return the number of bytes that can be read from this input stream without
70    * blocking.
71    * @throws IOException if an I/O error occurs.
72    */

73   public int available() throws IOException {
74     return buffer.remaining();
75   }
76
77   /**
78    * Closes this input stream and releases any system resources associated with
79    * the stream.
80    *
81    * @throws IOException if an I/O error occurs.
82    */

83   public void close() throws IOException {
84   }
85
86   /**
87    * Marks the current position in this input stream.
88    *
89    * @param readlimit the maximum limit of bytes that can be read before the
90    * mark position becomes invalid.
91    */

92   public synchronized void mark(int readlimit) {
93     buffer.mark();
94   }
95
96   /**
97    * Tests if this input stream supports the <code>mark</code> and
98    * <code>reset</code> methods.
99    *
100    * @return <code>true</code> if this stream instance supports the mark and
101    * reset methods; <code>false</code> otherwise.
102    */

103   public boolean markSupported() {
104     return true;
105   }
106
107   /**
108    * Reads some number of bytes from the input stream and stores them into the
109    * buffer array <code>b</code>.
110    *
111    * @param b the buffer into which the data is read.
112    * @return the total number of bytes read into the buffer, or <code>-1</code>
113    * is there is no more data because the end of the stream has been reached.
114    * @throws IOException if an I/O error occurs.
115    */

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   /**
126    * Reads up to <code>len</code> bytes of data from the input stream into an
127    * array of bytes.
128    *
129    * @param b the buffer into which the data is read.
130    * @param off the start offset in array <code>b</code> at which the data is
131    * written.
132    * @param len the maximum number of bytes to read.
133    * @return the total number of bytes read into the buffer, or <code>-1</code>
134    * if there is no more data because the end of the stream has been reached.
135    * @throws IOException if an I/O error occurs.
136    */

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   /**
147    * Repositions this stream to the position at the time the <code>mark</code>
148    * method was last called on this input stream.
149    *
150    * @throws IOException if this stream has not been marked or if the mark has
151    * been invalidated.
152    */

153   public synchronized void reset() throws IOException {
154     buffer.reset();
155   }
156
157   /**
158    * Skips over and discards <code>n</code> bytes of data from this input stream.
159    *
160    * @param n the number of bytes to be skipped.
161    * @return the actual number of bytes skipped.
162    * @throws IOException if an I/O error occurs.
163    */

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   /**
171    * Gets the current position in the underlying <code>buffer</code>.
172    * @return
173    * <code>buffer.position()</code>.
174    */

175   public long getPosition() {
176     return buffer.position();
177   }
178
179   /**
180    * Checks whether a mark has been set on the input stream. This can be used
181    * to determine whether the value returned by {@link #available()} is limited
182    * by a readlimit set when the mark has been set.
183    * @return
184    * always <code>true</code>. If no mark has been set explicitly, the mark
185    * is set to the initial position (i.e. zero).
186    */

187   public boolean isMarked() {
188     return true;
189   }
190
191   /**
192    * Gets the total number of bytes that can be read from this input stream.
193    * @return
194    * the number of bytes that can be read from this stream.
195    */

196   public int getAvailableBytes() {
197     return buffer.limit();
198   }
199
200 }
201
Popular Tags