Package org.antlr.v4.runtime
Enum Class CharStreams
- All Implemented Interfaces:
Serializable,Comparable<CharStreams>,Constable
This class represents the primary interface for creating
CharStreams
from a variety of sources as of 4.7. The motivation was to support
Unicode code points > U+FFFF. ANTLRInputStream and
ANTLRFileStream are now deprecated in favor of the streams created
by this interface.
DEPRECATED: new ANTLRFileStream("myinputfile")
NEW: CharStreams.fromFileName("myinputfile")
WARNING: If you use both the deprecated and the new streams, you will see
a nontrivial performance degradation. This speed hit is because the
Lexer's internal code goes from a monomorphic to megamorphic
dynamic dispatch to get characters from the input stream. Java's
on-the-fly compiler (JIT) is unable to perform the same optimizations
so stick with either the old or the new streams, if performance is
a primary concern. See the extreme debugging and spelunking
needed to identify this issue in our timing rig:
https://github.com/antlr/antlr4/pull/1781
The ANTLR character streams still buffer all the input when you create
the stream, as they have done for ~20 years. If you need unbuffered
access, please note that it becomes challenging to create
parse trees. The parse tree has to point to tokens which will either
point into a stale location in an unbuffered stream or you have to copy
the characters out of the buffer into the token. That defeats the purpose
of unbuffered input. Per the ANTLR book, unbuffered streams are primarily
useful for processing infinite streams *during the parse.*
The new streams also use 8-bit buffers when possible so this new
interface supports character streams that use half as much memory
as the old ANTLRFileStream, which assumed 16-bit characters.
A big shout out to Ben Hamilton (github bhamiltoncx) for his superhuman
efforts across all targets to get true Unicode 3.1 support for U+10FFFF.- Since:
- 4.7
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>> -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic CodePointBufferbufferFromChannel(ReadableByteChannel channel, Charset charset, int bufferSize, CodingErrorAction decodingErrorAction, long inputSize) static CharStreamfromChannel(ReadableByteChannel channel) Creates aCharStreamgiven an openedReadableByteChannelcontaining UTF-8 bytes.static CodePointCharStreamfromChannel(ReadableByteChannel channel, int bufferSize, CodingErrorAction decodingErrorAction, String sourceName) Creates aCharStreamgiven an openedReadableByteChannelcontaining UTF-8 bytes.static CharStreamfromChannel(ReadableByteChannel channel, Charset charset) Creates aCharStreamgiven an openedReadableByteChanneland the charset of the bytes contained in the channel.static CodePointCharStreamfromChannel(ReadableByteChannel channel, Charset charset, int bufferSize, CodingErrorAction decodingErrorAction, String sourceName, long inputSize) static CharStreamCreates aCharStreamgiven a path to a UTF-8 encoded file on disk.static CharStreamCreates aCharStreamgiven a path to a file on disk and the charset of the bytes contained in the file.static CharStreamfromFileName(String fileName) Creates aCharStreamgiven a string containing a path to a UTF-8 file on disk.static CharStreamfromFileName(String fileName, Charset charset) Creates aCharStreamgiven a string containing a path to a file on disk and the charset of the bytes contained in the file.static CodePointCharStreamfromReader(Reader r) Creates aCharStreamgiven aReader.static CodePointCharStreamfromReader(Reader r, String sourceName) Creates aCharStreamgiven aReaderand its source name.static CharStreamCreates aCharStreamgiven an openedInputStreamcontaining UTF-8 bytes.static CharStreamfromStream(InputStream is, Charset charset) Creates aCharStreamgiven an openedInputStreamand the charset of the bytes contained in the stream.static CharStreamfromStream(InputStream is, Charset charset, String sourceName, long inputSize) static CodePointCharStreamfromString(String s) Creates aCharStreamgiven aString.static CodePointCharStreamfromString(String s, String sourceName) static CharStreamsReturns the enum constant of this class with the specified name.static CharStreams[]values()Returns an array containing the constants of this enum class, in the order they are declared.
-
Field Details
-
DEFAULT_BUFFER_SIZE
private static final int DEFAULT_BUFFER_SIZE- See Also:
-
-
Constructor Details
-
CharStreams
private CharStreams()
-
-
Method Details
-
values
Returns an array containing the constants of this enum class, in the order they are declared.- Returns:
- an array containing the constants of this enum class, in the order they are declared
-
valueOf
Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)- Parameters:
name- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException- if this enum class has no constant with the specified nameNullPointerException- if the argument is null
-
fromFile
Creates aCharStreamgiven a path to a UTF-8 encoded file on disk. Reads the entire contents of the file into the result before returning.- Throws:
IOException
-
fromFile
Creates aCharStreamgiven a path to a file on disk and the charset of the bytes contained in the file. Reads the entire contents of the file into the result before returning.- Throws:
IOException
-
fromFileName
Creates aCharStreamgiven a string containing a path to a UTF-8 file on disk. Reads the entire contents of the file into the result before returning.- Throws:
IOException
-
fromFileName
Creates aCharStreamgiven a string containing a path to a file on disk and the charset of the bytes contained in the file. Reads the entire contents of the file into the result before returning.- Throws:
IOException
-
fromStream
Creates aCharStreamgiven an openedInputStreamcontaining UTF-8 bytes. Reads the entire contents of theInputStreaminto the result before returning, then closes theInputStream.- Throws:
IOException
-
fromStream
Creates aCharStreamgiven an openedInputStreamand the charset of the bytes contained in the stream. Reads the entire contents of theInputStreaminto the result before returning, then closes theInputStream.- Throws:
IOException
-
fromStream
public static CharStream fromStream(InputStream is, Charset charset, String sourceName, long inputSize) throws IOException - Throws:
IOException
-
fromChannel
Creates aCharStreamgiven an openedReadableByteChannelcontaining UTF-8 bytes. Reads the entire contents of thechannelinto the result before returning, then closes thechannel.- Throws:
IOException
-
fromChannel
public static CharStream fromChannel(ReadableByteChannel channel, Charset charset) throws IOException Creates aCharStreamgiven an openedReadableByteChanneland the charset of the bytes contained in the channel. Reads the entire contents of thechannelinto the result before returning, then closes thechannel.- Throws:
IOException
-
fromReader
Creates aCharStreamgiven aReader. Closes the reader before returning.- Throws:
IOException
-
fromReader
Creates aCharStreamgiven aReaderand its source name. Closes the reader before returning.- Throws:
IOException
-
fromString
Creates aCharStreamgiven aString. -
fromString
Creates aCharStreamgiven aStringand thesourceNamefrom which it came.UTF-16 code units are converted directly into
CodePointBuffer's compact byte / char / int storage viaCodePointBuffer.Builder.append(String). That path shares the same conversion state machine asCodePointBuffer.Builder.append(CharBuffer)(including read-onlyCharBuffer.wrap(CharSequence)views) and does not allocate a temporary mutableCharBufferor char array solely to feed the builder. -
fromChannel
public static CodePointCharStream fromChannel(ReadableByteChannel channel, int bufferSize, CodingErrorAction decodingErrorAction, String sourceName) throws IOException Creates aCharStreamgiven an openedReadableByteChannelcontaining UTF-8 bytes. Reads the entire contents of thechannelinto the result before returning, then closes thechannel.- Throws:
IOException
-
fromChannel
public static CodePointCharStream fromChannel(ReadableByteChannel channel, Charset charset, int bufferSize, CodingErrorAction decodingErrorAction, String sourceName, long inputSize) throws IOException - Throws:
IOException
-
bufferFromChannel
public static CodePointBuffer bufferFromChannel(ReadableByteChannel channel, Charset charset, int bufferSize, CodingErrorAction decodingErrorAction, long inputSize) throws IOException - Throws:
IOException
-