001    package org.shiftone.jrat.util.io;
002    
003    
004    import org.shiftone.jrat.util.io.proxy.ProxyInputStream;
005    import org.shiftone.jrat.util.log.Logger;
006    
007    import java.io.IOException;
008    import java.io.InputStream;
009    
010    
011    /**
012     * Class OpenInputStream wrapps/proxies to a real InputStream and prevents the
013     * caller from closing the underlying input stream. This is useful when reading
014     * chunks from a ZipInputStream passing the archive entry inputStreams to code
015     * that calls close(). This would typically close the entire ZipInputStream,
016     * which would prevent any other archive entries from being read.
017     *
018     * @author jeff@shiftone.org (Jeff Drost)
019     */
020    public class OpenInputStream extends ProxyInputStream {
021    
022        private static final Logger LOG = Logger.getLogger(OpenInputStream.class);
023        private InputStream inputStream = null;
024    
025        public OpenInputStream(InputStream inputStream) {
026            this.inputStream = inputStream;
027        }
028    
029    
030        protected InputStream getTarget() throws IOException {
031    
032            assertOpen();
033    
034            return inputStream;
035        }
036    
037    
038        public void assertOpen() throws IOException {
039    
040            if (inputStream == null) {
041                throw new IOException("InputStream is closed");
042            }
043        }
044    
045    
046        /**
047         * Method close does not call close() on the underlying input stream. It
048         * set's a flag that is used assertions in the read methods of this class.
049         */
050        public void close() throws IOException {
051    
052            assertOpen();
053    
054            inputStream = null;
055    
056            // DO NOT inputStream.close();
057        }
058    }