Re: [tcljava-dev] Jacl and stdout channel
Brought to you by:
mdejong
From: Mo D. <su...@ba...> - 2001-12-06 17:48:14
|
On Wed, 5 Dec 2001 02:01:32 -0800 "Raffaele Sena" <ra...@ar...> wrote: > Hi, > > I downloaded the latest sources from CVS and started playing with it. > > One thing I noticed is the following: > > - if I run jacl in interactive mode, everything works fine. > I can write to standard output with "puts" and get my string printed out. > > - if I run jacl non interactive (with a script), print to standard output > don't... get printed. > The problem seems to be a missing flush() somewhere (if I end my script > with "flush" the missing messages appear), but I cannot find a good place > where to flush the stdout channel. I tried do add a "finally" clause to > the try/catch block in Shell.java, but while a simple test worked find, > a more complex test didn't. > > -- Raffaele Humm, this one seems a little tricky. It was caused by the recent Channel rewrite. The problem is that we are writing to our own buffer before sending the data to System.out and that buffer is not getting flushed when the interp exits. I would have though the Writer classes would have a finalize() method that would flush the buffer, but it seems that is not implemented. What do you think of the following patch? The patch turns on finalization at exit time and adds a finalize method to the Channel class. Index: src/jacl/tcl/lang/Channel.java =================================================================== RCS file: /cvsroot/tcljava/tcljava/src/jacl/tcl/lang/Channel.java,v retrieving revision 1.15 diff -u -r1.15 Channel.java --- src/jacl/tcl/lang/Channel.java 2001/11/27 18:05:25 1.15 +++ src/jacl/tcl/lang/Channel.java 2001/12/06 17:43:08 @@ -87,6 +87,22 @@ protected String encoding = "iso8859-1"; /** + * A finalizer is invoked before the Object is cleaned up by the + * garbage collector. Channel requires one so that the output + * buffers are properly flushed. + * + */ + + protected void finalize() + { + try { + close(); + } catch (IOException ex) { + ex.printStackTrace(System.err); + } + } + + /** * Read data from the Channel. * * @param interp is used for TclExceptions. Index: src/jacl/tcl/lang/Shell.java =================================================================== RCS file: /cvsroot/tcljava/tcljava/src/jacl/tcl/lang/Shell.java,v retrieving revision 1.10 diff -u -r1.10 Shell.java --- src/jacl/tcl/lang/Shell.java 2001/11/18 06:29:37 1.10 +++ src/jacl/tcl/lang/Shell.java 2001/12/06 17:43:10 @@ -50,6 +50,12 @@ { String fileName = null; + // Inform system that Object.finalize() methods need to be + // run before exiting. This is required so that the Interp + // can clean itself up and flush buffers before exiting. + + System.runFinalizersOnExit(true); + // Create the interpreter. This will also create the built-in // Tcl commands. Any reactions? Mo DeJong |