Package io.immutables.common
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
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.
-
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionappend(char c) append(CharSequence cs) append(CharSequence cs, int start, int end) charcharAt(int index) voidclose()voidflush()voidgetChars(int srcStart, int srcEnd, char[] dest, int destStart) String and StringBuilder -like character copy to destination array.char[]Provides unsafe access to the raw underlying array.intlength()voidreset()Resets internal content size to 0 and leaving internal character array intact, effectively "forgetting" any content written so far.subSequence(int begin, int end) We do shallow copy subsequence, whereas StringBuilder does copy character content.char[]Safe copy of the internal data trimmed to the actual content length.toString()voidwrite(char[] buffer) voidwrite(char[] buffer, int offset, int length) voidwrite(int c) voidvoidMethods inherited from class java.io.Writer
nullWriterMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.CharSequence
chars, codePoints, isEmpty
-
Constructor Details
-
Buffer
public Buffer() -
Buffer
public Buffer(int initialCapacity)
-
-
Method Details
-
length
public int length()- Specified by:
lengthin interfaceCharSequence
-
charAt
public char charAt(int index) - Specified by:
charAtin interfaceCharSequence
-
subSequence
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:
subSequencein interfaceCharSequence
-
toString
- Specified by:
toStringin interfaceCharSequence- Overrides:
toStringin classObject
-
flush
public void flush() -
close
public void close() -
append
- Specified by:
appendin interfaceAppendable- Overrides:
appendin classWriter
-
append
- Specified by:
appendin interfaceAppendable- Overrides:
appendin classWriter
-
write
-
write
-
write
public void write(char[] buffer) -
write
public void write(char[] buffer, int offset, int length) -
write
public void write(int c) -
append
- Specified by:
appendin interfaceAppendable- Overrides:
appendin classWriter
-
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.
-