Top Description Inners Fields Constructors Methods
org.python.util

public abstract Class ConsoleInputStream

extends FilterInputStream
Class Inheritance
Known Direct Subclasses
org.python.util.JLineConsole.Stream
Imports
java.io.EOFException, .FilterInputStream, .IOException, .InputStream, java.nio.ByteBuffer, .CharBuffer, java.nio.charset.Charset

This class is intended to replace System.in for use with console libraries that provide a line-oriented input mechanism. The console libraries provide a method to get the next line from the console as a String. Particular sub-classes should wrap this character-oriented method in a definition of getLine().

The libraries JLine and Java Readline have both been used to give Jython line-recall, editing and a line history preserved between sessions. Both deal with the console encoding internally, and interact with the user in terms of a buffer of characters. Our need in Jython is to access a byte-stream encoding the characters, with line-endings, since it is the text layer of the Python io stack, whether we are using the io module or file built-in, that should deal with encoding.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
ConsoleInputStream.EOLPolicy

Enumeration used to specify whether an end-of-line should be added or replaced at the end of each line read.

Field Summary

Modifier and TypeField and Description
private ByteBuffer
buf

Bytes decoded from the last line read.

protected static final ByteBuffer
EMPTY_BUF

Empty buffer

protected final Charset
encoding

The character encoding specified in the constructor.

protected final String
eol

The end-of-line String specified in the constructor.

protected final ConsoleInputStream.EOLPolicy
eolPolicy

The EOLPolicy specified in the constructor.

protected static final String
LINE_SEPARATOR

Platform-defined end-of-line for convenience

Inherited from java.io.FilterInputStream:
in

Constructor Summary

AccessConstructor and Description
pack-priv
ConsoleInputStream(InputStream
stream to wrap, normally System.in
in
,
Charset
to use to encode the buffered characters
encoding
,
ConsoleInputStream.EOLPolicy
choice of how to treat an end-of-line marker
eolPolicy
,
String
the end-of-line to use when eolPolicy is not LEAVE
eol
)

Create a wrapper configured with end-of-line handling that matches the specific console library being wrapped, and a character encoding matching the expectations of the client.

Method Summary

Modifier and TypeMethod and Description
public int
available()

Overrides java.io.FilterInputStream.available.

The number of bytes left unread in the current encoded line.

private void
fillBuffer()

Get a line of text from the console and re-encode it using the console encoding to bytes that will be returned from this InputStream in subsequent read operations.

protected abstract CharSequence

Returns:

Line entered by user
getLine
()

Get one line of input from the console.

public synchronized void
mark(int
the maximum limit of bytes that can be read before the mark position becomes invalid.
readlimit
)

Overrides java.io.FilterInputStream.mark.

Mark is not supported.

public boolean
markSupported()

Overrides java.io.FilterInputStream.markSupported.

Mark is not supported.

public int
read()

Overrides java.io.FilterInputStream.read.

Read the next byte of data from the buffered input line.

public int
read(byte[]
the buffer into which the data is read.
b
,
int
the start offset in array b at which the data is written.
off
,
int
the maximum number of bytes to read.
len
)

Overrides java.io.FilterInputStream.read.

Reads up to len bytes of data from this input stream into an array of bytes.

public synchronized void
reset()

Overrides java.io.FilterInputStream.reset.

Mark is not supported.

public long
skip(long
the number of bytes to be skipped.
n
)

Overrides java.io.FilterInputStream.skip.

Skip forward n bytes within the current encoded line.

Inherited from java.io.FilterInputStream:
closeread

Field Detail

bufback to summary
private ByteBuffer buf

Bytes decoded from the last line read.

EMPTY_BUFback to summary
protected static final ByteBuffer EMPTY_BUF

Empty buffer

encodingback to summary
protected final Charset encoding

The character encoding specified in the constructor.

eolback to summary
protected final String eol

The end-of-line String specified in the constructor.

eolPolicyback to summary
protected final ConsoleInputStream.EOLPolicy eolPolicy

The EOLPolicy specified in the constructor.

LINE_SEPARATORback to summary
protected static final String LINE_SEPARATOR

Platform-defined end-of-line for convenience

Constructor Detail

ConsoleInputStreamback to summary
pack-priv ConsoleInputStream(InputStream in, Charset encoding, ConsoleInputStream.EOLPolicy eolPolicy, String eol)

Create a wrapper configured with end-of-line handling that matches the specific console library being wrapped, and a character encoding matching the expectations of the client. Since this is an abstract class, this constructor will be called as the first action of the library-specific concrete class. The end-of-line policy can be chosen from LEAVE (do not modify the line), ADD (always append eol, and REPLACE (remove a trailing '\n', '\r', or '\r\n' provided by the library, then add eol).

Parameters
in:InputStream

stream to wrap, normally System.in

encoding:Charset

to use to encode the buffered characters

eolPolicy:ConsoleInputStream.EOLPolicy

choice of how to treat an end-of-line marker

eol:String

the end-of-line to use when eolPolicy is not LEAVE

Method Detail

availableback to summary
public int available() throws IOException

Overrides java.io.FilterInputStream.available.

The number of bytes left unread in the current encoded line.

Returns:int

Doc from java.io.FilterInputStream.available.

an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking.

Annotations
@Override
Exceptions
IOException:

Doc from java.io.FilterInputStream.available.

if an I/O error occurs.

fillBufferback to summary
private void fillBuffer() throws IOException, EOFException

Get a line of text from the console and re-encode it using the console encoding to bytes that will be returned from this InputStream in subsequent read operations.

getLineback to summary
protected abstract CharSequence getLine() throws IOException, EOFException

Get one line of input from the console. Override this method with the actions specific to the library in use.

Returns:CharSequence

Line entered by user

Exceptions
IOException:
in case of an error
EOFException:
if the library recognises an end-of-file condition

markback to summary
public synchronized void mark(int readlimit)

Overrides java.io.FilterInputStream.mark.

Mark is not supported.

Parameters
readlimit:int

Doc from java.io.FilterInputStream.mark.

the maximum limit of bytes that can be read before the mark position becomes invalid.

Annotations
@Override
markSupportedback to summary
public boolean markSupported()

Overrides java.io.FilterInputStream.markSupported.

Mark is not supported.

Returns:boolean

Doc from java.io.FilterInputStream.markSupported.

true if this stream type supports the mark and reset method; false otherwise.

Annotations
@Override

readback to summary
public int read() throws IOException

Overrides java.io.FilterInputStream.read.

Read the next byte of data from the buffered input line. The byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been recognised, the value -1 is returned. This method blocks until input data are available, the end of the stream is detected, or an exception is thrown. Normally, an empty line results in an encoded end-of-line being returned.

Returns:int

Doc from java.io.FilterInputStream.read.

the next byte of data, or -1 if the end of the stream is reached.

Annotations
@Override
Exceptions
IOException:

Doc from java.io.FilterInputStream.read.

if an I/O error occurs.

readback to summary
public int read(byte[] b, int off, int len) throws IOException, EOFException

Overrides java.io.FilterInputStream.read.

Reads up to len bytes of data from this input stream into an array of bytes. If len is not zero, the method blocks until some input is available; otherwise, no bytes are read and 0 is returned. This implementation calls getLine() at most once to get a line of characters from the console, and encodes them as bytes to be read back from the stream.

Parameters
b:byte[]

Doc from java.io.FilterInputStream.read.

the buffer into which the data is read.

off:int

Doc from java.io.FilterInputStream.read.

the start offset in array b at which the data is written.

len:int

Doc from java.io.FilterInputStream.read.

the maximum number of bytes to read.

Returns:int

Doc from java.io.FilterInputStream.read.

the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

Annotations
@Override
Exceptions
IOException:

Doc from java.io.FilterInputStream.read.

if an I/O error occurs.

resetback to summary
public synchronized void reset() throws IOException

Overrides java.io.FilterInputStream.reset.

Mark is not supported.

Annotations
@Override
Exceptions
IOException:

Doc from java.io.FilterInputStream.reset.

if this stream has not been marked or if the mark has been invalidated.

skipback to summary
public long skip(long n) throws IOException

Overrides java.io.FilterInputStream.skip.

Skip forward n bytes within the current encoded line. A call to skip will not result in reading a new line with getLine().

Parameters
n:long

Doc from java.io.FilterInputStream.skip.

the number of bytes to be skipped.

Returns:long

Doc from java.io.FilterInputStream.skip.

the actual number of bytes skipped.

Annotations
@Override
Exceptions
IOException:

Doc from java.io.FilterInputStream.skip.

if in.skip(n) throws an IOException.

org.python.util back to summary

public final Enum ConsoleInputStream.EOLPolicy

extends Enum<ConsoleInputStream.EOLPolicy>
Class Inheritance

Enumeration used to specify whether an end-of-line should be added or replaced at the end of each line read. LEAVE means process the line exactly as the library returns it; ADD means always add an end-of-line; and REPLACE means strip any final '\n', '\r', or '\r\n' and add an end-of-line. The end-of-line to add is specified as a String in the constructor.

Field Summary

Modifier and TypeField and Description
public static final ConsoleInputStream.EOLPolicy
public static final ConsoleInputStream.EOLPolicy
public static final ConsoleInputStream.EOLPolicy

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static ConsoleInputStream.EOLPolicy
public static ConsoleInputStream.EOLPolicy[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

ADDback to summary
public static final ConsoleInputStream.EOLPolicy ADD
LEAVEback to summary
public static final ConsoleInputStream.EOLPolicy LEAVE
REPLACEback to summary
public static final ConsoleInputStream.EOLPolicy REPLACE

Constructor Detail

EOLPolicyback to summary
private EOLPolicy()

Method Detail

valueOfback to summary
public static ConsoleInputStream.EOLPolicy valueOf(String name)
valuesback to summary
public static ConsoleInputStream.EOLPolicy[] values()