From: Xuan B. <med...@us...> - 2007-04-17 04:48:21
|
Update of /cvsroot/tm4j/tm4j/src/org/tm4j/topicmap/cmd In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv22526/src/org/tm4j/topicmap/cmd Modified Files: TopicMapList.java Log Message: Support for delayed opening and early closing of streams in response to bug https://sourceforge.net/tracker/index.php?func=detail&aid=1698489&group_id=27895&atid=391879 . Index: TopicMapList.java =================================================================== RCS file: /cvsroot/tm4j/tm4j/src/org/tm4j/topicmap/cmd/TopicMapList.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** TopicMapList.java 26 Feb 2004 21:33:44 -0000 1.7 --- TopicMapList.java 17 Apr 2007 04:48:19 -0000 1.8 *************** *** 23,26 **** --- 23,28 ---- import java.io.IOException; import java.io.InputStream; + import java.io.FilterInputStream; + import java.io.EOFException; import java.net.MalformedURLException; *************** *** 48,51 **** --- 50,56 ---- private String m_shortArg; private String m_longArg; + + /** to minimize filedescriptor usage */ + protected final static boolean useOnDemandInputStreams = true; public TopicMapList(char shortArg, String longArg) { *************** *** 104,108 **** if (sourceFile.exists()) { try { ! tmStream = new FileInputStream(sourceFile); if (tmBase == null) { --- 109,117 ---- if (sourceFile.exists()) { try { ! if (useOnDemandInputStreams) { ! tmStream = new OnDemandFileInputStream(sourceFile); ! } else { ! tmStream = new FileInputStream(sourceFile); ! } if (tmBase == null) { *************** *** 127,131 **** try { URL url = new URL(source); ! tmStream = url.openStream(); if (tmBase == null) { --- 136,144 ---- try { URL url = new URL(source); ! if (useOnDemandInputStreams) { ! tmStream = new OnDemandURLInputStream(url); ! } else { ! tmStream = url.openStream(); ! } if (tmBase == null) { *************** *** 134,138 **** } catch (MalformedURLException ex) { throw new ParseException("Could not convert '" + source + ! "' to a local file name or to a URL."); } catch (IOException ex) { throw new ParseException("Error opening URL '" + source + --- 147,151 ---- } catch (MalformedURLException ex) { throw new ParseException("Could not convert '" + source + ! "' to a local file name or to a URL.",ex); } catch (IOException ex) { throw new ParseException("Error opening URL '" + source + *************** *** 161,164 **** --- 174,293 ---- super(msg); } + + ParseException(String msg,Exception cause) { + super(msg,cause); + } + } + + public class OnDemandURLInputStream extends OnDemandInputStream { + URL url; + + public OnDemandURLInputStream(URL url) { + this.url = url; + } + + protected InputStream internalOpen() throws IOException { + return url.openStream(); + } + + } + + public class OnDemandFileInputStream extends OnDemandInputStream { + File file; + + public OnDemandFileInputStream(File file) { + this.file = file; + } + + protected InputStream internalOpen() throws IOException { + return new FileInputStream(file); + } + } + + public abstract class OnDemandInputStream extends FilterInputStream { + boolean alreadyOpened; + boolean alreadyClosed; + + public OnDemandInputStream() { + super(null); + } + + protected void ensureOpen() throws IOException { + if (!alreadyOpened&&!alreadyClosed) { + in = internalOpen(); + alreadyOpened = true; + } + } + + protected abstract InputStream internalOpen() throws IOException; + + protected void internalClose() throws IOException { + if (alreadyOpened) { + if (!alreadyClosed) { + alreadyClosed = true; + in.close(); + } + } + } + + protected void handlePossibleEOF(int readReturn) throws IOException { + if (readReturn==-1) { + handleEOF(); + } + } + + protected void handleEOF() throws IOException { + internalClose(); + } + + public int available() throws IOException { + ensureOpen(); + return super.available(); + } + + public long skip(long n) throws IOException { + ensureOpen(); + return super.skip(n); + } + + public int read() throws IOException { + ensureOpen(); + try { + int result = super.read(); + handlePossibleEOF(result); + return result; + } catch (EOFException e) { + handleEOF(); + throw e; + } + } + + public int read(byte[] b,int off,int len) throws IOException { + ensureOpen(); + try { + int result = super.read(b,off,len); + handlePossibleEOF(result); + return result; + } catch (EOFException e) { + handleEOF(); + throw e; + } + } + + public boolean markSupported() { + return false; + } + + public void mark() { + } + + public void reset() throws IOException { + throw new IOException("mark() not supported."); + } + + public void close() throws IOException { + internalClose(); + } + } } *************** *** 167,170 **** --- 296,302 ---- /* * $Log$ + * Revision 1.8 2007/04/17 04:48:19 mediumnet + * Support for delayed opening and early closing of streams in response to bug https://sourceforge.net/tracker/index.php?func=detail&aid=1698489&group_id=27895&atid=391879 . + * * Revision 1.7 2004/02/26 21:33:44 kal_ahmed * Updated license text and reformatted source with Jalopy. |