Package io.microsphere.io
Class StringBuilderWriter
- java.lang.Object
-
- java.io.Writer
-
- io.microsphere.io.StringBuilderWriter
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.lang.Appendable
,java.lang.AutoCloseable
public class StringBuilderWriter extends java.io.Writer
Writer
implementation that outputs to aStringBuilder
.NOTE: This implementation, as an alternative to
java.io.StringWriter
, provides- an un-synchronized (i.e. for use in a single thread) implementation for better performance.
For safe usage with multiple
Thread
s thenStringWriter
should be used. -
the write methods do not throw any
IOException
declaration:
Example Usage
StringBuilderWriter writer = new StringBuilderWriter(); writer.write("Hello, World!"); System.out.println(writer.toString()); // Output: Hello, World!
The class can also be used with a pre-defined capacity:
StringBuilderWriter writer = new StringBuilderWriter(1024); writer.write("Data");
- Author:
- Mercy
- See Also:
StringWriter
,Writer
-
-
Constructor Summary
Constructors Constructor Description StringBuilderWriter()
Constructs a newStringBuilder
instance with default capacity.StringBuilderWriter(int capacity)
Constructs a newStringBuilder
instance with the specified capacity.StringBuilderWriter(java.lang.StringBuilder builder)
Constructs a new instance with the specifiedStringBuilder
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.io.Writer
append(char value)
Appends a single character to this Writer.java.io.Writer
append(java.lang.CharSequence value)
Appends a character sequence to this Writer.java.io.Writer
append(java.lang.CharSequence value, int start, int end)
Appends a portion of a character sequence to theStringBuilder
.void
close()
Closing this writer has no effect.void
flush()
Flushing this writer has no effect.java.lang.StringBuilder
getBuilder()
Returns the underlying builder.java.lang.String
toString()
ReturnsStringBuilder.toString()
.void
write(char[] value)
Writes an array of characters to theStringBuilder
.void
write(char[] value, int offset, int length)
Writes a portion of a character array to theStringBuilder
.void
write(int c)
Writes a single character to theStringBuilder
.void
write(java.lang.String value)
Writes a String to theStringBuilder
.void
write(java.lang.String value, int off, int len)
-
-
-
Constructor Detail
-
StringBuilderWriter
public StringBuilderWriter()
Constructs a newStringBuilder
instance with default capacity.
-
StringBuilderWriter
public StringBuilderWriter(int capacity)
Constructs a newStringBuilder
instance with the specified capacity.- Parameters:
capacity
- The initial capacity of the underlyingStringBuilder
-
StringBuilderWriter
public StringBuilderWriter(java.lang.StringBuilder builder)
Constructs a new instance with the specifiedStringBuilder
.If
builder
is null a new instance with default capacity will be created.- Parameters:
builder
- The String builder. May be null.
-
-
Method Detail
-
write
public void write(int c)
Writes a single character to theStringBuilder
.- Overrides:
write
in classjava.io.Writer
- Parameters:
c
- The integer value of the character to write
-
write
public void write(char[] value)
Writes an array of characters to theStringBuilder
.- Overrides:
write
in classjava.io.Writer
- Parameters:
value
- The character array to write. May be null.
-
write
public void write(char[] value, int offset, int length)
Writes a portion of a character array to theStringBuilder
.- Specified by:
write
in classjava.io.Writer
- Parameters:
value
- The value to writeoffset
- The index of the first characterlength
- The number of characters to write
-
write
public void write(java.lang.String value)
Writes a String to theStringBuilder
.- Overrides:
write
in classjava.io.Writer
- Parameters:
value
- The value to write
-
write
public void write(java.lang.String value, int off, int len)
- Overrides:
write
in classjava.io.Writer
-
append
public java.io.Writer append(char value)
Appends a single character to this Writer.- Specified by:
append
in interfacejava.lang.Appendable
- Overrides:
append
in classjava.io.Writer
- Parameters:
value
- The character to append- Returns:
- This writer instance
-
append
public java.io.Writer append(java.lang.CharSequence value)
Appends a character sequence to this Writer.- Specified by:
append
in interfacejava.lang.Appendable
- Overrides:
append
in classjava.io.Writer
- Parameters:
value
- The character to append- Returns:
- This writer instance
-
append
public java.io.Writer append(java.lang.CharSequence value, int start, int end)
Appends a portion of a character sequence to theStringBuilder
.- Specified by:
append
in interfacejava.lang.Appendable
- Overrides:
append
in classjava.io.Writer
- Parameters:
value
- The character to appendstart
- The index of the first characterend
- The index of the last character + 1- Returns:
- This writer instance
-
close
public void close()
Closing this writer has no effect.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in classjava.io.Writer
-
flush
public void flush()
Flushing this writer has no effect.- Specified by:
flush
in interfacejava.io.Flushable
- Specified by:
flush
in classjava.io.Writer
-
getBuilder
public java.lang.StringBuilder getBuilder()
Returns the underlying builder.- Returns:
- The underlying builder
-
toString
public java.lang.String toString()
ReturnsStringBuilder.toString()
.- Overrides:
toString
in classjava.lang.Object
- Returns:
- The contents of the String builder.
-
-