KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > io > ClosingInputSource


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: ClosingInputSource.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.io;
25
26 import java.io.FilterInputStream JavaDoc;
27 import java.io.FilterReader JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.Reader JavaDoc;
31
32 import org.xml.sax.InputSource JavaDoc;
33
34 /**
35  * Input source that automatically close its InputStream when EOF is reached
36  * or an error occurs. This works around a flaw in the SAX API where there is
37  * not method of an entity handler that opens a stream to be informed when the
38  * stream is no longer needed.
39  */

40 public class ClosingInputSource extends InputSource JavaDoc {
41     /**
42      * InputStream that closes on EOF or read error.
43      */

44     private class ClosingInputStream extends FilterInputStream JavaDoc {
45         /**
46          * Constructor
47          */

48         public ClosingInputStream(InputStream JavaDoc stream) {
49             super(stream);
50         }
51
52         /**
53          * Read a byte from the stream.
54          */

55         public int read() throws IOException JavaDoc {
56             try {
57                 int bt = super.read();
58                 if (bt < 0) {
59                     close();
60                 }
61                 return bt;
62             } catch (IOException JavaDoc except) {
63                 close();
64                 throw except;
65             }
66         }
67
68         /**
69          * Read bytes from the stream.
70          */

71         public int read(byte[] bBuf, int off, int len) throws IOException JavaDoc {
72             try {
73                 int readLen = super.read(bBuf, off, len);
74                 if (readLen < 0) {
75                     close();
76                 }
77                 return readLen;
78             } catch (IOException JavaDoc except) {
79                 close();
80                 throw except;
81             }
82         }
83     }
84  
85     /**
86      * Reader that closes on EOF or read error.
87      */

88     private class ClosingReader extends FilterReader JavaDoc {
89         /**
90          * Constructor
91          */

92         public ClosingReader(Reader JavaDoc stream) {
93             super(stream);
94         }
95
96         /**
97          * Read a character from the stream.
98          */

99         public int read() throws IOException JavaDoc {
100             try {
101                 int ch = super.read();
102                 if (ch < 0) {
103                     close();
104                 }
105                 return ch;
106             } catch (IOException JavaDoc except) {
107                 close();
108                 throw except;
109             }
110         }
111
112         /**
113          * Read characters from the stream.
114          */

115         public int read(char[] cBuf, int off, int len) throws IOException JavaDoc {
116             try {
117                 int readLen = super.read(cBuf, off, len);
118                 if (readLen < 0) {
119                     close();
120                 }
121                 return readLen;
122             } catch (IOException JavaDoc except) {
123                 close();
124                 throw except;
125             }
126         }
127     }
128  
129     /**
130      * If the resource is specified by system id, open it now as a byte
131      * stream. If the resource is specified by byte or character stream, this
132      * does nothing.
133      */

134     public void open() throws IOException JavaDoc {
135         if ((getByteStream() == null) && (getCharacterStream() == null)) {
136             String JavaDoc systemId = getSystemId();
137             if (systemId == null) {
138                 throw new IOException JavaDoc("systemId is not set");
139             }
140             setByteStream(InputSourceOps.openSystemId(systemId));
141         }
142     }
143
144     /**
145      * Zero-argument default constructor.
146      * @see InputSource#InputSource
147      */

148     public ClosingInputSource() {
149         super();
150     }
151     
152     /**
153      * Create a new input source from another input source.
154      *
155      * @param inputSource Input source specification to clone
156      * @param openNow If true, the input source is opened immediatly
157      * if specified as a system id.
158      * @see InputSource
159      */

160     public ClosingInputSource(InputSource JavaDoc inputSource,
161                               boolean openNow) throws IOException JavaDoc {
162         if (inputSource.getByteStream() != null) {
163             super.setByteStream(new ClosingInputStream(inputSource.getByteStream()));
164         } else if (inputSource.getCharacterStream() != null) {
165             super.setCharacterStream(new ClosingReader(inputSource.getCharacterStream()));
166         } else if (inputSource.getSystemId() != null) {
167             super.setSystemId(inputSource.getSystemId());
168             if (openNow) {
169                 open();
170             }
171         } else {
172             throw new IOException JavaDoc("InputSource does not specify an external source of input");
173         }
174
175         // Fill in public and system ids and encoding
176
if (inputSource.getPublicId() != null) {
177             super.setPublicId(inputSource.getPublicId());
178         }
179         if (inputSource.getSystemId() != null) {
180             super.setSystemId(inputSource.getSystemId());
181         }
182     }
183
184     /**
185      * Create a new input source with a system identifier.
186      *
187      * @param systemId The system identifier.
188      * @see InputSource#InputSource(String)
189      */

190     public ClosingInputSource(String JavaDoc systemId) {
191         super(systemId);
192     }
193
194     /**
195      * Create a new input source with a system identifier.
196      *
197      * @param systemId The system identifier.
198      * @param openNow If true, the input source is opened immediatly
199      * if specified as a system id.
200      * @exception IOException If an error occures opening the stream.
201      * @see InputSource#InputSource(String)
202      */

203     public ClosingInputSource(String JavaDoc systemId,
204                               boolean openNow) throws IOException JavaDoc {
205         super(systemId);
206         if (openNow) {
207             open();
208         }
209     }
210
211     /**
212      * Create a new input source with a byte stream.
213      * @see InputSource#InputSource(InputStream)
214      */

215     public ClosingInputSource(InputStream JavaDoc byteStream) {
216         super();
217         setByteStream(byteStream);
218     }
219     
220     /**
221      * Create a new input source with a character stream.
222      * @see InputSource#InputSource(Reader)
223      */

224     public ClosingInputSource(Reader JavaDoc characterStream) {
225         super();
226         setCharacterStream(characterStream);
227     }
228
229     /**
230      * Set the byte stream, copying it to a buffer.
231      * @see InputSource#setByteStream
232      */

233     public void setByteStream(InputStream JavaDoc byteStream) {
234         super.setByteStream(new ClosingInputStream(byteStream));
235     }
236
237     /**
238      * Set the character stream, copying it to a buffer.
239      * @see InputSource#setCharacterStream
240      */

241     public void setCharacterStream(Reader JavaDoc characterStream) {
242         super.setCharacterStream(new ClosingReader(characterStream));
243     }
244 }
245
Popular Tags