Package org.apache.groovy.json.internal

# Class CharBuf

java.lang.Object
java.io.Writer
org.apache.groovy.json.internal.CharBuf

All Implemented Interfaces:
  `Closeable`, `Flushable`, `Appendable`, `AutoCloseable`, `CharSequence`

public class CharBuf extends Writer implements CharSequence
Growable character buffer used by the JSON internals.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected char[]`
  `buffer`
  Backing storage for buffered characters.
  `protected int`
  `capacity`
  Current backing-array capacity.
  `protected int`
  `location`
  Next write position in `buffer`.

  ### Fields inherited from class java.io.Writer

  `lock`

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `CharBuf()`
  Creates a growable buffer with the default capacity.
  ``
  `CharBuf(byte[] bytes)`
  Decodes UTF-8 bytes into the backing character buffer.
  ``
  `CharBuf(char[] buffer)`
  Wraps an existing character buffer.
  `protected`
  `CharBuf(int capacity)`
  Creates a growable buffer with the supplied initial capacity.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `_len(int location)`
  Sets the current logical buffer length.
  `final CharBuf`
  `add(boolean b)`
  Appends a boolean value using cached JSON literals.
  `final CharBuf`
  `add(byte i)`
  Appends a byte value.
  `CharBuf`
  `add(byte[] chars)`
  Appends a byte array as characters.
  `CharBuf`
  `add(byte[] bytes, int start, int end)`
  Appends a byte subrange as characters.
  `final CharBuf`
  `add(char ch)`
  Appends a single character.
  `CharBuf`
  `add(char[] chars)`
  Appends a character array.
  `CharBuf`
  `add(char[] chars, int length)`
  Appends a fixed-length prefix of a character array.
  `final CharBuf`
  `add(double d)`
  Appends a double value.
  `final CharBuf`
  `add(float d)`
  Appends a float value.
  `final CharBuf`
  `add(int i)`
  Appends an int value.
  `final CharBuf`
  `add(long l)`
  Appends a long value.
  `final CharBuf`
  `add(short i)`
  Appends a short value.
  `final CharBuf`
  `add(String str)`
  Appends a string.
  `CharBuf`
  `addBigDecimal(BigDecimal key)`
  Appends a `BigDecimal` using cached character data.
  `CharBuf`
  `addBigInteger(BigInteger key)`
  Appends a `BigInteger` using cached character data.
  `final CharBuf`
  `addBoolean(boolean b)`
  Appends a boolean value.
  `final CharBuf`
  `addByte(byte i)`
  Appends a byte value as a character digit sequence.
  `final CharBuf`
  `addChar(byte i)`
  Appends a byte as a single character.
  `final CharBuf`
  `addChar(char ch)`
  Appends a character.
  `final CharBuf`
  `addChar(int i)`
  Appends an int as a single character.
  `final CharBuf`
  `addChar(short i)`
  Appends a short as a single character.
  `final CharBuf`
  `addChars(char[] chars)`
  Appends a character array using `System.arraycopy(Object, int, Object, int, int)`.
  `final CharBuf`
  `addDouble(double d)`
  Appends a double value using cached character data when available.
  `final CharBuf`
  `addDouble(Double key)`
  Appends a `Double` using cached character data.
  `final CharBuf`
  `addFloat(float d)`
  Appends a float value using cached character data when available.
  `final CharBuf`
  `addFloat(Float key)`
  Appends a `Float` using cached character data.
  `final CharBuf`
  `addInt(int i)`
  Appends an int value using cached character data when available.
  `final CharBuf`
  `addInt(Integer key)`
  Appends an `Integer` using cached character data.
  `final CharBuf`
  `addJsonEscapedString(char[] charArray)`
  Appends a quoted JSON string with escaping enabled.
  `final CharBuf`
  `addJsonEscapedString(char[] charArray, boolean disableUnicodeEscaping)`
  Appends a quoted JSON string from a character array.
  `final CharBuf`
  `addJsonEscapedString(String jsonString)`
  Appends a quoted JSON string with escaping enabled.
  `final CharBuf`
  `addJsonEscapedString(String jsonString, boolean disableUnicodeEscaping)`
  Appends a quoted JSON string.
  `final CharBuf`
  `addJsonFieldName(char[] chars)`
  Appends a quoted JSON field name followed by a colon.
  `final CharBuf`
  `addJsonFieldName(char[] chars, boolean disableUnicodeEscaping)`
  Appends a quoted JSON field name followed by a colon.
  `final CharBuf`
  `addJsonFieldName(String str)`
  Appends a quoted JSON field name followed by a colon.
  `final CharBuf`
  `addJsonFieldName(String str, boolean disableUnicodeEscaping)`
  Appends a quoted JSON field name followed by a colon.
  `CharBuf`
  `addLine(CharSequence str)`
  Appends a character sequence followed by a newline character.
  `CharBuf`
  `addLine(String str)`
  Appends a line followed by a newline character.
  `final CharBuf`
  `addLong(long l)`
  Appends a long value using cached character data when available.
  `final CharBuf`
  `addLong(Long key)`
  Appends a `Long` using cached character data.
  `final void`
  `addNull()`
  Appends the JSON literal `null`.
  `final CharBuf`
  `addQuoted(char[] chars)`
  Appends a quoted character array without escaping.
  `final CharBuf`
  `addQuoted(String str)`
  Appends a quoted string without escaping.
  `final CharBuf`
  `addShort(short i)`
  Appends a short value as a character digit sequence.
  `final CharBuf`
  `addString(String str)`
  Appends a string.
  `byte`
  `byteValue()`
  Parses the buffered characters as a `byte`.
  `char`
  `charAt(int index)`
  `void`
  `close()`
  No-op for this in-memory buffer.
  `static CharBuf`
  `create(char[] buffer)`
  Wraps the supplied character buffer.
  `static CharBuf`
  `create(int capacity)`
  Creates a growable buffer with the supplied capacity.
  `static CharBuf`
  `createExact(int capacity)`
  Creates a buffer sized exactly for the requested capacity.
  `final CharBuf`
  `decodeJsonString(char[] chars)`
  Decodes a JSON string fragment into this buffer.
  `final CharBuf`
  `decodeJsonString(char[] chars, int start, int to)`
  Decodes a JSON string subrange into this buffer.
  `double`
  `doubleValue()`
  Parses the buffered characters as a `double`.
  `float`
  `floatValue()`
  Parses the buffered characters as a `float`.
  `void`
  `flush()`
  No-op for this in-memory buffer.
  `void`
  `init()`
  Allocates the backing array from the current capacity.
  `int`
  `intValue()`
  Parses the buffered characters as an `int`.
  `int`
  `len()`
  Returns the current buffer length.
  `int`
  `length()`
  `long`
  `longValue()`
  Parses the buffered characters as a `long`.
  `char[]`
  `readForRecycle()`
  Returns the backing array and resets the write position.
  `void`
  `recycle()`
  Resets the write position without clearing the backing array.
  `void`
  `removeLastChar()`
  Removes the last character when the buffer is not empty.
  `void`
  `removeLastChar(char expect)`
  Removes the last character when it matches the expected value.
  `short`
  `shortValue()`
  Parses the buffered characters as a `short`.
  `CharSequence`
  `subSequence(int start, int end)`
  `char[]`
  `toCharArray()`
  Returns the backing character array.
  `String`
  `toDebugString()`
  Returns a debug view of the current buffer state.
  `Number`
  `toIntegerWrapper()`
  Parses the buffered characters as either an `Integer` or `Long`.
  `String`
  `toString()`
  Returns the buffered characters as a string.
  `String`
  `toStringAndRecycle()`
  Returns the buffered text and resets the write position.
  `void`
  `write(char[] cbuf, int off, int len)`

  ### Methods inherited from class java.io.Writer

  `append, append, append, nullWriter, write, write, write, write`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

  ### Methods inherited from interface java.lang.CharSequence

  `chars, codePoints, isEmpty`

- ## Field Details

  - ### capacity

    protected int capacity
    Current backing-array capacity.

  - ### location

    protected int location
    Next write position in `buffer`.

  - ### buffer

    protected char[] buffer
    Backing storage for buffered characters.

- ## Constructor Details

  - ### CharBuf

    public CharBuf(char[] buffer)
    Wraps an existing character buffer.

  - ### CharBuf

    public CharBuf(byte[] bytes)
    Decodes UTF-8 bytes into the backing character buffer.

  - ### CharBuf

    protected CharBuf(int capacity)
    Creates a growable buffer with the supplied initial capacity.

  - ### CharBuf

    protected CharBuf()
    Creates a growable buffer with the default capacity.

- ## Method Details

  - ### createExact

    public static CharBuf createExact(int capacity)
    Creates a buffer sized exactly for the requested capacity.

  - ### create

    public static CharBuf create(int capacity)
    Creates a growable buffer with the supplied capacity.

  - ### create

    public static CharBuf create(char[] buffer)
    Wraps the supplied character buffer.

  - ### write

    public void write(char[] cbuf, int off, int len)
    Specified by:
      `write` in class `Writer`

  - ### flush

    public void flush() throws IOException
    No-op for this in-memory buffer.
    Specified by:
      `flush` in interface `Flushable`
    Specified by:
      `flush` in class `Writer`
    Throws:
      `IOException`

  - ### close

    public void close() throws IOException
    No-op for this in-memory buffer.
    Specified by:
      `close` in interface `AutoCloseable`
    Specified by:
      `close` in interface `Closeable`
    Specified by:
      `close` in class `Writer`
    Throws:
      `IOException`

  - ### init

    public void init()
    Allocates the backing array from the current capacity.

  - ### add

    public final CharBuf add(String str)
    Appends a string.

  - ### addString

    public final CharBuf addString(String str)
    Appends a string.

  - ### add

    public final CharBuf add(int i)
    Appends an int value.

  - ### addInt

    public final CharBuf addInt(int i)
    Appends an int value using cached character data when available.

  - ### addInt

    public final CharBuf addInt(Integer key)
    Appends an `Integer` using cached character data.

  - ### add

    public final CharBuf add(boolean b)
    Appends a boolean value using cached JSON literals.

  - ### addBoolean

    public final CharBuf addBoolean(boolean b)
    Appends a boolean value.

  - ### add

    public final CharBuf add(byte i)
    Appends a byte value.

  - ### addByte

    public final CharBuf addByte(byte i)
    Appends a byte value as a character digit sequence.

  - ### add

    public final CharBuf add(short i)
    Appends a short value.

  - ### addShort

    public final CharBuf addShort(short i)
    Appends a short value as a character digit sequence.

  - ### add

    public final CharBuf add(long l)
    Appends a long value.

  - ### add

    public final CharBuf add(double d)
    Appends a double value.

  - ### addDouble

    public final CharBuf addDouble(double d)
    Appends a double value using cached character data when available.

  - ### addDouble

    public final CharBuf addDouble(Double key)
    Appends a `Double` using cached character data.

  - ### add

    public final CharBuf add(float d)
    Appends a float value.

  - ### addFloat

    public final CharBuf addFloat(float d)
    Appends a float value using cached character data when available.

  - ### addFloat

    public final CharBuf addFloat(Float key)
    Appends a `Float` using cached character data.

  - ### addChar

    public final CharBuf addChar(byte i)
    Appends a byte as a single character.

  - ### addChar

    public final CharBuf addChar(int i)
    Appends an int as a single character.

  - ### addChar

    public final CharBuf addChar(short i)
    Appends a short as a single character.

  - ### addChar

    public final CharBuf addChar(char ch)
    Appends a character.

  - ### addLine

    public CharBuf addLine(String str)
    Appends a line followed by a newline character.

  - ### addLine

    public CharBuf addLine(CharSequence str)
    Appends a character sequence followed by a newline character.

  - ### add

    public CharBuf add(char[] chars)
    Appends a character array.

  - ### addChars

    public final CharBuf addChars(char[] chars)
    Appends a character array using `System.arraycopy(Object, int, Object, int, int)`.

  - ### addQuoted

    public final CharBuf addQuoted(char[] chars)
    Appends a quoted character array without escaping.

  - ### addJsonEscapedString

    public final CharBuf addJsonEscapedString(String jsonString)
    Appends a quoted JSON string with escaping enabled.

  - ### addJsonEscapedString

    public final CharBuf addJsonEscapedString(String jsonString, boolean disableUnicodeEscaping)
    Appends a quoted JSON string.

  - ### addJsonEscapedString

    public final CharBuf addJsonEscapedString(char[] charArray)
    Appends a quoted JSON string with escaping enabled.

  - ### addJsonEscapedString

    public final CharBuf addJsonEscapedString(char[] charArray, boolean disableUnicodeEscaping)
    Appends a quoted JSON string from a character array.

  - ### addJsonFieldName

    public final CharBuf addJsonFieldName(String str)
    Appends a quoted JSON field name followed by a colon.

  - ### addJsonFieldName

    public final CharBuf addJsonFieldName(String str, boolean disableUnicodeEscaping)
    Appends a quoted JSON field name followed by a colon.

  - ### addJsonFieldName

    public final CharBuf addJsonFieldName(char[] chars)
    Appends a quoted JSON field name followed by a colon.

  - ### addJsonFieldName

    public final CharBuf addJsonFieldName(char[] chars, boolean disableUnicodeEscaping)
    Appends a quoted JSON field name followed by a colon.

  - ### addQuoted

    public final CharBuf addQuoted(String str)
    Appends a quoted string without escaping.

  - ### add

    public CharBuf add(char[] chars, int length)
    Appends a fixed-length prefix of a character array.

  - ### add

    public CharBuf add(byte[] chars)
    Appends a byte array as characters.

  - ### add

    public CharBuf add(byte[] bytes, int start, int end)
    Appends a byte subrange as characters.

  - ### add

    public final CharBuf add(char ch)
    Appends a single character.

  - ### length

    public int length()
    Specified by:
      `length` in interface `CharSequence`

  - ### charAt

    public char charAt(int index)
    Specified by:
      `charAt` in interface `CharSequence`

  - ### subSequence

    public CharSequence subSequence(int start, int end)
    Specified by:
      `subSequence` in interface `CharSequence`

  - ### toString

    public String toString()
    Returns the buffered characters as a string.
    Specified by:
      `toString` in interface `CharSequence`
    Overrides:
      `toString` in class `Object`

  - ### toDebugString

    public String toDebugString()
    Returns a debug view of the current buffer state.

  - ### toStringAndRecycle

    public String toStringAndRecycle()
    Returns the buffered text and resets the write position.

  - ### len

    public int len()
    Returns the current buffer length.

  - ### toCharArray

    public char[] toCharArray()
    Returns the backing character array.

  - ### _len

    public void _len(int location)
    Sets the current logical buffer length.

  - ### readForRecycle

    public char[] readForRecycle()
    Returns the backing array and resets the write position.

  - ### recycle

    public void recycle()
    Resets the write position without clearing the backing array.

  - ### doubleValue

    public double doubleValue()
    Parses the buffered characters as a `double`.

  - ### floatValue

    public float floatValue()
    Parses the buffered characters as a `float`.

  - ### intValue

    public int intValue()
    Parses the buffered characters as an `int`.

  - ### longValue

    public long longValue()
    Parses the buffered characters as a `long`.

  - ### byteValue

    public byte byteValue()
    Parses the buffered characters as a `byte`.

  - ### shortValue

    public short shortValue()
    Parses the buffered characters as a `short`.

  - ### toIntegerWrapper

    public Number toIntegerWrapper()
    Parses the buffered characters as either an `Integer` or `Long`.

  - ### addNull

    public final void addNull()
    Appends the JSON literal `null`.

  - ### removeLastChar

    public void removeLastChar()
    Removes the last character when the buffer is not empty.

  - ### removeLastChar

    public void removeLastChar(char expect)
    Removes the last character when it matches the expected value.

  - ### addBigDecimal

    public CharBuf addBigDecimal(BigDecimal key)
    Appends a `BigDecimal` using cached character data.

  - ### addBigInteger

    public CharBuf addBigInteger(BigInteger key)
    Appends a `BigInteger` using cached character data.

  - ### addLong

    public final CharBuf addLong(long l)
    Appends a long value using cached character data when available.

  - ### addLong

    public final CharBuf addLong(Long key)
    Appends a `Long` using cached character data.

  - ### decodeJsonString

    public final CharBuf decodeJsonString(char[] chars)
    Decodes a JSON string fragment into this buffer.

  - ### decodeJsonString

    public final CharBuf decodeJsonString(char[] chars, int start, int to)
    Decodes a JSON string subrange into this buffer.