简体   繁体   English

Java-具有自定义OutputStream的PrintStream上的异常行为

[英]Java - Strange behaviour on PrintStream with custom OutputStream

I am trying to write a program that redirects System.out to a JTextArea (it doesn't have to be a JTextArea), but when I call System.out.println("Test!") the output to the text area is like so: 我正在尝试编写一个将System.out重定向到JTextArea的程序(它不必是JTextArea),但是当我调用System.out.println(“ Test!”)时,输出到文本区域就像所以:

\n
st!
\n

The code for my OutputStream: 我的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 that creates the PrintStream: 创建PrintStream的代码:

PrintStream ps = new PrintStream(new LogOutputStream(), true);

Can anyone please tell me what on Earth is going on? 谁能告诉我地球上正在发生什么?

Your code isn't thread-safe, basically. 基本上,您的代码不是线程安全的。

You're accepting a synchronous call accepting a byte array - and then you're using that byte array later, and assuming it will still have the same content. 您正在接受一个接受一个字节数组的同步调用,然后稍后再使用该字节数组,并假设它仍具有相同的内容。 What if the caller to write() overwrites the data in the byte array immediately after the method returns? 如果在方法返回后, write()的调用者立即覆盖字节数组中的数据,该怎么办? By the time you get to use it, you won't have the right data. 当您开始使用它时,您将没有正确的数据。

I would extract the String from the byte array in your write call, and then use that String in the call to write0 . 我会在您的write调用中从字节数组中提取String ,然后在对write0的调用中使用该String

(I'd also personally use a Writer rather than an OutputStream - fundamentally you want to deal with text data, not binary data.) (我个人也使用Writer而不是OutputStream基本上,您要处理文本数据,而不是二进制数据。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM