KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Ostermiller > util > SizeLimitInputStream


1 /*
2  * Input stream wrapper with a byte limit.
3  * Copyright (C) 2004 Stephen Ostermiller
4  * http://ostermiller.org/contact.pl?regarding=Java+Utilities
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See COPYING.TXT for details.
17  */

18 package com.Ostermiller.util;
19
20 import java.io.*;
21
22 /**
23  * An input stream wrapper that will read only a set number of bytes from the
24  * underlying stream.
25  *
26  * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
27  * @since ostermillerutils 1.04.00
28  */

29 public class SizeLimitInputStream extends InputStream {
30
31     /**
32      * The input stream that is being protected.
33      * All methods should be forwarded to it,
34      * after checking the size that has been read.
35      *
36      * @since ostermillerutils 1.04.00
37      */

38     protected InputStream in;
39
40     /**
41      * The number of bytes to read at most from this
42      * Stream. Read methods should
43      * check to ensure that bytesRead never
44      * exceeds maxBytesToRead.
45      *
46      * @since ostermillerutils 1.04.00
47      */

48     protected long maxBytesToRead = 0;
49
50     /**
51      * The number of bytes that have been read
52      * from this stream. Read methods should
53      * check to ensure that bytesRead never
54      * exceeds maxBytesToRead.
55      *
56      * @since ostermillerutils 1.04.00
57      */

58     protected long bytesRead = 0;
59
60     /**
61      * Get the number of bytes actually read
62      * from this stream.
63      *
64      * @return number of bytes that have already been taken from this stream.
65      *
66      * @since ostermillerutils 1.04.00
67      */

68     public long getBytesRead(){
69         return bytesRead;
70     }
71
72     /**
73      * Get the maximum number of bytes left to read
74      * before the limit (set in the constructor) is reached.
75      *
76      * @return The number of bytes that (at a maximum) are left to be taken from this stream.
77      *
78      * @since ostermillerutils 1.04.00
79      */

80     public long getBytesLeft(){
81         return maxBytesToRead - bytesRead;
82     }
83
84     /**
85      * Tell whether the number of bytes specified
86      * in the constructor have been read yet.
87      *
88      * @return true iff the specified number of bytes have all been read.
89      *
90      * @since ostermillerutils 1.04.00
91      */

92     public boolean allBytesRead(){
93         return getBytesLeft() == 0;
94     }
95
96     /**
97      * Get the number of total bytes (including bytes already read)
98      * that can be read from this stream (as set in the constructor).
99      *
100      * @since ostermillerutils 1.04.00
101      */

102     public long getMaxBytesToRead(){
103         return maxBytesToRead;
104     }
105
106     /**
107      * Create a new size limit input stream from
108      * another stream given a size limit.
109      *
110      * @param in The input stream.
111      * @param maxBytesToRead the max number of bytes to allow to be read from the underlying stream.
112      *
113      * @since ostermillerutils 1.04.00
114      */

115     public SizeLimitInputStream(InputStream in, long maxBytesToRead){
116         this.in = in;
117         this.maxBytesToRead = maxBytesToRead;
118     }
119
120     /**
121      * {@inheritDoc}
122      */

123     public int read() throws IOException {
124         if (bytesRead >= maxBytesToRead){
125             return -1;
126         }
127         int b = in.read();
128         if(b != -1){
129             bytesRead++;
130         }
131         return b;
132     }
133
134     /**
135      * {@inheritDoc}
136      */

137     public int read(byte[] b) throws IOException {
138         return this.read(b, 0, b.length);
139     }
140
141     /**
142      * {@inheritDoc}
143      */

144     public int read(byte[] b, int off, int len) throws IOException {
145         if (bytesRead >= maxBytesToRead){
146             return -1;
147         }
148         long bytesLeft = getBytesLeft();
149         if (len > bytesLeft){
150             len = (int)bytesLeft;
151         }
152         int bytesJustRead = in.read(b, off, len);
153         bytesRead += bytesJustRead;
154         return bytesJustRead;
155     }
156
157     /**
158      * {@inheritDoc}
159      */

160     public long skip(long n) throws IOException {
161         if (bytesRead >= maxBytesToRead){
162             return -1;
163         }
164         long bytesLeft = getBytesLeft();
165         if (n > bytesLeft){
166             n = bytesLeft;
167         }
168         return in.skip(n);
169     }
170
171     /**
172      * {@inheritDoc}
173      */

174     public int available() throws IOException {
175         int available = in.available();
176         long bytesLeft = getBytesLeft();
177         if (available > bytesLeft){
178             available = (int)bytesLeft;
179         }
180         return available;
181     }
182
183     /**
184      * Close this stream and underlying streams.
185      * Calling this method may make data on the
186      * underlying stream unavailable.
187      * <p>
188      * Consider wrapping this stream in a NoCloseStream
189      * so that clients can
190      * call close() with no effect.
191      *
192      * @since ostermillerutils 1.04.00
193      */

194     public void close() throws IOException {
195         in.close();
196     }
197
198     /**
199      * {@inheritDoc}
200      */

201     public void mark(int readlimit) {
202         in.mark(readlimit);
203     }
204
205     /**
206      * {@inheritDoc}
207      */

208     public void reset() throws IOException {
209         in.reset();
210     }
211
212     /**
213      * {@inheritDoc}
214      */

215     public boolean markSupported(){
216         return in.markSupported();
217     }
218 }
219
Popular Tags