Home | History | Annotate | Download | only in mime4j
      1 /****************************************************************
      2  * Licensed to the Apache Software Foundation (ASF) under one   *
      3  * or more contributor license agreements.  See the NOTICE file *
      4  * distributed with this work for additional information        *
      5  * regarding copyright ownership.  The ASF licenses this file   *
      6  * to you under the Apache License, Version 2.0 (the            *
      7  * "License"); you may not use this file except in compliance   *
      8  * with the License.  You may obtain a copy of the License at   *
      9  *                                                              *
     10  *   http://www.apache.org/licenses/LICENSE-2.0                 *
     11  *                                                              *
     12  * Unless required by applicable law or agreed to in writing,   *
     13  * software distributed under the License is distributed on an  *
     14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
     15  * KIND, either express or implied.  See the License for the    *
     16  * specific language governing permissions and limitations      *
     17  * under the License.                                           *
     18  ****************************************************************/
     19 
     20 package org.apache.james.mime4j;
     21 
     22 import java.io.InputStream;
     23 import java.io.IOException;
     24 
     25 /**
     26  * InputStream that shields its underlying input stream from
     27  * being closed.
     28  *
     29  *
     30  * @version $Id: CloseShieldInputStream.java,v 1.2 2004/10/02 12:41:10 ntherning Exp $
     31  */
     32 public class CloseShieldInputStream extends InputStream {
     33 
     34     /**
     35      * Underlying InputStream
     36      */
     37     private InputStream is;
     38 
     39     public CloseShieldInputStream(InputStream is) {
     40         this.is = is;
     41     }
     42 
     43     public InputStream getUnderlyingStream() {
     44         return is;
     45     }
     46 
     47     /**
     48      * @see java.io.InputStream#read()
     49      */
     50     public int read() throws IOException {
     51         checkIfClosed();
     52         return is.read();
     53     }
     54 
     55     /**
     56      * @see java.io.InputStream#available()
     57      */
     58     public int available() throws IOException {
     59         checkIfClosed();
     60         return is.available();
     61     }
     62 
     63 
     64     /**
     65      * Set the underlying InputStream to null
     66      */
     67     public void close() throws IOException {
     68         is = null;
     69     }
     70 
     71     /**
     72      * @see java.io.FilterInputStream#reset()
     73      */
     74     public synchronized void reset() throws IOException {
     75         checkIfClosed();
     76         is.reset();
     77     }
     78 
     79     /**
     80      * @see java.io.FilterInputStream#markSupported()
     81      */
     82     public boolean markSupported() {
     83         if (is == null)
     84             return false;
     85         return is.markSupported();
     86     }
     87 
     88     /**
     89      * @see java.io.FilterInputStream#mark(int)
     90      */
     91     public synchronized void mark(int readlimit) {
     92         if (is != null)
     93             is.mark(readlimit);
     94     }
     95 
     96     /**
     97      * @see java.io.FilterInputStream#skip(long)
     98      */
     99     public long skip(long n) throws IOException {
    100         checkIfClosed();
    101         return is.skip(n);
    102     }
    103 
    104     /**
    105      * @see java.io.FilterInputStream#read(byte[])
    106      */
    107     public int read(byte b[]) throws IOException {
    108         checkIfClosed();
    109         return is.read(b);
    110     }
    111 
    112     /**
    113      * @see java.io.FilterInputStream#read(byte[], int, int)
    114      */
    115     public int read(byte b[], int off, int len) throws IOException {
    116         checkIfClosed();
    117         return is.read(b, off, len);
    118     }
    119 
    120     /**
    121      * Check if the underlying InputStream is null. If so throw an Exception
    122      *
    123      * @throws IOException if the underlying InputStream is null
    124      */
    125     private void checkIfClosed() throws IOException {
    126         if (is == null)
    127             throw new IOException("Stream is closed");
    128     }
    129 }
    130