01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: /* $Id: SubInputStream.java 426584 2006-07-28 16:01:47Z jeremias $ */
19:
20: package org.apache.xmlgraphics.util.io;
21:
22: import java.io.FilterInputStream;
23: import java.io.IOException;
24: import java.io.InputStream;
25:
26: /**
27: * This class is a FilterInputStream descendant that reads from an underlying InputStream
28: * up to a defined number of bytes or the end of the underlying stream. Closing this InputStream
29: * will not result in the underlying InputStream to be closed, too.
30: * <p>
31: * This InputStream can be used to read chunks from a larger file of which the length is
32: * known in advance.
33: */
34: public class SubInputStream extends FilterInputStream {
35:
36: /** Indicates the number of bytes remaning to be read from the underlying InputStream. */
37: private long bytesToRead;
38:
39: /**
40: * Creates a new SubInputStream.
41: * @param in the InputStream to read from
42: * @param maxLen the maximum number of bytes to read from the underlying InputStream until
43: * the end-of-file is signalled.
44: */
45: public SubInputStream(InputStream in, long maxLen) {
46: super (in);
47: this .bytesToRead = maxLen;
48: }
49:
50: /** @see java.io.InputStream#read() */
51: public int read() throws IOException {
52: if (bytesToRead > 0) {
53: int result = super .read();
54: if (result <= 0) {
55: bytesToRead--;
56: return result;
57: } else {
58: return -1;
59: }
60: } else {
61: return -1;
62: }
63: }
64:
65: /** @see java.io.InputStream#read(byte[], int, int) */
66: public int read(byte[] b, int off, int len) throws IOException {
67: if (bytesToRead == 0) {
68: return -1;
69: }
70: int effRead = (int) Math.min(bytesToRead, len);
71: //cast to int is safe because len can never be bigger than Integer.MAX_VALUE
72:
73: int result = super .read(b, off, effRead);
74: if (result >= 0) {
75: bytesToRead -= result;
76: }
77: return result;
78: }
79:
80: /** @see java.io.InputStream#skip(long) */
81: public long skip(long n) throws IOException {
82: long effRead = Math.min(bytesToRead, n);
83: long result = super .skip(effRead);
84: bytesToRead -= result;
85: return result;
86: }
87:
88: /** @see java.io.InputStream#close() */
89: public void close() throws IOException {
90: //Don't close the underlying InputStream!!!
91: this .bytesToRead = 0;
92: }
93: }
|