Revision: 5292
          http://jnode.svn.sourceforge.net/jnode/?rev=5292&view=rev
Author:   crawley
Date:     2009-04-16 14:42:56 +0000 (Thu, 16 Apr 2009)

Log Message:
-----------
Further changes to protect against (in this case) an application closing a
WriterOutputStream wrapper for the shell's console out/err Writers.

Modified Paths:
--------------
    trunk/core/src/core/org/jnode/log4j/config/Log4jConfigurePlugin.java
    trunk/core/src/core/org/jnode/util/WriterOutputStream.java
    
trunk/core/src/driver/org/jnode/driver/console/textscreen/TextScreenConsole.java
    
trunk/core/src/driver/org/jnode/driver/console/textscreen/TextScreenConsolePlugin.java
    
trunk/shell/src/shell/org/jnode/shell/command/driver/console/ConsoleCommand.java
    trunk/shell/src/shell/org/jnode/shell/io/CommandInputOutput.java
    trunk/shell/src/shell/org/jnode/shell/io/CommandOutput.java
    trunk/shell/src/shell/org/jnode/shell/io/ShellConsoleReader.java
    trunk/shell/src/shell/org/jnode/shell/io/ShellConsoleWriter.java
    trunk/shell/src/shell/org/jnode/shell/isolate/IsolateCommandThreadImpl.java
    trunk/shell/src/test/org/jnode/test/shell/io/WriterOutputStreamTest.java

Modified: trunk/core/src/core/org/jnode/log4j/config/Log4jConfigurePlugin.java
===================================================================
--- trunk/core/src/core/org/jnode/log4j/config/Log4jConfigurePlugin.java        
2009-04-16 13:27:02 UTC (rev 5291)
+++ trunk/core/src/core/org/jnode/log4j/config/Log4jConfigurePlugin.java        
2009-04-16 14:42:56 UTC (rev 5292)
@@ -79,7 +79,7 @@
             final VirtualConsoleAppender debugApp =
                 new VirtualConsoleAppender(new PatternLayout(LAYOUT), console, 
false);
             debugApp.setThreshold(Level.DEBUG);
-            BootLog.setDebugOut(new PrintStream(new 
WriterOutputStream(console.getOut()), true));
+            BootLog.setDebugOut(new PrintStream(new 
WriterOutputStream(console.getOut(), false), true));
 
             TextConsole atc = new ActiveTextConsole(conMgr);
             final VirtualConsoleAppender infoApp = new VirtualConsoleAppender(

Modified: trunk/core/src/core/org/jnode/util/WriterOutputStream.java
===================================================================
--- trunk/core/src/core/org/jnode/util/WriterOutputStream.java  2009-04-16 
13:27:02 UTC (rev 5291)
+++ trunk/core/src/core/org/jnode/util/WriterOutputStream.java  2009-04-16 
14:42:56 UTC (rev 5292)
@@ -42,16 +42,31 @@
     
     private Writer writer;
     private CharsetDecoder decoder;
+    private final boolean reallyClose;
     
-    public WriterOutputStream(Writer writer) {
-        this(writer, Charset.defaultCharset().name());
+    /**
+     * Construct an OutputStream that encodes the data in the default 
character coding system.
+     * @param writer the Writer to be wrapped
+     * @param reallyClose if {...@code true}, calling {...@link #close()} will 
close
+     *    the Writer; otherwise {...@link #close()} means {...@link #flush()}.
+     */
+    public WriterOutputStream(Writer writer, boolean reallyClose) {
+        this(writer, Charset.defaultCharset().name(), reallyClose);
     }
 
-    public WriterOutputStream(Writer writer, String encoding) {
+    /**
+     * Construct an OutputStream that encodes the data in the supplied 
character coding system.
+     * @param writer the Writer to be wrapped
+     * @param encoding the name of a character coding system.
+     * @param reallyClose if {...@code true}, calling {...@link #close()} will 
close
+     *    the Writer; otherwise {...@link #close()} means {...@link #flush()}.
+     */
+    public WriterOutputStream(Writer writer, String encoding, boolean 
reallyClose) {
         this.writer = writer;
         this.decoder = Charset.forName(encoding).newDecoder();
-        bytes.clear();
-        chars.clear();
+        this.bytes.clear();
+        this.chars.clear();
+        this.reallyClose = reallyClose;
     }
 
     @Override
@@ -70,7 +85,7 @@
     
     @Override
     public void close() throws IOException {
-        flush(true);
+        flush(reallyClose);
         writer.close();
     }
 

Modified: 
trunk/core/src/driver/org/jnode/driver/console/textscreen/TextScreenConsole.java
===================================================================
--- 
trunk/core/src/driver/org/jnode/driver/console/textscreen/TextScreenConsole.java
    2009-04-16 13:27:02 UTC (rev 5291)
+++ 
trunk/core/src/driver/org/jnode/driver/console/textscreen/TextScreenConsole.java
    2009-04-16 14:42:56 UTC (rev 5292)
@@ -110,8 +110,8 @@
         this.scrHeight = screen.getHeight();
         this.out = new ConsoleWriter(this, 0x07);
         this.err = new ConsoleWriter(this, 0x04);
-        this.savedOut = new PrintStream(new WriterOutputStream(this.out), 
true);
-        this.savedErr = new PrintStream(new WriterOutputStream(this.err), 
true);
+        this.savedOut = new PrintStream(new WriterOutputStream(this.out, 
false), true);
+        this.savedErr = new PrintStream(new WriterOutputStream(this.err, 
false), true);
         this.claimSystemOutErr = false;
         this.myIsolate = VmIsolate.currentIsolate();
     }

Modified: 
trunk/core/src/driver/org/jnode/driver/console/textscreen/TextScreenConsolePlugin.java
===================================================================
--- 
trunk/core/src/driver/org/jnode/driver/console/textscreen/TextScreenConsolePlugin.java
      2009-04-16 13:27:02 UTC (rev 5291)
+++ 
trunk/core/src/driver/org/jnode/driver/console/textscreen/TextScreenConsolePlugin.java
      2009-04-16 14:42:56 UTC (rev 5292)
@@ -62,8 +62,8 @@
                 (ConsoleManager.CreateOptions.TEXT |
                     ConsoleManager.CreateOptions.SCROLLABLE));
             mgr.focus(first);
-            System.setOut(new PrintStream(new 
WriterOutputStream(first.getOut()), true));
-            System.setErr(new PrintStream(new 
WriterOutputStream(first.getErr()), true));
+            System.setOut(new PrintStream(new 
WriterOutputStream(first.getOut(), false), true));
+            System.setErr(new PrintStream(new 
WriterOutputStream(first.getErr(), false), true));
             System.out.println(VmSystem.getBootLog());
         } catch (ConsoleException ex) {
             throw new PluginException(ex);

Modified: 
trunk/shell/src/shell/org/jnode/shell/command/driver/console/ConsoleCommand.java
===================================================================
--- 
trunk/shell/src/shell/org/jnode/shell/command/driver/console/ConsoleCommand.java
    2009-04-16 13:27:02 UTC (rev 5291)
+++ 
trunk/shell/src/shell/org/jnode/shell/command/driver/console/ConsoleCommand.java
    2009-04-16 14:42:56 UTC (rev 5292)
@@ -120,8 +120,8 @@
                 final PrintWriter out = new PrintWriter(new 
OutputStreamWriter(System.out));
                 TextConsole console = createConsoleWithShell(conMgr, out);
                 System.setIn(new ReaderInputStream(console.getIn()));
-                System.setOut(new PrintStream(new 
WriterOutputStream(console.getOut()), true));
-                System.setErr(new PrintStream(new 
WriterOutputStream(console.getErr()), true));
+                System.setOut(new PrintStream(new 
WriterOutputStream(console.getOut(), false), true));
+                System.setErr(new PrintStream(new 
WriterOutputStream(console.getErr(), false), true));
             } catch (Exception ex) {
                 // FIXME
                 System.out.println("Problem creating the isolated console");

Modified: trunk/shell/src/shell/org/jnode/shell/io/CommandInputOutput.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/io/CommandInputOutput.java    
2009-04-16 13:27:02 UTC (rev 5291)
+++ trunk/shell/src/shell/org/jnode/shell/io/CommandInputOutput.java    
2009-04-16 14:42:56 UTC (rev 5292)
@@ -67,7 +67,8 @@
     
     public synchronized OutputStream getOutputStream() {
         if (outputStream == null) {
-            outputStream = new WriterOutputStream(writer, getEncoding());
+            boolean isConsole = writer instanceof ShellConsoleWriter;
+            outputStream = new WriterOutputStream(writer, getEncoding(), 
!isConsole);
         }
         return outputStream;
     }

Modified: trunk/shell/src/shell/org/jnode/shell/io/CommandOutput.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/io/CommandOutput.java 2009-04-16 
13:27:02 UTC (rev 5291)
+++ trunk/shell/src/shell/org/jnode/shell/io/CommandOutput.java 2009-04-16 
14:42:56 UTC (rev 5292)
@@ -56,7 +56,8 @@
 
     public synchronized OutputStream getOutputStream() {
         if (outputStream == null) {
-            outputStream = new WriterOutputStream(writer, getEncoding());
+            boolean isConsole = writer instanceof ShellConsoleWriter;
+            outputStream = new WriterOutputStream(writer, getEncoding(), 
!isConsole);
         }
         return outputStream;
     }

Modified: trunk/shell/src/shell/org/jnode/shell/io/ShellConsoleReader.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/io/ShellConsoleReader.java    
2009-04-16 13:27:02 UTC (rev 5291)
+++ trunk/shell/src/shell/org/jnode/shell/io/ShellConsoleReader.java    
2009-04-16 14:42:56 UTC (rev 5292)
@@ -44,6 +44,9 @@
         this.reader = reader;
     }
 
+    /**
+     * Calling close has no effect.
+     */
     @Override
     public void close() throws IOException {
         // Do nothing

Modified: trunk/shell/src/shell/org/jnode/shell/io/ShellConsoleWriter.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/io/ShellConsoleWriter.java    
2009-04-16 13:27:02 UTC (rev 5291)
+++ trunk/shell/src/shell/org/jnode/shell/io/ShellConsoleWriter.java    
2009-04-16 14:42:56 UTC (rev 5292)
@@ -41,6 +41,9 @@
         this.writer = writer;
     }
 
+    /**
+     * The close method flushes the underlying stream but does not close it.
+     */
     @Override
     public void close() throws IOException {
         flush();

Modified: 
trunk/shell/src/shell/org/jnode/shell/isolate/IsolateCommandThreadImpl.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/isolate/IsolateCommandThreadImpl.java 
2009-04-16 13:27:02 UTC (rev 5291)
+++ trunk/shell/src/shell/org/jnode/shell/isolate/IsolateCommandThreadImpl.java 
2009-04-16 14:42:56 UTC (rev 5292)
@@ -43,6 +43,7 @@
 import org.jnode.shell.ShellInvocationException;
 import org.jnode.shell.ThreadExitListener;
 import org.jnode.shell.io.CommandIO;
+import org.jnode.shell.io.ShellConsoleWriter;
 import org.jnode.util.ReaderInputStream;
 import org.jnode.util.WriterOutputStream;
 import org.jnode.vm.isolate.ObjectLinkMessage;
@@ -110,7 +111,8 @@
     private Socket createSocketForOutput(Closeable closeable) throws 
IOException {
         OutputStream out;
         if (closeable instanceof Writer) {
-            out = new WriterOutputStream((Writer) closeable);        
+            boolean isConsole = closeable instanceof ShellConsoleWriter;
+            out = new WriterOutputStream((Writer) closeable, !isConsole);      
  
         } else {
             out = (OutputStream) closeable;
         }

Modified: 
trunk/shell/src/test/org/jnode/test/shell/io/WriterOutputStreamTest.java
===================================================================
--- trunk/shell/src/test/org/jnode/test/shell/io/WriterOutputStreamTest.java    
2009-04-16 13:27:02 UTC (rev 5291)
+++ trunk/shell/src/test/org/jnode/test/shell/io/WriterOutputStreamTest.java    
2009-04-16 14:42:56 UTC (rev 5292)
@@ -32,7 +32,7 @@
     public void testEmpty() throws Exception {
         String LINE = "";
         StringWriter sw = new StringWriter();
-        WriterOutputStream wos = new WriterOutputStream(sw, "UTF-8");
+        WriterOutputStream wos = new WriterOutputStream(sw, "UTF-8", true);
         byte[] buffer = LINE.getBytes();
         wos.write(buffer);
         wos.flush();
@@ -42,7 +42,7 @@
     public void testLine() throws Exception {
         String LINE = "The quick brown fox jumped over the lazy doc";
         StringWriter sw = new StringWriter();
-        WriterOutputStream wos = new WriterOutputStream(sw, "UTF-8");
+        WriterOutputStream wos = new WriterOutputStream(sw, "UTF-8", true);
         byte[] buffer = LINE.getBytes();
         wos.write(buffer);
         wos.flush();
@@ -52,7 +52,7 @@
     public void testByteAtATime() throws Exception {
         String LINE = "The quick brown fox jumped over the lazy doc";
         StringWriter sw = new StringWriter();
-        WriterOutputStream wos = new WriterOutputStream(sw, "UTF-8");
+        WriterOutputStream wos = new WriterOutputStream(sw, "UTF-8", true);
         byte[] buffer = LINE.getBytes();
         for (byte b : buffer) {
             wos.write(b);
@@ -64,7 +64,7 @@
     public void testByteAtATimeWithFlushes() throws Exception {
         String LINE = "The quick brown fox jumped over the lazy doc";
         StringWriter sw = new StringWriter();
-        WriterOutputStream wos = new WriterOutputStream(sw, "UTF-8");
+        WriterOutputStream wos = new WriterOutputStream(sw, "UTF-8", true);
         byte[] buffer = LINE.getBytes();
         for (int i = 0; i < buffer.length; i++) {
             wos.write(buffer[i]);
@@ -81,7 +81,7 @@
         }
         byte[] buffer = new String(chars).getBytes();
         StringWriter sw = new StringWriter();
-        WriterOutputStream wos = new WriterOutputStream(sw, "UTF-8");
+        WriterOutputStream wos = new WriterOutputStream(sw, "UTF-8", true);
         wos.write(buffer);
         wos.flush();
         StringBuffer sb = sw.getBuffer();
@@ -94,7 +94,7 @@
     public void testBadUnicode() throws Exception {
         byte[] BAD = new byte[] {(byte) 0x80};
         StringWriter sw = new StringWriter();
-        WriterOutputStream wos = new WriterOutputStream(sw, "UTF-8");
+        WriterOutputStream wos = new WriterOutputStream(sw, "UTF-8", true);
         try {
             wos.write(BAD);
             wos.flush();
@@ -107,7 +107,7 @@
     public void testBadUnicode2() throws Exception {
         byte[] BAD = new byte[] {(byte) 'h', (byte) 'i', (byte) 0x80};
         StringWriter sw = new StringWriter();
-        WriterOutputStream wos = new WriterOutputStream(sw, "UTF-8");
+        WriterOutputStream wos = new WriterOutputStream(sw, "UTF-8", true);
         try {
             wos.write(BAD);
             wos.flush();
@@ -121,7 +121,7 @@
     public void testBadUnicode3() throws Exception {
         byte[] BAD = new byte[] {(byte) 'h', (byte) 'i', (byte) 0xc2, (byte) 
0x00};
         StringWriter sw = new StringWriter();
-        WriterOutputStream wos = new WriterOutputStream(sw, "UTF-8");
+        WriterOutputStream wos = new WriterOutputStream(sw, "UTF-8", true);
         try {
             wos.write(BAD);
             wos.flush();
@@ -135,7 +135,7 @@
     public void testBadUnicode4() throws Exception {
         byte[] BAD = new byte[] {(byte) 'h', (byte) 'i', (byte) 0xc2};
         StringWriter sw = new StringWriter();
-        WriterOutputStream wos = new WriterOutputStream(sw, "UTF-8");
+        WriterOutputStream wos = new WriterOutputStream(sw, "UTF-8", true);
         wos.write(BAD);
         wos.flush();
         try {


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Stay on top of everything new and different, both inside and 
around Java (TM) technology - register by April 22, and save
$200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
300 plus technical and hands-on sessions. Register today. 
Use priority code J9JMT32. http://p.sf.net/sfu/p
_______________________________________________
Jnode-svn-commits mailing list
Jnode-svn-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jnode-svn-commits

Reply via email to