public class ImprovedStringWriter
extends java.io.Writer
StringWriter
,
but using a more efficient StringBuilder
instead of a StringBuffer
.
This means that this class is not thread-safe, but in most usage scenarios this is not necessary.
Constructor and Description |
---|
ImprovedStringWriter()
Constructor.
|
ImprovedStringWriter(int initialCapacity)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
java.io.Writer |
append(char c)
Appends the specified character to this writer.
|
java.io.Writer |
append(java.lang.CharSequence csq)
Appends the specified character sequence to this writer.
|
java.io.Writer |
append(java.lang.CharSequence csq,
int start,
int end)
Appends a subsequence of the specified character sequence to this writer.
|
void |
close()
Closes the stream, flushing it first.
|
void |
flush()
Flushes the stream.
|
java.lang.StringBuilder |
getBuffer()
Get the underlying buffer.
|
java.lang.String |
toString()
Returns the current content of this writer.
|
void |
write(char[] cbuf,
int off,
int len)
Writes a portion of an array of characters.
|
void |
write(int c)
Writes a single character.
|
void |
write(java.lang.String str)
Writes a string.
|
void |
write(java.lang.String str,
int off,
int len)
Writes a portion of a string.
|
public ImprovedStringWriter()
public ImprovedStringWriter(int initialCapacity)
initialCapacity
- initial buffer capacityjava.lang.IllegalArgumentException
- if initialCapacity
is negativepublic void write(int c) throws java.io.IOException
Subclasses that intend to support efficient single-character output should override this method.
write
in class java.io.Writer
c
- int specifying a character to be writtenjava.io.IOException
- If an I/O error occurspublic void write(@NotNull char[] cbuf, int off, int len) throws java.io.IOException
write
in class java.io.Writer
cbuf
- Array of charactersoff
- Offset from which to start writing characterslen
- Number of characters to writejava.io.IOException
- If an I/O error occurspublic void write(@NotNull java.lang.String str) throws java.io.IOException
write
in class java.io.Writer
str
- String to be writtenjava.io.IOException
- If an I/O error occurspublic void write(@NotNull java.lang.String str, int off, int len) throws java.io.IOException
write
in class java.io.Writer
str
- A Stringoff
- Offset from which to start writing characterslen
- Number of characters to writejava.lang.IndexOutOfBoundsException
- If off is negative, or len is negative,
or off+len is negative or greater than the length
of the given stringjava.io.IOException
- If an I/O error occurspublic java.io.Writer append(java.lang.CharSequence csq) throws java.io.IOException
An invocation of this method of the form out.append(csq) behaves in exactly the same way as the invocation
out.write(csq.toString())
Depending on the specification of toString for the character sequence csq, the entire sequence may not be appended. For instance, invoking the toString method of a character buffer will return a subsequence whose content depends upon the buffer's position and limit.
append
in interface java.lang.Appendable
append
in class java.io.Writer
csq
- The character sequence to append. If csq is
null, then the four characters "null" are
appended to this writer.java.io.IOException
- If an I/O error occurspublic java.io.Writer append(java.lang.CharSequence csq, int start, int end) throws java.io.IOException
An invocation of this method of the form out.append(csq, start, end) when csq is not null behaves in exactly the same way as the invocation
out.write(csq.subSequence(start, end).toString())
append
in interface java.lang.Appendable
append
in class java.io.Writer
csq
- The character sequence from which a subsequence will be
appended. If csq is null, then characters
will be appended as if csq contained the four
characters "null".start
- The index of the first character in the subsequenceend
- The index of the character following the last character in the
subsequencejava.lang.IndexOutOfBoundsException
- If start or end are negative, start
is greater than end, or end is greater than
csq.length()java.io.IOException
- If an I/O error occurspublic java.io.Writer append(char c) throws java.io.IOException
An invocation of this method of the form out.append(c) behaves in exactly the same way as the invocation
out.write(c)
append
in interface java.lang.Appendable
append
in class java.io.Writer
c
- The 16-bit character to appendjava.io.IOException
- If an I/O error occurspublic void flush() throws java.io.IOException
If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.
flush
in interface java.io.Flushable
flush
in class java.io.Writer
java.io.IOException
- If an I/O error occurspublic void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.Writer
java.io.IOException
- If an I/O error occurspublic java.lang.StringBuilder getBuffer()
toString()
public java.lang.String toString()
toString
in class java.lang.Object
getBuffer()