View Javadoc

1   /*** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  
19  package org.codehaus.activemq.message.util;
20  import java.io.ByteArrayInputStream;
21  
22  /***
23   * Optimized ByteArrayInputStream that can be used more than once. Which is nice.
24   * 
25   * @version $Revision: 1.1 $
26   */
27  public class WireByteArrayInputStream extends ByteArrayInputStream {
28      /***
29       * Creates a <code>WireByteArrayInputStream</code>.
30       * 
31       * @param buf the input buffer.
32       */
33      public WireByteArrayInputStream(byte buf[]) {
34          super(buf);
35      }
36  
37      /***
38       * Creates <code>WireByteArrayInputStream</code> that uses <code>buf</code> as its buffer array.
39       * 
40       * @param buf the input buffer.
41       * @param offset the offset in the buffer of the first byte to read.
42       * @param length the maximum number of bytes to read from the buffer.
43       */
44      public WireByteArrayInputStream(byte buf[], int offset, int length) {
45          super(buf, offset, length);
46      }
47  
48      /***
49       * Creates <code>WireByteArrayInputStream</code> with a minmalist byte array
50       */
51      public WireByteArrayInputStream() {
52          super(new byte[0]);
53      }
54  
55      /***
56       * reset the <code>WireByteArrayInputStream</code> to use an new byte array
57       * 
58       * @param newBuff buffer to use
59       * @param offset the offset in the buffer of the first byte to read.
60       * @param length the maximum number of bytes to read from the buffer.
61       */
62      public void restart(byte[] newBuff, int offset, int length) {
63          buf = newBuff;
64          pos = offset;
65          count = Math.min(offset + length, newBuff.length);
66          mark = offset;
67      }
68  
69      /***
70       * reset the <code>WireByteArrayInputStream</code> to use an new byte array
71       * 
72       * @param newBuff
73       */
74      public void restart(byte[] newBuff) {
75          restart(newBuff, 0, newBuff.length);
76      }
77  
78      /***
79       * Reads the next byte of data from this input stream. The value byte is returned as an <code>int</code> in the
80       * range <code>0</code> to <code>255</code>. If no byte is available because the end of the stream has been
81       * reached, the value <code>-1</code> is returned.
82       * <p>
83       * This <code>read</code> method cannot block.
84       * 
85       * @return the next byte of data, or <code>-1</code> if the end of the stream has been reached.
86       */
87      public int read() {
88          return (pos < count) ? (buf[pos++] & 0xff) : -1;
89      }
90  
91      /***
92       * Reads up to <code>len</code> bytes of data into an array of bytes from this input stream.
93       * 
94       * @param b the buffer into which the data is read.
95       * @param off the start offset of the data.
96       * @param len the maximum number of bytes read.
97       * @return the total number of bytes read into the buffer, or <code>-1</code> if there is no more data because the
98       * end of the stream has been reached.
99       */
100     public int read(byte b[], int off, int len) {
101         if (b == null) {
102             throw new NullPointerException();
103         }
104         else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) {
105             throw new IndexOutOfBoundsException();
106         }
107         if (pos >= count) {
108             return -1;
109         }
110         if (pos + len > count) {
111             len = count - pos;
112         }
113         if (len <= 0) {
114             return 0;
115         }
116         System.arraycopy(buf, pos, b, off, len);
117         pos += len;
118         return len;
119     }
120 
121     /***
122      * @return the number of bytes that can be read from the input stream without blocking.
123      */
124     public int available() {
125         return count - pos;
126     }
127 }