KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22
23
24 /**
25  * Very similar to the java.io.ByteArrayInputStream but this version
26  * is not thread safe.
27  */

28 public class ByteArrayInputStream extends InputStream JavaDoc {
29
30     byte buffer[];
31     int limit;
32     int pos;
33     int mark;
34
35     public ByteArrayInputStream(byte data[]) {
36         this(data, 0, data.length);
37     }
38     
39     public ByteArrayInputStream(ByteSequence sequence) {
40         this(sequence.getData(), sequence.getOffset(), sequence.getLength());
41     }
42
43     public ByteArrayInputStream(byte data[], int offset, int size) {
44         this.buffer = data;
45         this.mark= this.pos = offset;
46         this.limit = offset+size;
47     }
48     
49     public int read() throws IOException JavaDoc {
50         if( pos < limit )
51             return buffer[pos++] & 0xff;
52         else
53             return -1;
54     }
55
56     public int read(byte[] b) throws IOException JavaDoc {
57         return read(b, 0, b.length);
58     }
59     
60     public int read(byte b[], int off, int len) {
61         if (pos < limit) {
62             len = Math.min(len, limit-pos);
63             if (len > 0) {
64                 System.arraycopy(buffer, pos, b, off, len);
65                 pos += len;
66             }
67             return len;
68         } else {
69             return -1;
70         }
71     }
72
73     public long skip(long len) throws IOException JavaDoc {
74         if (pos < limit) {
75             len = Math.min(len, limit-pos);
76             if (len > 0) {
77                 pos += len;
78             }
79             return len;
80         } else {
81             return -1;
82         }
83     }
84     
85     public int available() {
86         return limit - pos;
87     }
88
89     public boolean markSupported() {
90         return true;
91     }
92     
93     public void mark(int markpos) {
94         mark = pos;
95     }
96     
97     public void reset() {
98         pos = mark;
99     }
100 }
101
Popular Tags