Class StringBuilderWriter
- All Implemented Interfaces:
Closeable,Flushable,Appendable,AutoCloseable
Writer implementation that writes to a StringBuilder instead of a StringBuffer.
This class is similar to StringWriter, but uses a StringBuilder instead of a
StringBuffer to avoid synchronization overhead. This makes it more efficient for
single-threaded use cases where thread-safety is not required.
Features:
- No synchronization overhead - uses
StringBuilderinstead ofStringBuffer - Efficient string building - optimized for single-threaded string construction
- Configurable initial capacity - can specify initial buffer size
- Wraps existing StringBuilder - can wrap an existing StringBuilder instance
- No-op close/flush - close and flush operations do nothing
Use Cases:
- Building strings efficiently in single-threaded contexts
- Capturing output from APIs that require a Writer
- Converting Writer-based APIs to StringBuilder output
- Performance-critical string building where synchronization is not needed
Usage:
Comparison with StringWriter:
- StringWriter: Uses
StringBuffer(thread-safe, synchronized) - StringBuilderWriter: Uses
StringBuilder(not thread-safe, faster) - StringWriter: Suitable for multi-threaded scenarios
- StringBuilderWriter: Suitable for single-threaded scenarios where performance matters
Thread Safety:
This class is not thread-safe. It uses a StringBuilder internally, which is not
synchronized. If multiple threads need to write to the same StringBuilderWriter instance,
external synchronization is required.
See Also:
-
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionappend(char c) append(CharSequence csq) append(CharSequence csq, int start, int end) voidclose()voidflush()toString()voidwrite(char[] cbuf, int start, int length) voidwrite(int c) voidvoidMethods inherited from class java.io.Writer
nullWriter, write
-
Constructor Details
-
StringBuilderWriter
public StringBuilderWriter()Constructor.Creates a new StringBuilderWriter with the default initial capacity (16 characters).
Example:
StringBuilderWriter
writer =new StringBuilderWriter();writer .write("Hello" ); -
StringBuilderWriter
Constructor.Creates a new StringBuilderWriter with the specified initial capacity. This can improve performance if you know approximately how large the resulting string will be, avoiding multiple buffer reallocations.
Example:
// Pre-allocate buffer for known size StringBuilderWriterwriter =new StringBuilderWriter(1000);writer .write("Large content..." );- Parameters:
initialSize- The initial capacity of the internal StringBuilder in characters. Must be non-negative.- Throws:
IllegalArgumentException- If initialSize is negative.
-
StringBuilderWriter
Constructor.Creates a new StringBuilderWriter that wraps an existing StringBuilder. All writes to this writer will be appended to the provided StringBuilder. This is useful when you want to write to a StringBuilder that you already have a reference to.
Example:
StringBuilder
sb =new StringBuilder("Prefix: " ); StringBuilderWriterwriter =new StringBuilderWriter(sb );writer .write("Suffix" ); Stringresult =sb .toString();// Returns "Prefix: Suffix" - Parameters:
sb- The StringBuilder to wrap. Must not benull .
-
-
Method Details
-
append
- Specified by:
appendin interfaceAppendable- Overrides:
appendin classWriter
-
append
- Specified by:
appendin interfaceAppendable- Overrides:
appendin classWriter
-
append
- Specified by:
appendin interfaceAppendable- Overrides:
appendin classWriter
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classWriter- Throws:
IOException
-
flush
-
toString
-
write
-
write
-
write
-
write
-