KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > io > LimitedInputStream


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 package com.nightlabs.io;
32
33 import java.io.*;
34
35 /**
36  * This class is used to make sure, not more data is read from the socket as
37  * the current block (e.g. body) contains. Thus, you can use this as a wrapper
38  * for a SocketInputStream and give this to higher streams like GZipInputStream or
39  * even a reader + deserializer.
40  *
41  * @author Marco Schulze
42  * @author Marc Klinger - marc at nightlabs dot de (API documentation fixes)
43  */

44 public class LimitedInputStream extends InputStream
45 {
46   protected InputStream in;
47   protected int minLimit;
48   protected int maxLimit;
49   protected int readPos = 0;
50
51
52   /**
53    * This is the only constructor that makes sence for this stream.
54    * @param in This is my input stream
55    */

56   public LimitedInputStream(InputStream in, int minLimit, int maxLimit)
57   {
58     this.in = in;
59     this.minLimit = minLimit;
60     this.maxLimit = maxLimit;
61   }
62
63   public int available()
64   throws IOException
65   {
66     int reallyAvailable = in.available();
67
68     if (reallyAvailable < 0) {
69       if (readPos < minLimit)
70         throw new IOException("inputStream is closed; only "+readPos+" Bytes read, but minLimit = "+minLimit+" Bytes!");
71     }
72
73     if (reallyAvailable <= 0) // we are never blocking, but if in is blocking and returned 0, we should return 0 as well
74
return reallyAvailable;
75
76     int limitedAvailable = Math.min(
77       reallyAvailable,
78       maxLimit - readPos
79     );
80
81 // limitedAvailable = Math.max( // es geht darum, wieviel NONBLOCKING gelesen werden kann, also auskommentieren.
82
// minLimit - readPos,
83
// limitedAvailable
84
// );
85

86     if (limitedAvailable <= 0)
87       limitedAvailable = -1;
88
89     return limitedAvailable;
90   }
91
92 /*
93   public void close()
94   throws IOException
95   {
96
97   }
98 */

99
100 // public void mark(int readlimit)
101
// {
102
// if (in.markSupported())
103
// in.mark(readlimit);
104
// }
105

106
107   public boolean markSupported()
108   {
109     return false;
110   }
111
112   public int read()
113   throws java.io.IOException JavaDoc
114   {
115     if (readPos < maxLimit) {
116       int res = in.read();
117
118       if (res >= 0)
119         readPos++;
120       else {
121         if (readPos < minLimit)
122           throw new IOException("inputStream is closed; only "+readPos+" Bytes read, but minLimit = "+minLimit+" Bytes!");
123       }
124
125       return res;
126     }
127     else
128       return -1;
129   }
130
131   public int read(byte[] b)
132   throws IOException
133   {
134     return this.read(b, 0, b.length);
135   }
136
137   public int read(byte[] b, int off, int len)
138   throws IOException
139   {
140     if (readPos + len > maxLimit)
141       len = maxLimit - readPos;
142
143     if (len > 0) {
144       int bytesRead = in.read(b, off, len);
145
146       if (bytesRead > 0)
147         readPos += bytesRead;
148
149       if (bytesRead < 0 && readPos < minLimit)
150           throw new IOException("inputStream is closed; only "+readPos+" Bytes read, but minLimit = "+minLimit+" Bytes!");
151
152       if (bytesRead == 0)
153         throw new IllegalStateException JavaDoc("according to the documentation, InputStream.read(...) should never return 0 if len != 0, but it happened!");
154
155       return bytesRead;
156     }
157     else if (len < 0)
158       throw new IndexOutOfBoundsException JavaDoc("len < 0!");
159     else
160       return -1;
161   }
162
163 /*
164   public void reset()
165   throws IOException
166   {
167   }
168 */

169
170   public long skip(long len)
171   throws IOException
172   {
173     if (readPos + len > maxLimit)
174       len = maxLimit - readPos;
175
176     if (len > 0) {
177       long bytesSkipped = in.skip(len);
178
179       if (bytesSkipped > 0)
180         readPos += bytesSkipped;
181
182       return bytesSkipped;
183     }
184     else
185       return -1;
186   }
187
188 }
Popular Tags