001 package org.shiftone.jrat.inject.process;
002
003
004 import org.shiftone.jrat.util.log.Logger;
005
006 import java.io.IOException;
007 import java.io.InputStream;
008
009
010 /**
011 * Class OpenInputStream wrapps/proxies to a real InputStream and prevents the
012 * caller from closing the underlying input stream. This is useful when reading
013 * chunks from a ZipInputStream passing the archive entry inputStreams to code
014 * that calls close(). This would typically close the entire ZipInputStream,
015 * which would prevent any other archive entries from being read.
016 *
017 * @author jeff@shiftone.org (Jeff Drost)
018 */
019 public class OpenInputStream extends InputStream {
020
021 private static final Logger LOG = Logger.getLogger(OpenInputStream.class);
022 private InputStream inputStream = null;
023 private boolean isOpen = true;
024
025 public OpenInputStream(InputStream inputStream) {
026 this.inputStream = inputStream;
027 }
028
029
030 public void assertOpen() throws IOException {
031
032 if (isOpen == false) {
033 throw new IOException("InputStream is closed");
034 }
035 }
036
037
038 public int available() throws IOException {
039 return inputStream.available();
040 }
041
042
043 /**
044 * Method close does not call close() on the underlying input stream. It
045 * set's a flag that is used assertions in the read methods of this class.
046 */
047 public void close() throws IOException {
048
049 assertOpen();
050
051 isOpen = false;
052
053 // DO NOT inputStream.close();
054 }
055
056
057 public synchronized void mark(int readlimit) {
058 inputStream.mark(readlimit);
059 }
060
061
062 public boolean markSupported() {
063 return inputStream.markSupported();
064 }
065
066
067 public int read() throws IOException {
068
069 assertOpen();
070
071 return inputStream.read();
072 }
073
074
075 public int read(byte b[]) throws IOException {
076
077 assertOpen();
078
079 return inputStream.read(b);
080 }
081
082
083 public int read(byte b[], int off, int len) throws IOException {
084
085 assertOpen();
086
087 return inputStream.read(b, off, len);
088 }
089
090
091 public synchronized void reset() throws IOException {
092 assertOpen();
093 inputStream.reset();
094 }
095
096
097 public long skip(long n) throws IOException {
098
099 assertOpen();
100
101 return inputStream.skip(n);
102 }
103 }