Package com.fasterxml.jackson.core.util
Class TextBuffer
- java.lang.Object
-
- com.fasterxml.jackson.core.util.TextBuffer
-
public final class TextBuffer extends java.lang.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.
-
-
Field Summary
Fields Modifier and Type Field Description private BufferRecycler
_allocator
private char[]
_currentSegment
private int
_currentSize
Number of characters in currently active (last) segmentprivate boolean
_hasSegments
Flag that indicates whether _seqments is non-emptyprivate char[]
_inputBuffer
Shared input buffer; stored here in case some input can be returned as is, without being copied to collector's own buffers.private int
_inputLen
private int
_inputStart
Character offset of first char in input buffer; -1 to indicate that input buffer currently does not contain any useful char dataprivate char[]
_resultArray
private java.lang.String
_resultString
String that will be constructed when the whole contents are needed; will be temporarily stored in case asked for again.private java.util.ArrayList<char[]>
_segments
List of segments prior to currently active segment.private int
_segmentSize
Amount of characters in segments in_segments
(package private) static int
MAX_SEGMENT_LEN
Let's limit maximum segment length to something sensible.(package private) static int
MIN_SEGMENT_LEN
Let's start with sizable but not huge buffer, will grow as necessary(package private) static char[]
NO_CHARS
-
Constructor Summary
Constructors Modifier Constructor Description TextBuffer(BufferRecycler allocator)
protected
TextBuffer(BufferRecycler allocator, char[] initialSegment)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
append(char c)
void
append(char[] c, int start, int len)
void
append(java.lang.String str, int offset, int len)
private char[]
buf(int needed)
Helper method used to find a buffer to use, ideally one recycled earlier.private char[]
carr(int len)
private void
clearSegments()
char[]
contentsAsArray()
java.math.BigDecimal
contentsAsDecimal()
Convenience method for converting contents of the buffer into aBigDecimal
.double
contentsAsDouble()
Convenience method for converting contents of the buffer into a Double 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).java.lang.String
contentsAsString()
int
contentsToWriter(java.io.Writer w)
char[]
emptyAndGetCurrentSegment()
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.private void
expand(int minNewSegmentSize)
Method called when current segment is full, to allocate new segment.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.char[]
finishCurrentSegment()
static TextBuffer
fromInitial(char[] initialSegment)
Factory method for constructing an instance with no allocator, and with initial full segment.char[]
getBufferWithoutReset()
char[]
getCurrentSegment()
int
getCurrentSegmentSize()
char[]
getTextBuffer()
Accessor that may be used to get the contents of this buffer in a singlechar
array regardless of whether they were collected in a segmented fashion or not.int
getTextOffset()
boolean
hasTextAsCharacters()
Method that can be used to check whether textual contents can be efficiently accessed usinggetTextBuffer()
.void
releaseBuffers()
Method called to indicate that the underlying buffers should now be recycled if they haven't yet been recycled.void
resetWith(char ch)
void
resetWithCopy(char[] buf, int start, int len)
void
resetWithCopy(java.lang.String text, int start, int len)
void
resetWithEmpty()
Method called to clear out any content text buffer may have, and initializes buffer to use non-shared data.void
resetWithShared(char[] buf, int start, 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(java.lang.String value)
private char[]
resultArray()
java.lang.String
setCurrentAndReturn(int len)
void
setCurrentLength(int len)
int
size()
java.lang.String
toString()
Note: calling this method may not be as efficient as callingcontentsAsString()
, since it's not guaranteed that resulting String is cached.private void
unshare(int needExtra)
Method called if/when we need to append content when we have been initialized to use shared buffer.
-
-
-
Field Detail
-
NO_CHARS
static final char[] NO_CHARS
-
MIN_SEGMENT_LEN
static final int MIN_SEGMENT_LEN
Let's start with sizable but not huge buffer, will grow as necessaryReduced from 1000 down to 500 in 2.10.
- See Also:
- Constant Field Values
-
MAX_SEGMENT_LEN
static final int MAX_SEGMENT_LEN
Let's limit maximum segment length to something sensible. For 2.10, let's limit to using 64kc chunks (128 kB) -- was 256kC/512kB up to 2.9- See Also:
- Constant Field Values
-
_allocator
private final BufferRecycler _allocator
-
_inputBuffer
private char[] _inputBuffer
Shared input buffer; stored here in case some input can be returned as is, without being copied to collector's own buffers. Note that this is read-only for this Object.
-
_inputStart
private int _inputStart
Character offset of first char in input buffer; -1 to indicate that input buffer currently does not contain any useful char data
-
_inputLen
private int _inputLen
-
_segments
private java.util.ArrayList<char[]> _segments
List of segments prior to currently active segment.
-
_hasSegments
private boolean _hasSegments
Flag that indicates whether _seqments is non-empty
-
_segmentSize
private int _segmentSize
Amount of characters in segments in_segments
-
_currentSegment
private char[] _currentSegment
-
_currentSize
private int _currentSize
Number of characters in currently active (last) segment
-
_resultString
private java.lang.String _resultString
String that will be constructed when the whole contents are needed; will be temporarily stored in case asked for again.
-
_resultArray
private char[] _resultArray
-
-
Constructor Detail
-
TextBuffer
public TextBuffer(BufferRecycler allocator)
-
TextBuffer
protected TextBuffer(BufferRecycler allocator, char[] initialSegment)
- Since:
- 2.10
-
-
Method Detail
-
fromInitial
public static TextBuffer fromInitial(char[] initialSegment)
Factory method for constructing an instance with no allocator, and with initial full segment.- 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)
- Since:
- 2.9
-
resetWithShared
public void resetWithShared(char[] buf, int start, 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).
-
resetWithCopy
public void resetWithCopy(char[] buf, int start, int len)
-
resetWithCopy
public void resetWithCopy(java.lang.String text, int start, int len)
- Since:
- 2.9
-
resetWithString
public void resetWithString(java.lang.String value)
-
getBufferWithoutReset
public char[] getBufferWithoutReset()
- Since:
- 2.9
-
buf
private char[] buf(int needed)
Helper method used to find a buffer to use, ideally one recycled earlier.
-
clearSegments
private void clearSegments()
-
size
public int size()
- Returns:
- Number of characters currently stored by this collector
-
getTextOffset
public int getTextOffset()
-
hasTextAsCharacters
public boolean hasTextAsCharacters()
Method that can be used to check whether textual contents can be efficiently accessed usinggetTextBuffer()
.
-
getTextBuffer
public char[] getTextBuffer()
Accessor that may be used to get the contents of this buffer in a singlechar
array regardless of whether they were collected in a segmented fashion or not.
-
contentsAsString
public java.lang.String contentsAsString()
-
contentsAsArray
public char[] contentsAsArray()
-
contentsAsDecimal
public java.math.BigDecimal contentsAsDecimal() throws java.lang.NumberFormatException
Convenience method for converting contents of the buffer into aBigDecimal
.- Throws:
java.lang.NumberFormatException
-
contentsAsDouble
public double contentsAsDouble() throws java.lang.NumberFormatException
Convenience method for converting contents of the buffer into a Double value.- Throws:
java.lang.NumberFormatException
-
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).- Parameters:
neg
- Whether contents start with a minus sign- 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).- Parameters:
neg
- Whether contents start with a minus sign- Since:
- 2.9
-
contentsToWriter
public int contentsToWriter(java.io.Writer w) throws java.io.IOException
- Throws:
java.io.IOException
- 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)
-
append
public void append(char[] c, int start, int len)
-
append
public void append(java.lang.String str, int offset, int len)
-
getCurrentSegment
public char[] getCurrentSegment()
-
emptyAndGetCurrentSegment
public char[] emptyAndGetCurrentSegment()
-
getCurrentSegmentSize
public int getCurrentSegmentSize()
-
setCurrentLength
public void setCurrentLength(int len)
-
setCurrentAndReturn
public java.lang.String setCurrentAndReturn(int len)
- Since:
- 2.6
-
finishCurrentSegment
public char[] finishCurrentSegment()
-
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.
-
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- Since:
- 2.4.0
-
toString
public java.lang.String toString()
Note: calling this method may not be as efficient as callingcontentsAsString()
, since it's not guaranteed that resulting String is cached.- Overrides:
toString
in classjava.lang.Object
-
unshare
private void unshare(int needExtra)
Method called if/when we need to append content when we have been initialized to use shared buffer.
-
expand
private void expand(int minNewSegmentSize)
Method called when current segment is full, to allocate new segment.
-
resultArray
private char[] resultArray()
-
carr
private char[] carr(int len)
-
-