View Javadoc

1   /**********************************************************************
2    * UncloseableInputStream.java
3    * created on 02.07.2005 by netseeker
4    * $$Source$$
5    * $Date$
6    * $$Revision$$
7    *
8    * ====================================================================
9    *
10   *  Copyright 2006 netseeker aka Michael Manske
11   *
12   *  Licensed under the Apache License, Version 2.0 (the "License");
13   *  you may not use this file except in compliance with the License.
14   *  You may obtain a copy of the License at
15   *
16   *      http://www.apache.org/licenses/LICENSE-2.0
17   *
18   *  Unless required by applicable law or agreed to in writing, software
19   *  distributed under the License is distributed on an "AS IS" BASIS,
20   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   *  See the License for the specific language governing permissions and
22   *  limitations under the License.
23   * ====================================================================
24   *
25   * This file is part of the ejoe framework.
26   * For more information on the author, please see
27   * <http://www.manskes.de/>.
28   *
29   *********************************************************************/
30  
31  package de.netseeker.ejoe.io;
32  
33  import java.io.FilterInputStream;
34  import java.io.IOException;
35  import java.io.InputStream;
36  
37  import de.netseeker.ejoe.EJConstants;
38  
39  /***
40   * Simple buffered input stream wrapper which overwrites #close to prevent some and is used when input streams are
41   * handed over to (de)serialize adapters to prevent some parsers, eg. Xerces, to close our socket input streams
42   * automatically.
43   * 
44   * @author netseeker
45   * @since 0.3.5
46   */
47  public class UncloseableInputStream extends FilterInputStream
48  {
49      private boolean _customEOF;
50  
51      private boolean _isClosed;
52  
53      /***
54       * @param in
55       */
56      public UncloseableInputStream(InputStream in)
57      {
58          this( in, false );
59      }
60  
61      /***
62       * @param in
63       * @param customEOF
64       */
65      public UncloseableInputStream(InputStream in, boolean customEOF)
66      {
67          super( in );
68          _customEOF = customEOF;
69      }
70  
71      /*
72       * (non-Javadoc)
73       * 
74       * @see java.io.FilterInputStream#close()
75       */
76      public void close() throws IOException
77      {
78          _isClosed = true;
79      }
80  
81      /*
82       * (non-Javadoc)
83       * 
84       * @see java.io.FilterInputStream#available()
85       */
86      public int available() throws IOException
87      {
88          if ( !_isClosed )
89          {
90              return super.available();
91          }
92          else
93          {
94              return 0;
95          }
96      }
97  
98      /*
99       * (non-Javadoc)
100      * 
101      * @see java.io.FilterInputStream#read()
102      */
103     public int read() throws IOException
104     {
105         if ( _isClosed ) return -1;
106 
107         int ret = super.read();
108         
109         if ( ret == -1 || ( _customEOF && ret == EJConstants.EJOE_EOF ) )
110         {
111             close();
112         }
113 
114         return ret;
115     }
116 
117     /*
118      * (non-Javadoc)
119      * 
120      * @see java.io.FilterInputStream#read(byte[], int, int)
121      */
122     public int read( byte[] b, int off, int len ) throws IOException
123     {
124         if ( _isClosed ) return -1;
125 
126         int ret = super.read( b, off, len );
127 
128         if ( ret > 0 && _customEOF && (b[ret - 1] == (byte) EJConstants.EJOE_EOF) )
129         {
130             close();
131             b[ret - 1] = 0;
132 
133             if ( ret > 1 )
134                 ret -= 1;
135             else
136                 ret = -1;
137         }
138         else if ( ret == -1 )
139         {
140             close();
141         }
142 
143         return ret;
144     }
145 
146     /*
147      * (non-Javadoc)
148      * 
149      * @see java.io.FilterInputStream#read(byte[])
150      */
151     public int read( byte[] b ) throws IOException
152     {
153         return read( b, 0, b.length );
154     }
155 }