1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.services.io.LimitReader |
4 | |
5 | Copyright 1999, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.services.io; |
22 | |
23 | import java.io.Reader; |
24 | import java.io.IOException; |
25 | |
26 | /** |
27 | A Reader that provides methods to limit the range that |
28 | can be read from the reader. |
29 | */ |
30 | public final class LimitReader extends Reader implements Limit |
31 | { |
32 | private int remainingCharacters; |
33 | private boolean limitInPlace; |
34 | private Reader reader; |
35 | |
36 | /** |
37 | Construct a LimitReader and call the clearLimit() method. |
38 | */ |
39 | public LimitReader(Reader reader) |
40 | { |
41 | super(); |
42 | this.reader = reader; |
43 | clearLimit(); |
44 | } |
45 | |
46 | public int read() throws IOException |
47 | { |
48 | |
49 | if (!limitInPlace) |
50 | return reader.read(); |
51 | |
52 | if (remainingCharacters == 0) |
53 | return -1; // end of file |
54 | |
55 | |
56 | int value = reader.read(); |
57 | if (value >= 0) |
58 | remainingCharacters--; |
59 | return value; |
60 | |
61 | } |
62 | |
63 | public int read(char c[], int off, int len) throws IOException |
64 | { |
65 | if (!limitInPlace) |
66 | return reader.read(c, off, len); |
67 | |
68 | if (remainingCharacters == 0) |
69 | return -1; |
70 | |
71 | if (remainingCharacters < len) |
72 | { |
73 | len = remainingCharacters; // end of file |
74 | } |
75 | |
76 | len = reader.read(c, off, len); |
77 | if (len >= 0) |
78 | remainingCharacters -= len; |
79 | return len; |
80 | } |
81 | |
82 | public long skip(long count) |
83 | throws IOException |
84 | { |
85 | if (!limitInPlace) |
86 | return reader.skip(count); |
87 | |
88 | if (remainingCharacters == 0) |
89 | return 0; // end of file |
90 | |
91 | if (remainingCharacters < count) |
92 | count = remainingCharacters; |
93 | |
94 | count = reader.skip(count); |
95 | remainingCharacters -= count; |
96 | return count; |
97 | } |
98 | |
99 | public void close() |
100 | throws IOException |
101 | { |
102 | reader.close(); |
103 | } |
104 | |
105 | /** |
106 | Set the limit of the stream that can be read. After this |
107 | call up to and including length characters can be read from |
108 | or skipped in the stream. |
109 | Any attempt to read more than length characters will |
110 | result in an EOFException |
111 | |
112 | @exception IOException IOException from some underlying stream |
113 | @exception EOFException The set limit would exceed |
114 | the available data in the stream. |
115 | */ |
116 | public void setLimit(int length) |
117 | { |
118 | remainingCharacters = length; |
119 | limitInPlace = true; |
120 | return; |
121 | } |
122 | |
123 | /** |
124 | * return limit of the stream that can be read without throwing |
125 | * EOFException |
126 | * @return the remaining characters left to be read from the stream |
127 | */ |
128 | public final int getLimit() |
129 | { |
130 | return remainingCharacters; |
131 | } |
132 | |
133 | /** |
134 | Clear any limit set by setLimit. After this call no limit checking |
135 | will be made on any read until a setLimit()) call is made. |
136 | |
137 | @return the number of bytes within the limit that have not been read. |
138 | -1 if not limit was set. |
139 | */ |
140 | public int clearLimit() |
141 | { |
142 | int leftOver = remainingCharacters; |
143 | limitInPlace = false; |
144 | remainingCharacters = -1; |
145 | return leftOver; |
146 | } |
147 | } |