Class Source.Buffer

java.lang.Object
java.io.Writer
io.immutables.common.Source.Buffer
All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable, CharSequence
Enclosing interface:
Source

public static final class Source.Buffer extends Writer implements CharSequence
Clean implementation of in memory CharSequence and Writer/Appendable. Auto-grows akin to ArrayList. Contains common optimization for character copying. Create shallow subsequences and otherwise minimizes copying/object creation.
  • Constructor Details

    • Buffer

      public Buffer()
    • Buffer

      public Buffer(int initialCapacity)
  • Method Details

    • 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 begin, int end)
      We do shallow copy subsequence, whereas StringBuilder does copy character content. They did it do minimize accidental memory leaks. Our classes are very specialized and are to be used carefully, so it's beneficial to share underlying char data and copy explicitly when needed: wrap(buffer.toCharArray())
      Specified by:
      subSequence in interface CharSequence
    • toString

      public String toString()
      Specified by:
      toString in interface CharSequence
      Overrides:
      toString in class Object
    • flush

      public void flush()
      Specified by:
      flush in interface Flushable
      Specified by:
      flush in class Writer
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class Writer
    • append

      public Source.Buffer append(CharSequence cs)
      Specified by:
      append in interface Appendable
      Overrides:
      append in class Writer
    • append

      public Source.Buffer append(CharSequence cs, int start, int end)
      Specified by:
      append in interface Appendable
      Overrides:
      append in class Writer
    • write

      public void write(String string)
      Overrides:
      write in class Writer
    • write

      public void write(String string, int offset, int length)
      Overrides:
      write in class Writer
    • write

      public void write(char[] buffer)
      Overrides:
      write in class Writer
    • write

      public void write(char[] buffer, int offset, int length)
      Specified by:
      write in class Writer
    • write

      public void write(int c)
      Overrides:
      write in class Writer
    • append

      public Source.Buffer append(char c)
      Specified by:
      append in interface Appendable
      Overrides:
      append in class Writer
    • getChars

      public void getChars(int srcStart, int srcEnd, char[] dest, int destStart)
      String and StringBuilder -like character copy to destination array.
    • getUnsafeArray

      public char[] getUnsafeArray()
      Provides unsafe access to the raw underlying array.
    • reset

      public void reset()
      Resets internal content size to 0 and leaving internal character array intact, effectively "forgetting" any content written so far. Could be use for repeatable reading into the same array.
    • toCharArray

      public char[] toCharArray()
      Safe copy of the internal data trimmed to the actual content length.