/** * Closes the underlying <code>Reader</code> or <code>InputStream</code> and releases any system resources associated with it. * <p> * If the stream is already closed then invoking this method has no effect. * * @throws IOException if an I/O error occurs. */ public void close() throws IOException { if (reader==null) return; // implies internallyCreatedReaderFinalizer is also null try { reader.close(); } finally { if (internallyCreatedReaderFinalizer!=null) { internallyCreatedReaderFinalizer.setReaderToNull(); // Prevent attempt to close the reader a second time during finalization internallyCreatedReaderFinalizer=null; // Allow GC to clean up the InternallyCreatedReaderFinalizer object even if references to this StreamedSource still exist. Probably a negligible optimisation. } } }
private StreamedSource(final Reader reader, final boolean isInternallyCreatedReader, final String encoding, final String encodingSpecificationInfo, final String preliminaryEncodingInfo) throws IOException { this.reader=reader; if (isInternallyCreatedReader) internallyCreatedReaderFinalizer=new InternallyCreatedReaderFinalizer(reader); streamedText=new StreamedText(reader); streamedParseText=new StreamedParseText(streamedText); source=new Source(streamedText,streamedParseText,encoding,encodingSpecificationInfo,preliminaryEncodingInfo); }