Class StringBuilderWriter

java.lang.Object
java.io.Writer
org.apache.juneau.commons.io.StringBuilderWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable

public class StringBuilderWriter extends Writer
A 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 StringBuilder instead of StringBuffer
  • 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:

// Basic usage StringBuilderWriter writer = new StringBuilderWriter(); writer.write("Hello"); writer.write(" World"); String result = writer.toString(); // Returns "Hello World" // With initial capacity StringBuilderWriter writer2 = new StringBuilderWriter(1000); // Wrap existing StringBuilder StringBuilder sb = new StringBuilder(); StringBuilderWriter writer3 = new StringBuilderWriter(sb); writer3.write("test"); // sb now contains "test"

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:
  • Constructor Details

    • StringBuilderWriter

      Constructor.

      Creates a new StringBuilderWriter with the default initial capacity (16 characters).

      Example:

      StringBuilderWriter writer = new StringBuilderWriter(); writer.write("Hello");

    • StringBuilderWriter

      public StringBuilderWriter(int initialSize)
      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 StringBuilderWriter writer = 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: "); StringBuilderWriter writer = new StringBuilderWriter(sb); writer.write("Suffix"); String result = sb.toString(); // Returns "Prefix: Suffix"

      Parameters:
      sb - The StringBuilder to wrap. Must not be null.
  • Method Details