behavior - Java - Strange behaviour on PrintStream with custom OutputStream -


i trying write program redirects system.out jtextarea (it doesn't have jtextarea), when call system.out.println("test!") output text area so:

\n st! \n 

the code outputstream:

package gui;  import java.awt.*; import java.io.*; import javax.swing.text.*;  public class logoutputstream extends outputstream {     public void write(final int b) throws ioexception     {         eventqueue.invokelater(new runnable()         {             public void run()             {                 write0(b);             }         });     }      public void write(final byte[] b, final int off, final int len)     {         eventqueue.invokelater(new runnable()         {             public void run()             {                 write0(b, off, len);             }         });     }      public void write(final byte[] b)     {         eventqueue.invokelater(new runnable()         {             public void run()             {                 write0(b);             }         });     }      private void write0(int b)     {         document doc = fernflowergui.frame.textarea.getdocument();         try         {             doc.insertstring(doc.getlength(), string.valueof((char)b), null);         }         catch(badlocationexception impossible)         {          }     }      private void write0(byte[] b, int off, int len)     {         document doc = fernflowergui.frame.textarea.getdocument();         try         {             doc.insertstring(doc.getlength(), new string(b, off, len), null);         }         catch(badlocationexception impossible)         {          }     }      private void write0(byte[] b)     {         write0(b, 0, b.length);     } } 

the code creates printstream:

printstream ps = new printstream(new logoutputstream(), true); 

can please tell me on earth going on?

your code isn't thread-safe, basically.

you're accepting synchronous call accepting byte array - , you're using byte array later, , assuming still have same content. if caller write() overwrites data in byte array after method returns? time use it, won't have right data.

i extract string byte array in write call, , use string in call write0.

(i'd use writer rather outputstream - fundamentally want deal text data, not binary data.)


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -