KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > services > io > NewByteArrayInputStream


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

21
22 package org.apache.derby.iapi.services.io;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 /**
28     An InputStream that is like java.io.ByteArrayInputStream but supports
29     a close() call that causes all methods to throw an IOException.
30     Java's ByteInputStream has a close() method that does not do anything.
31 */

32 public final class NewByteArrayInputStream extends InputStream JavaDoc {
33
34     private byte[] data;
35     private int offset;
36     private int length;
37
38     public NewByteArrayInputStream(byte[] data) {
39         this(data, 0, data.length);
40     }
41
42     public NewByteArrayInputStream(byte[] data, int offset, int length) {
43         this.data = data;
44         this.offset = offset;
45         this.length = length;
46     }
47
48     /*
49     ** Public methods
50     */

51     public int read() throws IOException JavaDoc {
52         if (data == null)
53             throw new IOException JavaDoc();
54
55         if (length == 0)
56             return -1; // end of file
57

58         length--;
59
60         return data[offset++] & 0xff ;
61
62     }
63
64     public int read(byte b[], int off, int len) throws IOException JavaDoc {
65         if (data == null)
66             throw new IOException JavaDoc();
67
68         if (length == 0)
69             return -1;
70
71         if (len > length)
72             len = length;
73
74         System.arraycopy(data, offset, b, off, len);
75         offset += len;
76         length -= len;
77         return len;
78     }
79
80     public long skip(long count) throws IOException JavaDoc {
81         if (data == null)
82             throw new IOException JavaDoc();
83
84         if (length == 0)
85             return -1;
86
87         if (count > length)
88             count = length;
89
90         offset += (int) count;
91         length -= (int) count;
92
93         return count;
94
95     }
96
97     public int available() throws IOException JavaDoc
98     {
99         if (data == null)
100             throw new IOException JavaDoc();
101
102         return length;
103     }
104
105     public byte[] getData() { return data; }
106
107     public void close()
108     {
109         data = null;
110     }
111
112 }
113
Popular Tags