Top Description Fields Constructors Methods
org.python.core.io

public abstract Class TextIOBase

extends IOBase
Class Inheritance
Known Direct Subclasses
org.python.core.io.UniversalIOWrapper, org.python.core.io.BinaryIOWrapper
Imports
java.io.InputStream, .OutputStream, java.nio.ByteBuffer, org.python.core.BufferProtocol, .Py, .PyArray, .PyBUF, .PyBuffer, .PyObject

Base class for text I/O. This class provides a character and line based interface to stream I/O.
Author
Philip Jenvey

Field Summary

Modifier and TypeField and Description
protected BufferedIOBase
bufferedIO

The underlying buffered i/o stream

protected StringBuilder
builder

Builds the final String returned from readline

public static final int
CHUNK_SIZE

The size of chunks read for readline

protected static final byte
CR_BYTE

Byte representation of the Carriage Return character

protected char[]
interimBuilder

An interim buffer for builder; readline loops move bytes into this array to avoid StringBuilder.append method calls

protected ByteBuffer
readahead

The readahead buffer.

Inherited from org.python.core.io.IOBase:
DEFAULT_BUFFER_SIZELF_BYTE

Constructor Summary

AccessConstructor and Description
public
TextIOBase(BufferedIOBase
a BufferedIOBase to wrap
bufferedIO
)

Contruct a TextIOBase wrapping the given BufferedIOBase.

Method Summary

Modifier and TypeMethod and Description
public InputStream
asInputStream()

Overrides org.python.core.io.IOBase.asInputStream.

Coerce this into an InputStream if possible, or return null.

public OutputStream
asOutputStream()

Overrides org.python.core.io.IOBase.asOutputStream.

Coerce this into an OutputStream if possible, or return null.

protected boolean

Returns:

true if the file pointer is currently at EOF
atEOF
()

Return true if the file pointer is currently at EOF.

protected void
clearReadahead()

Clear and reset the readahead buffer.

public void
close()

Overrides org.python.core.io.IOBase.close.

Flushes and closes the IO object.

public boolean
closed()

Overrides org.python.core.io.IOBase.closed.

Return whether this file has been closed.

protected String

Returns:

the result of builder.toString()
drainBuilder
()

Return the String result of the builder, and reset it/perform cleanup on it.

public RawIOBase
fileno()

Overrides org.python.core.io.IOBase.fileno.

Returns underlying file descriptor if one exists.

public void
flush()

Overrides org.python.core.io.IOBase.flush.

Flushes write buffers, if applicable.

public PyObject

Returns:

a PyObject containing all encountered Newlines, or None
getNewlines
()

Return the known Newline types, as a PyObject, encountered while reading this file.

public boolean
isatty()

Overrides org.python.core.io.IOBase.isatty.

Returns whether this is an 'interactive' stream.

protected void
packReadahead()

Restore the readahead to its original size (CHUNK_SIZE) if it was previously resized.

public String

Returns:

a String containing the bytes read
read
(int
the number of bytes to read
size
)

Read and return up to size bytes, contained in a String.

public boolean
readable()

Overrides org.python.core.io.IOBase.readable.

Return whether this file was opened for reading.

public String

Returns:

a String containing the bytes read
readall
()

Read until EOF.

protected int

Returns:

the amount of data read
readChunk
()

Read a chunk of data of size CHUNK_SIZE into the readahead buffer.

protected int

Returns:

the amount of data read
readChunk
(int
the desired size of the chunk
size
)

Read a chunk of data of the given size into the readahead buffer.

public int

Returns:

the amount of data read as an int
readinto
(PyObject
a PyObject compatible with the buffer API
buf
)

Read into the given PyObject that implements the Jython buffer API (with write access) or is a PyArray.

public String

Returns:

a String containing the bytes read
readline
(int
the number of bytes to read
size
)

Read until size, newline or EOF.

public long
seek(long
a long position value
pos
,
int
an int whence value
whence
)

Overrides org.python.core.io.IOBase.seek.

Seek to byte offset pos relative to position indicated by whence.

public long
tell()

Overrides org.python.core.io.IOBase.tell.

Return the current stream position.

public long
truncate(long
a long size to truncate to
pos
)

Overrides org.python.core.io.IOBase.truncate.

Truncate file to size in bytes.

public boolean
writable()

Overrides org.python.core.io.IOBase.writable.

Return whether this file was opened for writing.

public int

Returns:

the number of characters written as an int
write
(String
a String value
buf
)

Write the given String to the IO stream.

Inherited from org.python.core.io.IOBase:
checkClosedcheckReadablecheckWritableseekunsupported

Field Detail

bufferedIOback to summary
protected BufferedIOBase bufferedIO

The underlying buffered i/o stream

builderback to summary
protected StringBuilder builder

Builds the final String returned from readline

CHUNK_SIZEback to summary
public static final int CHUNK_SIZE

The size of chunks read for readline

CR_BYTEback to summary
protected static final byte CR_BYTE

Byte representation of the Carriage Return character

interimBuilderback to summary
protected char[] interimBuilder

An interim buffer for builder; readline loops move bytes into this array to avoid StringBuilder.append method calls

readaheadback to summary
protected ByteBuffer readahead

The readahead buffer. Though the underlying stream is sometimes buffered, readahead is specific to the TextIO layer to mostly benefit readline processing

Constructor Detail

TextIOBaseback to summary
public TextIOBase(BufferedIOBase bufferedIO)

Contruct a TextIOBase wrapping the given BufferedIOBase.

Parameters
bufferedIO:BufferedIOBase

a BufferedIOBase to wrap

Method Detail

asInputStreamback to summary
public InputStream asInputStream()

Overrides org.python.core.io.IOBase.asInputStream.

Doc from org.python.core.io.IOBase.asInputStream.

Coerce this into an InputStream if possible, or return null.

Annotations
@Override
asOutputStreamback to summary
public OutputStream asOutputStream()

Overrides org.python.core.io.IOBase.asOutputStream.

Doc from org.python.core.io.IOBase.asOutputStream.

Coerce this into an OutputStream if possible, or return null.

Annotations
@Override
atEOFback to summary
protected boolean atEOF()

Return true if the file pointer is currently at EOF. If the file pointer is not at EOF, the readahead will contain at least one byte after this method is called.

Returns:boolean

true if the file pointer is currently at EOF

clearReadaheadback to summary
protected void clearReadahead()

Clear and reset the readahead buffer.

closeback to summary
public void close()

Overrides org.python.core.io.IOBase.close.

Doc from org.python.core.io.IOBase.close.

Flushes and closes the IO object. This must be idempotent. It should also set a flag for the 'closed' property (see below) to test.

Annotations
@Override
closedback to summary
public boolean closed()

Overrides org.python.core.io.IOBase.closed.

Doc from org.python.core.io.IOBase.closed.

Return whether this file has been closed.

Returns:boolean

true if the file has been closed

Annotations
@Override

drainBuilderback to summary
protected String drainBuilder()

Return the String result of the builder, and reset it/perform cleanup on it.

Returns:String

the result of builder.toString()

filenoback to summary
public RawIOBase fileno()

Overrides org.python.core.io.IOBase.fileno.

Doc from org.python.core.io.IOBase.fileno.

Returns underlying file descriptor if one exists. Raises IOError if the IO object does not use a file descriptor.

Returns:RawIOBase

a file descriptor

Annotations
@Override

flushback to summary
public void flush()

Overrides org.python.core.io.IOBase.flush.

Doc from org.python.core.io.IOBase.flush.

Flushes write buffers, if applicable. This is a no-op for read-only and non-blocking streams.

Annotations
@Override
getNewlinesback to summary
public PyObject getNewlines()

Return the known Newline types, as a PyObject, encountered while reading this file. Returns None for all modes except universal newline mode.

Returns:PyObject

a PyObject containing all encountered Newlines, or None

isattyback to summary
public boolean isatty()

Overrides org.python.core.io.IOBase.isatty.

Doc from org.python.core.io.IOBase.isatty.

Returns whether this is an 'interactive' stream. Returns False if we don't know.

Returns:boolean

a boolean, true if an 'interactive' stream

Annotations
@Override

packReadaheadback to summary
protected void packReadahead()

Restore the readahead to its original size (CHUNK_SIZE) if it was previously resized. The readahead contents are preserved. This method assumes it contains a number of remaining elements less than or equal to CHUNK_SIZE.

readback to summary
public String read(int size)

Read and return up to size bytes, contained in a String. Returns an empty String on EOF

Parameters
size:int

the number of bytes to read

Returns:String

a String containing the bytes read

readableback to summary
public boolean readable()

Overrides org.python.core.io.IOBase.readable.

Doc from org.python.core.io.IOBase.readable.

Return whether this file was opened for reading.

Returns:boolean

true if the file was opened for reading

Annotations
@Override

readallback to summary
public String readall()

Read until EOF.

Returns:String

a String containing the bytes read

readChunkback to summary
protected int readChunk()

Read a chunk of data of size CHUNK_SIZE into the readahead buffer. Returns the amount of data read.

Returns:int

the amount of data read

readChunkback to summary
protected int readChunk(int size)

Read a chunk of data of the given size into the readahead buffer. Returns the amount of data read. Enforces a minimum size of CHUNK_SIZE.

Parameters
size:int

the desired size of the chunk

Returns:int

the amount of data read

readintoback to summary
public int readinto(PyObject buf)

Read into the given PyObject that implements the Jython buffer API (with write access) or is a PyArray.

Parameters
buf:PyObject

a PyObject compatible with the buffer API

Returns:int

the amount of data read as an int

readlineback to summary
public String readline(int size)

Read until size, newline or EOF. Returns an empty string if EOF is hit immediately.

Parameters
size:int

the number of bytes to read

Returns:String

a String containing the bytes read

seekback to summary
public long seek(long pos, int whence)

Overrides org.python.core.io.IOBase.seek.

Doc from org.python.core.io.IOBase.seek.

Seek to byte offset pos relative to position indicated by whence.

Semantics
whence Seek to pos
0 Start of stream (the default).Should be ≥0.
1 Current position + pos Either sign.
2 End of stream + pos Usually ≤0.
Returns the new absolute position.
Parameters
pos:long

a long position value

whence:int

an int whence value

Returns:long

a long position value seeked to

Annotations
@Override

tellback to summary
public long tell()

Overrides org.python.core.io.IOBase.tell.

Doc from org.python.core.io.IOBase.tell.

Return the current stream position.

Returns:long

a long position value

Annotations
@Override

truncateback to summary
public long truncate(long pos)

Overrides org.python.core.io.IOBase.truncate.

Doc from org.python.core.io.IOBase.truncate.

Truncate file to size in bytes. Returns the new size.

Parameters
pos:long

a long size to truncate to

Returns:long

a long size value the file was truncated to

Annotations
@Override

writableback to summary
public boolean writable()

Overrides org.python.core.io.IOBase.writable.

Doc from org.python.core.io.IOBase.writable.

Return whether this file was opened for writing.

Returns:boolean

true if the file was opened for writing

Annotations
@Override

writeback to summary
public int write(String buf)

Write the given String to the IO stream. Returns the number of characters written.

Parameters
buf:String

a String value

Returns:int

the number of characters written as an int