KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.io.LimitInputStream
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.FilterInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 /**
29     An abstract InputStream that provides abstract methods to limit the range that
30     can be read from the stream.
31 */

32 public class LimitInputStream extends FilterInputStream JavaDoc implements Limit {
33
34     protected int remainingBytes;
35     protected boolean limitInPlace;
36
37     /**
38         Construct a LimitInputStream and call the clearLimit() method.
39     */

40     public LimitInputStream(InputStream JavaDoc in) {
41         super(in);
42         clearLimit();
43     }
44
45     public int read() throws IOException JavaDoc {
46
47         if (!limitInPlace)
48             return super.read();
49         
50         if (remainingBytes == 0)
51             return -1; // end of file
52

53         
54         int value = super.read();
55         if (value >= 0)
56             remainingBytes--;
57         return value;
58
59     }
60
61     public int read(byte b[], int off, int len) throws IOException JavaDoc {
62
63         if (!limitInPlace)
64             return super.read(b, off, len);
65
66
67         if (remainingBytes == 0)
68             return -1;
69
70         if (remainingBytes < len) {
71             len = remainingBytes; // end of file
72
}
73
74         len = super.read(b, off, len);
75         if (len > 0)
76             remainingBytes -= len;
77
78         return len;
79     }
80
81     public long skip(long count) throws IOException JavaDoc {
82         if (!limitInPlace)
83             return super.skip(count);
84
85         if (remainingBytes == 0)
86             return 0; // end of file
87

88         if (remainingBytes < count)
89             count = remainingBytes;
90
91         count = super.skip(count);
92         remainingBytes -= count;
93         return count;
94     }
95
96     public int available() throws IOException JavaDoc {
97
98         if (!limitInPlace)
99             return super.available();
100
101         if (remainingBytes == 0)
102             return 0; // end of file
103

104         int actualLeft = super.available();
105
106         if (remainingBytes < actualLeft)
107             return remainingBytes;
108         
109
110         return actualLeft;
111     }
112
113
114     /**
115         Set the limit of the stream that can be read. After this
116         call up to and including length bytes can be read from or skipped in
117         the stream. Any attempt to read more than length bytes will
118         result in an EOFException
119
120         @exception IOException IOException from some underlying stream
121         @exception EOFException The set limit would exceed
122         the available data in the stream.
123     */

124     public void setLimit(int length) {
125         remainingBytes = length;
126         limitInPlace = true;
127         return;
128     }
129
130     /**
131         Clear any limit set by setLimit. After this call no limit checking
132         will be made on any read until a setLimit()) call is made.
133
134         @return the number of bytes within the limit that have not been read.
135         -1 if no limit was set.
136     */

137     public int clearLimit() {
138         int leftOver = remainingBytes;
139         limitInPlace = false;
140         remainingBytes = -1;
141         return leftOver;
142     }
143
144     public void setInput(InputStream JavaDoc in) {
145         this.in = in;
146     }
147 }
148
Popular Tags