Package com.fasterxml.jackson.core.util
Class TextBuffer
java.lang.Object
com.fasterxml.jackson.core.util.TextBuffer
- Direct Known Subclasses:
ReadConstrainedTextBuffer
TextBuffer is a class similar to
StringBuffer
, with
following differences:
- TextBuffer uses segments character arrays, to avoid having to do additional array copies when array is not big enough. This means that only reallocating that is necessary is done only once: if and when caller wants to access contents in a linear array (char[], String).
- TextBuffer can also be initialized in "shared mode", in which it will just act as a wrapper to a single char array managed by another object (like parser that owns it)
- TextBuffer is not synchronized.
-
Constructor Summary
ModifierConstructorDescriptionTextBuffer
(BufferRecycler allocator) protected
TextBuffer
(BufferRecycler allocator, char[] initialSegment) -
Method Summary
Modifier and TypeMethodDescriptionprotected void
_reportBufferOverflow
(int prev, int curr) void
append
(char c) void
append
(char[] c, int start, int len) void
char[]
Deprecated.double
Deprecated.double
contentsAsDouble
(boolean useFastParser) Convenience method for converting contents of the buffer into a Double value.float
Deprecated.float
contentsAsFloat
(boolean useFastParser) Convenience method for converting contents of the buffer into a Float value.int
contentsAsInt
(boolean neg) Specialized convenience method that will decode a 32-bit int, of at most 9 digits (and possible leading minus sign).long
contentsAsLong
(boolean neg) Specialized convenience method that will decode a 64-bit int, of at most 18 digits (and possible leading minus sign).Accessor that may be used to get the contents of this buffer as a singleString
regardless of whether they were collected in a segmented fashion or not: this typically require construction of the result String.int
Accessor that will write buffered contents using givenWriter
.char[]
void
Method called to make sure that buffer is not using shared input buffer; if it is, it will copy such contents to private buffer.char[]
Method called to expand size of the current segment, to accommodate for more contiguous content.char[]
expandCurrentSegment
(int minSize) Method called to expand size of the current segment, to accommodate for more contiguous content.finishAndReturn
(int lastSegmentEnd, boolean trimTrailingSpaces) char[]
static TextBuffer
fromInitial
(char[] initialSegment) Factory method for constructing an instance with no allocator, and with initial full segment.char[]
Method for accessing the currently active (last) content segment without changing state of the bufferchar[]
int
char[]
Accessor that may be used to get the contents of this buffer as a singlechar[]
regardless of whether they were collected in a segmented fashion or not: this typically require allocation of the result buffer.int
boolean
Method that can be used to check whether textual contents can be efficiently accessed usinggetTextBuffer()
.void
Method called to indicate that the underlying buffers should now be recycled if they haven't yet been recycled.void
resetWith
(char ch) Method for clearing out possibly existing content, and replacing them with a single-character content (sosize()
would return1
)void
resetWithCopy
(char[] buf, int offset, int len) void
resetWithCopy
(String text, int start, int len) void
Method called to clear out any content text buffer may have, and initializes buffer to use non-shared data.void
resetWithShared
(char[] buf, int offset, int len) Method called to initialize the buffer with a shared copy of data; this means that buffer will just have pointers to actual data.void
resetWithString
(String value) setCurrentAndReturn
(int len) Convenience method that finishes the current active content segment (by specifying how many characters within consists of valid content) and aggregates and returns resulting contents (similar to a call tocontentsAsString()
).void
setCurrentLength
(int len) int
size()
toString()
Note: calling this method may not be as efficient as callingcontentsAsString()
, since it's not guaranteed that resulting String is cached.protected void
validateStringLength
(int length) Convenience method that can be used to verify that a String of specified length does not exceed maximum specific by this constraints object: if it does, aJsonParseException
is thrown.
-
Constructor Details
-
TextBuffer
-
TextBuffer
-
-
Method Details
-
fromInitial
Factory method for constructing an instance with no allocator, and with initial full segment.- Parameters:
initialSegment
- Initial, full segment to use for creating buffer (buffersize()
would return length ofinitialSegment
)- Returns:
- TextBuffer constructed
- Since:
- 2.10
-
releaseBuffers
public void releaseBuffers()Method called to indicate that the underlying buffers should now be recycled if they haven't yet been recycled. Although caller can still use this text buffer, it is not advisable to call this method if that is likely, since next time a buffer is needed, buffers need to reallocated.Note: since Jackson 2.11, calling this method will NOT clear already aggregated contents (that is,
_currentSegment
, to retain current token text if (but only if!) already aggregated. -
resetWithEmpty
public void resetWithEmpty()Method called to clear out any content text buffer may have, and initializes buffer to use non-shared data. -
resetWith
public void resetWith(char ch) Method for clearing out possibly existing content, and replacing them with a single-character content (sosize()
would return1
)- Parameters:
ch
- Character to set as the buffer contents- Since:
- 2.9
-
resetWithCopy
- Parameters:
buf
- Buffer that contains new contentsoffset
- Offset of the first content character inbuf
len
- Length of content inbuf
- Throws:
IOException
- if the buffer has grown too large, seeStreamReadConstraints.Builder.maxStringLength(int)
-
resetWithCopy
- Parameters:
text
- String that contains new contentsstart
- Offset of the first content character intext
len
- Length of content intext
- Throws:
IOException
- if the buffer has grown too large, seeStreamReadConstraints.Builder.maxStringLength(int)
- Since:
- 2.9
-
resetWithString
- Parameters:
value
- to replace existing buffer- Throws:
IOException
- if the value is too large, seeStreamReadConstraints.Builder.maxStringLength(int)
-
getBufferWithoutReset
public char[] getBufferWithoutReset()Method for accessing the currently active (last) content segment without changing state of the buffer- Returns:
- Currently active (last) content segment
- Since:
- 2.9
-
size
public int size()- Returns:
- Number of characters currently stored in this buffer
-
getTextOffset
public int getTextOffset() -
hasTextAsCharacters
public boolean hasTextAsCharacters()Method that can be used to check whether textual contents can be efficiently accessed usinggetTextBuffer()
.- Returns:
True
if access viagetTextBuffer()
would be efficient (that is, content already available as aggregatedchar[]
)
-
getTextBuffer
Accessor that may be used to get the contents of this buffer as a singlechar[]
regardless of whether they were collected in a segmented fashion or not: this typically require allocation of the result buffer.- Returns:
- Aggregated
char[]
that contains all buffered content - Throws:
IOException
- if the text is too large, seeStreamReadConstraints.Builder.maxStringLength(int)
-
contentsAsString
Accessor that may be used to get the contents of this buffer as a singleString
regardless of whether they were collected in a segmented fashion or not: this typically require construction of the result String.- Returns:
- Aggregated buffered contents as a
String
- Throws:
IOException
- if the contents are too large, seeStreamReadConstraints.Builder.maxStringLength(int)
-
contentsAsArray
- Returns:
- char array
- Throws:
IOException
- if the text is too large, seeStreamReadConstraints.Builder.maxStringLength(int)
-
contentsAsDouble
Convenience method for converting contents of the buffer into a Double value.- Parameters:
useFastParser
- whether to useFastDoubleParser
- Returns:
- Buffered text value parsed as a
Double
, if possible - Throws:
NumberFormatException
- if contents are not a valid Java number- Since:
- 2.14
-
contentsAsDouble
Deprecated.Convenience method for converting contents of the buffer into a Double value.- Returns:
- Buffered text value parsed as a
Double
, if possible - Throws:
NumberFormatException
- if contents are not a valid Java number
-
contentsAsFloat
Deprecated.Convenience method for converting contents of the buffer into a Float value.- Returns:
- Buffered text value parsed as a
Float
, if possible - Throws:
NumberFormatException
- if contents are not a valid Java number
-
contentsAsFloat
Convenience method for converting contents of the buffer into a Float value.- Parameters:
useFastParser
- whether to useFastDoubleParser
- Returns:
- Buffered text value parsed as a
Float
, if possible - Throws:
NumberFormatException
- if contents are not a valid Java number- Since:
- 2.14
-
contentsAsDecimal
Deprecated.Since 2.15 just access String contents if necessary, callNumberInput.parseBigDecimal(String, boolean)
(or other overloads) directly instead- Returns:
- Buffered text value parsed as a
BigDecimal
, if possible - Throws:
NumberFormatException
- if contents are not a valid Java number
-
contentsAsInt
public int contentsAsInt(boolean neg) Specialized convenience method that will decode a 32-bit int, of at most 9 digits (and possible leading minus sign).NOTE: method DOES NOT verify that the contents actually are a valid Java
int
value (either regarding format, or value range): caller MUST validate that.- Parameters:
neg
- Whether contents start with a minus sign- Returns:
- Buffered text value parsed as an
int
usingNumberInput.parseInt(String)
method (which does NOT validate input) - Since:
- 2.9
-
contentsAsLong
public long contentsAsLong(boolean neg) Specialized convenience method that will decode a 64-bit int, of at most 18 digits (and possible leading minus sign).NOTE: method DOES NOT verify that the contents actually are a valid Java
long
value (either regarding format, or value range): caller MUST validate that.- Parameters:
neg
- Whether contents start with a minus sign- Returns:
- Buffered text value parsed as an
long
usingNumberInput.parseLong(String)
method (which does NOT validate input) - Since:
- 2.9
-
contentsToWriter
Accessor that will write buffered contents using givenWriter
.- Parameters:
w
- Writer to use for writing out buffered content- Returns:
- Number of characters written (same as
size()
) - Throws:
IOException
- If write usingWriter
parameter fails- Since:
- 2.8
-
append
- Parameters:
c
- char to append- Throws:
IOException
- if the buffer has grown too large, seeStreamReadConstraints.Builder.maxStringLength(int)
-
append
- Parameters:
c
- char array to appendstart
- the start index within the array (from which we read chars to append)len
- number of chars to take from the array- Throws:
IOException
- if the buffer has grown too large, seeStreamReadConstraints.Builder.maxStringLength(int)
-
append
- Parameters:
str
- string to appendoffset
- the start index within the string (from which we read chars to append)len
- number of chars to take from the string- Throws:
IOException
- if the buffer has grown too large, seeStreamReadConstraints.Builder.maxStringLength(int)
-
getCurrentSegment
public char[] getCurrentSegment() -
emptyAndGetCurrentSegment
public char[] emptyAndGetCurrentSegment() -
getCurrentSegmentSize
public int getCurrentSegmentSize() -
setCurrentLength
public void setCurrentLength(int len) -
setCurrentAndReturn
Convenience method that finishes the current active content segment (by specifying how many characters within consists of valid content) and aggregates and returns resulting contents (similar to a call tocontentsAsString()
).- Parameters:
len
- Length of content (in characters) of the current active segment- Returns:
- String that contains all buffered content
- Throws:
IOException
- if the text is too large, seeStreamReadConstraints.Builder.maxStringLength(int)
- Since:
- 2.6
-
finishCurrentSegment
- Returns:
- char array
- Throws:
IOException
- if the text is too large, seeStreamReadConstraints.Builder.maxStringLength(int)
-
finishAndReturn
- Parameters:
lastSegmentEnd
- End offset in the currently active segment, could be 0 in the case of first character is delimiter or end-of-linetrimTrailingSpaces
- Whether trailing spaces should be trimmed or not- Returns:
- token as text
- Throws:
IOException
- If length constraints (of longest allowed Text value) are violated- Since:
- 2.15
-
expandCurrentSegment
public char[] expandCurrentSegment()Method called to expand size of the current segment, to accommodate for more contiguous content. Usually only used when parsing tokens like names if even then. Method will both expand the segment and return it- Returns:
- Expanded current segment
-
expandCurrentSegment
public char[] expandCurrentSegment(int minSize) Method called to expand size of the current segment, to accommodate for more contiguous content. Usually only used when parsing tokens like names if even then.- Parameters:
minSize
- Required minimum strength of the current segment- Returns:
- Expanded current segment
- Since:
- 2.4
-
toString
Note: calling this method may not be as efficient as callingcontentsAsString()
, since it's not guaranteed that resulting String is cached. -
_reportBufferOverflow
protected void _reportBufferOverflow(int prev, int curr) -
validateStringLength
Convenience method that can be used to verify that a String of specified length does not exceed maximum specific by this constraints object: if it does, aJsonParseException
is thrown.- Parameters:
length
- Length of string in input units- Throws:
IOException
- If length exceeds maximum- Since:
- 2.15
-
NumberInput.parseBigDecimal(String, boolean)
(or other overloads) directly instead