Package org.python.util
Class ConsoleOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FilterOutputStream
-
- org.python.util.ConsoleOutputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
public class ConsoleOutputStream extends java.io.FilterOutputStream
This class may be used to wrap and replaceSystem.out
so that the console handling library (JLine or Java Readline) can treat an incomplete line (one without a newline) as a prompt string, and so know the true position of the cursor. It achieves this by keeping a copy of bytes that pass through from from upstream to the trueSystem.out
, until either a newline arrives, or a defined capacity (typically the console width) is reached. If client code requests the partial line as a prompt, that action also empties the buffer. In that case, the client (which is the console object) is responsible for making the prompt emerge on the real console.
-
-
Constructor Summary
Constructors Constructor Description ConsoleOutputStream(java.io.OutputStream out, int promptCapacity)
Create a wrapper on anOutputStream
that holds a copy of the last incomplete line written to it (as bytes), in case it is needed as a console prompt.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
void
flush()
void
write(int b)
This write method steals a copy of each byte in a buffer while passing it on to the wrapped stream.
-
-
-
Constructor Detail
-
ConsoleOutputStream
public ConsoleOutputStream(java.io.OutputStream out, int promptCapacity)
Create a wrapper on anOutputStream
that holds a copy of the last incomplete line written to it (as bytes), in case it is needed as a console prompt.- Parameters:
out
- the stream wrapped (normallySystem.out
)promptCapacity
- maximum number of bytes to buffer
-
-
Method Detail
-
write
public void write(int b) throws java.io.IOException
This write method steals a copy of each byte in a buffer while passing it on to the wrapped stream. The buffer is reset by each newline, when it overflows, or by the client when it is taken as a console prompt ingetPrompt(Charset)
.- Overrides:
write
in classjava.io.FilterOutputStream
- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOException
- Specified by:
flush
in interfacejava.io.Flushable
- Overrides:
flush
in classjava.io.FilterOutputStream
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.FilterOutputStream
- Throws:
java.io.IOException
-
-