Package com.fasterxml.jackson.core.util
Class TextBuffer
- java.lang.Object
-
- com.fasterxml.jackson.core.util.TextBuffer
-
- Direct Known Subclasses:
ReadConstrainedTextBuffer
public class TextBuffer extends Object
TextBuffer is a class similar toStringBuffer, 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
Constructors Modifier Constructor Description TextBuffer(BufferRecycler allocator)protectedTextBuffer(BufferRecycler allocator, char[] initialSegment)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected void_reportBufferOverflow(int prev, int curr)voidappend(char c)voidappend(char[] c, int start, int len)voidappend(String str, int offset, int len)char[]contentsAsArray()BigDecimalcontentsAsDecimal()Deprecated.Since 2.15 just access String contents if necessary, callNumberInput.parseBigDecimal(String, boolean)(or other overloads) directly insteaddoublecontentsAsDouble()Deprecated.doublecontentsAsDouble(boolean useFastParser)Convenience method for converting contents of the buffer into a Double value.floatcontentsAsFloat()Deprecated.floatcontentsAsFloat(boolean useFastParser)Convenience method for converting contents of the buffer into a Float value.intcontentsAsInt(boolean neg)Specialized convenience method that will decode a 32-bit int, of at most 9 digits (and possible leading minus sign).longcontentsAsLong(boolean neg)Specialized convenience method that will decode a 64-bit int, of at most 18 digits (and possible leading minus sign).StringcontentsAsString()Accessor that may be used to get the contents of this buffer as a singleStringregardless of whether they were collected in a segmented fashion or not: this typically require construction of the result String.intcontentsToWriter(Writer w)Accessor that will write buffered contents using givenWriter.char[]emptyAndGetCurrentSegment()voidensureNotShared()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[]expandCurrentSegment()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.StringfinishAndReturn(int lastSegmentEnd, boolean trimTrailingSpaces)char[]finishCurrentSegment()static TextBufferfromInitial(char[] initialSegment)Factory method for constructing an instance with no allocator, and with initial full segment.char[]getBufferWithoutReset()Method for accessing the currently active (last) content segment without changing state of the bufferchar[]getCurrentSegment()intgetCurrentSegmentSize()char[]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.intgetTextOffset()booleanhasTextAsCharacters()Method that can be used to check whether textual contents can be efficiently accessed usinggetTextBuffer().voidreleaseBuffers()Method called to indicate that the underlying buffers should now be recycled if they haven't yet been recycled.voidresetWith(char ch)Method for clearing out possibly existing content, and replacing them with a single-character content (sosize()would return1)voidresetWithCopy(char[] buf, int offset, int len)voidresetWithCopy(String text, int start, int len)voidresetWithEmpty()Method called to clear out any content text buffer may have, and initializes buffer to use non-shared data.voidresetWithShared(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.voidresetWithString(String value)StringsetCurrentAndReturn(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()).voidsetCurrentLength(int len)intsize()StringtoString()Note: calling this method may not be as efficient as callingcontentsAsString(), since it's not guaranteed that resulting String is cached.protected voidvalidateStringLength(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, aJsonParseExceptionis thrown.
-
-
-
Constructor Detail
-
TextBuffer
public TextBuffer(BufferRecycler allocator)
-
TextBuffer
protected TextBuffer(BufferRecycler allocator, char[] initialSegment)
-
-
Method Detail
-
fromInitial
public static TextBuffer fromInitial(char[] initialSegment)
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
-
resetWithShared
public 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. It also means that if anything is to be appended to the buffer, it will first have to unshare it (make a local copy).- Parameters:
buf- Buffer that contains shared contentsoffset- Offset of the first content character inbuflen- Length of content inbuf
-
resetWithCopy
public void resetWithCopy(char[] buf, int offset, int len) throws IOException- Parameters:
buf- Buffer that contains new contentsoffset- Offset of the first content character inbuflen- Length of content inbuf- Throws:
IOException- if the buffer has grown too large, seeStreamReadConstraints.Builder.maxStringLength(int)
-
resetWithCopy
public void resetWithCopy(String text, int start, int len) throws IOException
- Parameters:
text- String that contains new contentsstart- Offset of the first content character intextlen- Length of content intext- Throws:
IOException- if the buffer has grown too large, seeStreamReadConstraints.Builder.maxStringLength(int)- Since:
- 2.9
-
resetWithString
public void resetWithString(String value) throws IOException
- 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:
Trueif access viagetTextBuffer()would be efficient (that is, content already available as aggregatedchar[])
-
getTextBuffer
public char[] getTextBuffer() throws IOExceptionAccessor 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
public String contentsAsString() throws IOException
Accessor that may be used to get the contents of this buffer as a singleStringregardless 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
public char[] contentsAsArray() throws IOException- Returns:
- char array
- Throws:
IOException- if the text is too large, seeStreamReadConstraints.Builder.maxStringLength(int)
-
contentsAsDouble
public double contentsAsDouble(boolean useFastParser) throws NumberFormatExceptionConvenience 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 public double contentsAsDouble() throws NumberFormatException
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 public float contentsAsFloat() throws NumberFormatException
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
public float contentsAsFloat(boolean useFastParser) throws NumberFormatExceptionConvenience 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 public BigDecimal contentsAsDecimal() throws NumberFormatException
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
intvalue (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
intusingNumberInput.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
longvalue (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
longusingNumberInput.parseLong(String)method (which does NOT validate input) - Since:
- 2.9
-
contentsToWriter
public int contentsToWriter(Writer w) throws IOException
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 usingWriterparameter fails- Since:
- 2.8
-
ensureNotShared
public void ensureNotShared()
Method called to make sure that buffer is not using shared input buffer; if it is, it will copy such contents to private buffer.
-
append
public void append(char c) throws IOException- Parameters:
c- char to append- Throws:
IOException- if the buffer has grown too large, seeStreamReadConstraints.Builder.maxStringLength(int)
-
append
public void append(char[] c, int start, int len) throws IOException- 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
public void append(String str, int offset, int len) throws IOException
- 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
public String setCurrentAndReturn(int len) throws IOException
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
public char[] finishCurrentSegment() throws IOException- Returns:
- char array
- Throws:
IOException- if the text is too large, seeStreamReadConstraints.Builder.maxStringLength(int)
-
finishAndReturn
public String finishAndReturn(int lastSegmentEnd, boolean trimTrailingSpaces) throws IOException
- 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- 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
public String 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
protected void validateStringLength(int length) throws IOExceptionConvenience 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, aJsonParseExceptionis thrown.- Parameters:
length- Length of string in input units- Throws:
IOException- If length exceeds maximum- Since:
- 2.15
-
-