KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > util > DataByteArrayInputStream


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.util;
19
20 import java.io.DataInput JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.UTFDataFormatException JavaDoc;
24 import org.apache.activemq.util.ByteSequence;
25 /**
26  * Optimized ByteArrayInputStream that can be used more than once
27  *
28  * @version $Revision: 1.1.1.1 $
29  */

30 public final class DataByteArrayInputStream extends InputStream JavaDoc implements DataInput JavaDoc{
31     private byte[] buf;
32     private int pos;
33     private int offset;
34
35     /**
36      * Creates a <code>StoreByteArrayInputStream</code>.
37      *
38      * @param buf the input buffer.
39      */

40     public DataByteArrayInputStream(byte buf[]){
41         this.buf=buf;
42         this.pos=0;
43         this.offset = 0;
44     }
45     
46     /**
47      * Creates a <code>StoreByteArrayInputStream</code>.
48      *
49      * @param sequence the input buffer.
50      */

51     public DataByteArrayInputStream(ByteSequence sequence){
52         this.buf=sequence.getData();
53         this.offset=this.pos=sequence.getOffset();
54     }
55
56     /**
57      * Creates <code>WireByteArrayInputStream</code> with a minmalist byte array
58      */

59     public DataByteArrayInputStream(){
60         this(new byte[0]);
61     }
62
63     /**
64      *
65      * @return the size
66      */

67     public int size(){
68         return pos-offset;
69     }
70
71     /**
72      * @return the underlying data array
73      */

74     public byte[] getRawData(){
75         return buf;
76     }
77
78     /**
79      * reset the <code>StoreByteArrayInputStream</code> to use an new byte array
80      *
81      * @param newBuff
82      */

83     public void restart(byte[] newBuff){
84         buf=newBuff;
85         pos=0;
86     }
87     
88     /**
89      * reset the <code>StoreByteArrayInputStream</code> to use an new ByteSequence
90      * @param sequence
91      *
92      */

93     public void restart(ByteSequence sequence){
94         this.buf=sequence.getData();
95         this.pos=sequence.getOffset();
96     }
97
98     /**
99      * re-start the input stream - reusing the current buffer
100      *
101      * @param size
102      */

103     public void restart(int size){
104         if(buf==null||buf.length<size){
105             buf=new byte[size];
106         }
107         restart(buf);
108     }
109
110     /**
111      * Reads the next byte of data from this input stream. The value byte is returned as an <code>int</code> in the
112      * range <code>0</code> to <code>255</code>. If no byte is available because the end of the stream has been
113      * reached, the value <code>-1</code> is returned.
114      * <p>
115      * This <code>read</code> method cannot block.
116      *
117      * @return the next byte of data, or <code>-1</code> if the end of the stream has been reached.
118      */

119     public int read(){
120         return (pos<buf.length)?(buf[pos++]&0xff):-1;
121     }
122
123     /**
124      * Reads up to <code>len</code> bytes of data into an array of bytes from this input stream.
125      *
126      * @param b the buffer into which the data is read.
127      * @param off the start offset of the data.
128      * @param len the maximum number of bytes read.
129      * @return the total number of bytes read into the buffer, or <code>-1</code> if there is no more data because the
130      * end of the stream has been reached.
131      */

132     public int read(byte b[],int off,int len){
133         if(b==null){
134             throw new NullPointerException JavaDoc();
135         }
136         if(pos>=buf.length){
137             return -1;
138         }
139         if(pos+len>buf.length){
140             len=buf.length-pos;
141         }
142         if(len<=0){
143             return 0;
144         }
145         System.arraycopy(buf,pos,b,off,len);
146         pos+=len;
147         return len;
148     }
149
150     /**
151      * @return the number of bytes that can be read from the input stream without blocking.
152      */

153     public int available(){
154         return buf.length-pos;
155     }
156
157     public void readFully(byte[] b){
158         read(b,0,b.length);
159     }
160
161     public void readFully(byte[] b,int off,int len){
162         read(b,off,len);
163     }
164
165     public int skipBytes(int n){
166         if(pos+n>buf.length){
167             n=buf.length-pos;
168         }
169         if(n<0){
170             return 0;
171         }
172         pos+=n;
173         return n;
174     }
175
176     public boolean readBoolean(){
177         return read()!=0;
178     }
179
180     public byte readByte(){
181         return (byte) read();
182     }
183
184     public int readUnsignedByte(){
185         return read();
186     }
187
188     public short readShort(){
189         int ch1=read();
190         int ch2=read();
191         return (short) ((ch1<<8)+(ch2<<0));
192     }
193
194     public int readUnsignedShort(){
195         int ch1=read();
196         int ch2=read();
197         return ((ch1<<8)+(ch2<<0));
198     }
199
200     public char readChar(){
201         int ch1=read();
202         int ch2=read();
203         return (char) ((ch1<<8)+(ch2<<0));
204     }
205
206     public int readInt(){
207         int ch1=read();
208         int ch2=read();
209         int ch3=read();
210         int ch4=read();
211         return ((ch1<<24)+(ch2<<16)+(ch3<<8)+(ch4<<0));
212     }
213
214     public long readLong(){
215         return (((long) buf[pos++]<<56)+((long) (buf[pos++]&255)<<48)+((long) (buf[pos++]&255)<<40)
216                         +((long) (buf[pos++]&255)<<32)+((long) (buf[pos++]&255)<<24)+((buf[pos++]&255)<<16)
217                         +((buf[pos++]&255)<<8)+((buf[pos++]&255)<<0));
218     }
219
220     public float readFloat() throws IOException JavaDoc{
221         return Float.intBitsToFloat(readInt());
222     }
223
224     public double readDouble() throws IOException JavaDoc{
225         return Double.longBitsToDouble(readLong());
226     }
227
228     public String JavaDoc readLine(){
229         int start=pos;
230         while(pos<buf.length){
231             int c=read();
232             if(c=='\n'){
233                 break;
234             }
235             if(c=='\r'){
236                 c=read();
237                 if(c!='\n'&&c!=-1){
238                     pos--;
239                 }
240                 break;
241             }
242         }
243         return new String JavaDoc(buf,start,pos);
244     }
245
246     public String JavaDoc readUTF() throws IOException JavaDoc{
247         int length=readUnsignedShort();
248         char[] characters=new char[length];
249         int c,c2,c3;
250         int count=0;
251         int total=pos+length;
252         while(pos<total){
253             c=(int) buf[pos]&0xff;
254             if(c>127)
255                 break;
256             pos++;
257             characters[count++]=(char) c;
258         }
259         while(pos<total){
260             c=(int) buf[pos]&0xff;
261             switch(c>>4){
262             case 0:
263             case 1:
264             case 2:
265             case 3:
266             case 4:
267             case 5:
268             case 6:
269             case 7:
270                 pos++;
271                 characters[count++]=(char) c;
272                 break;
273             case 12:
274             case 13:
275                 pos+=2;
276                 if(pos>length)
277                     throw new UTFDataFormatException JavaDoc("bad string");
278                 c2=(int) buf[pos-1];
279                 if((c2&0xC0)!=0x80)
280                     throw new UTFDataFormatException JavaDoc("bad string");
281                 characters[count++]=(char) (((c&0x1F)<<6)|(c2&0x3F));
282                 break;
283             case 14:
284                 pos+=3;
285                 if(pos>length)
286                     throw new UTFDataFormatException JavaDoc("bad string");
287                 c2=(int) buf[pos-2];
288                 c3=(int) buf[pos-1];
289                 if(((c2&0xC0)!=0x80)||((c3&0xC0)!=0x80))
290                     throw new UTFDataFormatException JavaDoc("bad string");
291                 characters[count++]=(char) (((c&0x0F)<<12)|((c2&0x3F)<<6)|((c3&0x3F)<<0));
292                 break;
293             default:
294                 throw new UTFDataFormatException JavaDoc("bad string");
295             }
296         }
297         return new String JavaDoc(characters,0,count);
298     }
299 }
300
Popular Tags