java.lang.Object
java.io.Writer
cl.netswitch.lib.util.UStringWriter
- All Implemented Interfaces:
Closeable
,Flushable
,Appendable
,AutoCloseable
A character stream that collects its output in an unsynchronized string
builder, which can then be used to construct a string.
Closing a UStringWriter
has no effect. The methods in this class can
be called after the stream has been closed without generating an
IOException
.
-
Field Summary
-
Constructor Summary
ConstructorDescriptionCreate a new string writer using the default initial string builder size.UStringWriter
(int initialSize) Create a new string writer using the specified initial string builder size. -
Method Summary
Modifier and TypeMethodDescriptionappend
(char ch) Appends the specified character to this writer.append
(CharSequence csq) Appends the specified character sequence to this writer.append
(CharSequence csq, int start, int end) Appends a subsequence of the specified character sequence to this writer.void
close()
Closes this writer.void
flush()
Flushes the stream.Returns the buffer holding the current contents of this writer.toString()
Returns the current contents of the buffer as a string.void
write
(char[] chars, int offset, int length) Writes a portion of an array of characters.void
write
(int ch) Writes a single character.void
Writes a string.void
Writes a portion of a string.Methods inherited from class java.io.Writer
nullWriter, write
-
Constructor Details
-
UStringWriter
public UStringWriter()Create a new string writer using the default initial string builder size. -
UStringWriter
public UStringWriter(int initialSize) Create a new string writer using the specified initial string builder size.- Parameters:
initialSize
- the number ofchar
values that will fit into the string builder before it is automatically expanded.- Throws:
IllegalArgumentException
- ifinitialSize
is negative.
-
-
Method Details
-
write
public void write(int ch) Writes a single character. -
write
public void write(char[] chars, int offset, int length) Writes a portion of an array of characters.- Specified by:
write
in classWriter
- Parameters:
chars
- an array of characters.offset
- the offset from which to start writing characters.length
- the number of characters to write.- Throws:
IndexOutOfBoundsException
- if arguments are out-of-bounds.
-
write
Writes a string. -
write
Writes a portion of a string. -
append
Appends the specified character sequence to this writer.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 oftoString
for the character sequencecsq
the entire sequence may not be appended. For instance, invoking thetoString
method of a character buffer will return a subsequence whose content depends upon the buffer's position and limit.- Specified by:
append
in interfaceAppendable
- Overrides:
append
in classWriter
- Parameters:
csq
- the character sequence to append. Ifcsq
isnull
then the four characters"null"
are appended to this writer.- Returns:
- this writer.
-
append
Appends a subsequence of the specified character sequence to this writer.An invocation of this method of the form
out.append(csq, start, end)
whencsq
is notnull
, behaves in exactly the same way as the invocation:out.write(csq.subSequence(start, end).toString())
- Specified by:
append
in interfaceAppendable
- Overrides:
append
in classWriter
- Parameters:
csq
- the character sequence from which a subsequence will be appended. Ifcsq
isnull
, then characters will be appended as ifcsq
contained the four characters"null"
.start
- the index of the first character in the subsequence.end
- the index of the character following the last character in the subsequence.- Returns:
- this writer.
- Throws:
IndexOutOfBoundsException
- ifstart
orend
are negative,start
is greater thanend
, orend
is greater thancsq.length()
.
-
append
Appends the specified character to this writer.An invocation of this method of the form
out.append(c)
behaves in exactly the same way as the invocation:out.write(c)
- Specified by:
append
in interfaceAppendable
- Overrides:
append
in classWriter
- Parameters:
ch
- the character to append.- Returns:
- this writer.
-
toString
Returns the current contents of the buffer as a string. -
getBuffer
Returns the buffer holding the current contents of this writer.- Returns:
- the buffer holding the current contents of this writer.
-
flush
public void flush()Flushes the stream. -
close
Closes this writer. Closing aUStringWriter
has no effect. The methods in this class can be called after the stream has been closed without generating anIOException
.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in classWriter
- Throws:
IOException
- if an I/O error occurs.
-