Lines 180-187
Link Here
|
180 |
*/ |
180 |
*/ |
181 |
/** |
181 |
/** |
182 |
* Returns the given input stream's contents as a character array. |
182 |
* Returns the given input stream's contents as a character array. |
183 |
* If a length is specified (ie. if length != -1), only length chars |
183 |
* If a length is specified (ie. if length != -1), this represents the number of bytes in the stream. |
184 |
* are returned. Otherwise all chars in the stream are returned. |
|
|
185 |
* Note this doesn't close the stream. |
184 |
* Note this doesn't close the stream. |
186 |
* @throws IOException if a problem occured reading the stream. |
185 |
* @throws IOException if a problem occured reading the stream. |
187 |
*/ |
186 |
*/ |
Lines 197-270
Link Here
|
197 |
reader = new InputStreamReader(stream); |
196 |
reader = new InputStreamReader(stream); |
198 |
} |
197 |
} |
199 |
char[] contents; |
198 |
char[] contents; |
|
|
199 |
int totalRead = 0; |
200 |
if (length == -1) { |
200 |
if (length == -1) { |
201 |
contents = CharOperation.NO_CHAR; |
201 |
contents = CharOperation.NO_CHAR; |
202 |
int contentsLength = 0; |
|
|
203 |
int amountRead = -1; |
204 |
do { |
205 |
int amountRequested = Math.max(stream.available(), DEFAULT_READING_SIZE); // read at least 8K |
206 |
|
207 |
// resize contents if needed |
208 |
if (contentsLength + amountRequested > contents.length) { |
209 |
System.arraycopy( |
210 |
contents, |
211 |
0, |
212 |
contents = new char[contentsLength + amountRequested], |
213 |
0, |
214 |
contentsLength); |
215 |
} |
216 |
|
217 |
// read as many chars as possible |
218 |
amountRead = reader.read(contents, contentsLength, amountRequested); |
219 |
|
220 |
if (amountRead > 0) { |
221 |
// remember length of contents |
222 |
contentsLength += amountRead; |
223 |
} |
224 |
} while (amountRead != -1); |
225 |
|
226 |
// Do not keep first character for UTF-8 BOM encoding |
227 |
int start = 0; |
228 |
if (contentsLength > 0 && UTF_8.equals(encoding)) { |
229 |
if (contents[0] == 0xFEFF) { // if BOM char then skip |
230 |
contentsLength--; |
231 |
start = 1; |
232 |
} |
233 |
} |
234 |
// resize contents if necessary |
235 |
if (contentsLength < contents.length) { |
236 |
System.arraycopy( |
237 |
contents, |
238 |
start, |
239 |
contents = new char[contentsLength], |
240 |
0, |
241 |
contentsLength); |
242 |
} |
243 |
} else { |
202 |
} else { |
|
|
203 |
// length is a good guess when the encoding produces less or the same amount of characters than the file length |
244 |
contents = new char[length]; |
204 |
contents = new char[length]; |
245 |
int len = 0; |
205 |
do { |
246 |
int readSize = 0; |
206 |
int amountRead = reader.read(contents, totalRead, length - totalRead); |
247 |
while ((readSize != -1) && (len != length)) { |
207 |
if (amountRead < 0) |
248 |
// See PR 1FMS89U |
208 |
break; |
249 |
// We record first the read size. In this case len is the actual read size. |
209 |
totalRead += amountRead; |
250 |
len += readSize; |
210 |
} while (totalRead != length); |
251 |
readSize = reader.read(contents, len, length - len); |
211 |
} |
252 |
} |
212 |
|
253 |
// Do not keep first character for UTF-8 BOM encoding |
213 |
// read remaining characters |
254 |
int start = 0; |
214 |
while (true) { |
255 |
if (length > 0 && UTF_8.equals(encoding)) { |
215 |
// avoid creating a char[] if there is no more char to read |
256 |
if (contents[0] == 0xFEFF) { // if BOM char then skip |
216 |
int current = reader.read(); |
257 |
len--; |
217 |
if (current < 0) |
258 |
start = 1; |
218 |
break; |
259 |
} |
219 |
|
|
|
220 |
int amountRequested = Math.max(stream.available(), DEFAULT_READING_SIZE); // read at least 8K |
221 |
|
222 |
// resize contents if needed |
223 |
if (totalRead + 1 + amountRequested > contents.length) |
224 |
System.arraycopy(contents, 0, contents = new char[totalRead + 1 + amountRequested], 0, totalRead); |
225 |
contents[totalRead++] = (char) current; |
226 |
|
227 |
// read as many chars as possible |
228 |
int amountRead = reader.read(contents, totalRead, amountRequested); |
229 |
if (amountRead < 0) |
230 |
break; |
231 |
totalRead += amountRead; |
232 |
} |
233 |
|
234 |
// Do not keep first character for UTF-8 BOM encoding |
235 |
int start = 0; |
236 |
if (totalRead > 0 && UTF_8.equals(encoding)) { |
237 |
if (contents[0] == 0xFEFF) { // if BOM char then skip |
238 |
totalRead--; |
239 |
start = 1; |
260 |
} |
240 |
} |
261 |
// See PR 1FMS89U |
|
|
262 |
// Now we need to resize in case the default encoding used more than one byte for each |
263 |
// character |
264 |
if (len != length) |
265 |
System.arraycopy(contents, start, (contents = new char[len]), 0, len); |
266 |
} |
241 |
} |
267 |
|
242 |
|
|
|
243 |
// resize contents if necessary |
244 |
if (totalRead < contents.length) { |
245 |
System.arraycopy(contents, start, contents = new char[totalRead], 0, totalRead); |
246 |
} |
247 |
|
268 |
return contents; |
248 |
return contents; |
269 |
} |
249 |
} |
270 |
|
250 |
|