KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.io.LimitReader
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.Reader JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 /**
28     A Reader that provides methods to limit the range that
29     can be read from the reader.
30 */

31 public final class LimitReader extends Reader JavaDoc implements Limit
32 {
33     private int remainingCharacters;
34     private boolean limitInPlace;
35     private Reader JavaDoc reader;
36
37     /**
38         Construct a LimitReader and call the clearLimit() method.
39     */

40     public LimitReader(Reader JavaDoc reader)
41     {
42         super();
43         this.reader = reader;
44         clearLimit();
45     }
46
47     public int read() throws IOException JavaDoc
48     {
49
50         if (!limitInPlace)
51             return reader.read();
52         
53         if (remainingCharacters == 0)
54             return -1; // end of file
55

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

92         if (remainingCharacters < count)
93             count = remainingCharacters;
94
95         count = reader.skip(count);
96         remainingCharacters -= count;
97         return count;
98     }
99
100     public void close()
101         throws IOException JavaDoc
102     {
103         reader.close();
104     }
105
106     /**
107         Set the limit of the stream that can be read. After this
108         call up to and including length characters can be read from
109         or skipped in the stream.
110         Any attempt to read more than length characters will
111         result in an EOFException
112
113         @exception IOException IOException from some underlying stream
114         @exception EOFException The set limit would exceed
115         the available data in the stream.
116     */

117     public void setLimit(int length)
118     {
119         remainingCharacters = length;
120         limitInPlace = true;
121         return;
122     }
123     
124     /**
125      * return limit of the stream that can be read without throwing
126      * EOFException
127      * @return the remaining characters left to be read from the stream
128      */

129     public final int getLimit()
130     {
131         return remainingCharacters;
132     }
133
134     /**
135         Clear any limit set by setLimit. After this call no limit checking
136         will be made on any read until a setLimit()) call is made.
137
138         @return the number of bytes within the limit that have not been read.
139         -1 if not limit was set.
140     */

141     public int clearLimit()
142     {
143         int leftOver = remainingCharacters;
144         limitInPlace = false;
145         remainingCharacters = -1;
146         return leftOver;
147     }
148 }
149
Popular Tags