1 23 24 package org.enhydra.xml.io; 25 26 import java.io.FilterInputStream ; 27 import java.io.FilterReader ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.Reader ; 31 32 import org.xml.sax.InputSource ; 33 34 40 public class ClosingInputSource extends InputSource { 41 44 private class ClosingInputStream extends FilterInputStream { 45 48 public ClosingInputStream(InputStream stream) { 49 super(stream); 50 } 51 52 55 public int read() throws IOException { 56 try { 57 int bt = super.read(); 58 if (bt < 0) { 59 close(); 60 } 61 return bt; 62 } catch (IOException except) { 63 close(); 64 throw except; 65 } 66 } 67 68 71 public int read(byte[] bBuf, int off, int len) throws IOException { 72 try { 73 int readLen = super.read(bBuf, off, len); 74 if (readLen < 0) { 75 close(); 76 } 77 return readLen; 78 } catch (IOException except) { 79 close(); 80 throw except; 81 } 82 } 83 } 84 85 88 private class ClosingReader extends FilterReader { 89 92 public ClosingReader(Reader stream) { 93 super(stream); 94 } 95 96 99 public int read() throws IOException { 100 try { 101 int ch = super.read(); 102 if (ch < 0) { 103 close(); 104 } 105 return ch; 106 } catch (IOException except) { 107 close(); 108 throw except; 109 } 110 } 111 112 115 public int read(char[] cBuf, int off, int len) throws IOException { 116 try { 117 int readLen = super.read(cBuf, off, len); 118 if (readLen < 0) { 119 close(); 120 } 121 return readLen; 122 } catch (IOException except) { 123 close(); 124 throw except; 125 } 126 } 127 } 128 129 134 public void open() throws IOException { 135 if ((getByteStream() == null) && (getCharacterStream() == null)) { 136 String systemId = getSystemId(); 137 if (systemId == null) { 138 throw new IOException ("systemId is not set"); 139 } 140 setByteStream(InputSourceOps.openSystemId(systemId)); 141 } 142 } 143 144 148 public ClosingInputSource() { 149 super(); 150 } 151 152 160 public ClosingInputSource(InputSource inputSource, 161 boolean openNow) throws IOException { 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 ("InputSource does not specify an external source of input"); 173 } 174 175 if (inputSource.getPublicId() != null) { 177 super.setPublicId(inputSource.getPublicId()); 178 } 179 if (inputSource.getSystemId() != null) { 180 super.setSystemId(inputSource.getSystemId()); 181 } 182 } 183 184 190 public ClosingInputSource(String systemId) { 191 super(systemId); 192 } 193 194 203 public ClosingInputSource(String systemId, 204 boolean openNow) throws IOException { 205 super(systemId); 206 if (openNow) { 207 open(); 208 } 209 } 210 211 215 public ClosingInputSource(InputStream byteStream) { 216 super(); 217 setByteStream(byteStream); 218 } 219 220 224 public ClosingInputSource(Reader characterStream) { 225 super(); 226 setCharacterStream(characterStream); 227 } 228 229 233 public void setByteStream(InputStream byteStream) { 234 super.setByteStream(new ClosingInputStream(byteStream)); 235 } 236 237 241 public void setCharacterStream(Reader characterStream) { 242 super.setCharacterStream(new ClosingReader(characterStream)); 243 } 244 } 245 | Popular Tags |