org.apache.commons.io.output

## Class XmlStreamWriter

- java.lang.Object
- - java.io.Writer
  - - org.apache.commons.io.output.XmlStreamWriter

- All Implemented Interfaces:
    Closeable, Flushable, Appendable, AutoCloseable

      public class XmlStreamWriter
      extends Writer

  Character stream that handles all the necessary work to figure out the charset encoding of the XML document written to the stream.

  To build an instance, see `XmlStreamWriter.Builder`.

  Since:
    2.0
  See Also:
    `XmlStreamReader`

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `XmlStreamWriter.Builder` Builds a new `XmlStreamWriter` instance.

  - ### Field Summary

    - ### Fields inherited from class java.io.Writer

      `lock`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `XmlStreamWriter(File file)` Deprecated. Use `builder()`, `XmlStreamWriter.Builder`, and `XmlStreamWriter.Builder.get()`
    `XmlStreamWriter(File file, String defaultEncoding)` Deprecated. Use `builder()`, `XmlStreamWriter.Builder`, and `XmlStreamWriter.Builder.get()`
    `XmlStreamWriter(OutputStream out)` Deprecated. Use `builder()`, `XmlStreamWriter.Builder`, and `XmlStreamWriter.Builder.get()`
    `XmlStreamWriter(OutputStream out, String defaultEncoding)` Deprecated. Use `builder()`, `XmlStreamWriter.Builder`, and `XmlStreamWriter.Builder.get()`

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static XmlStreamWriter.Builder` | `builder()` Constructs a new `XmlStreamWriter.Builder`.
    `void` | `close()` Closes the underlying writer.
    `void` | `flush()` Flushes the underlying writer.
    `String` | `getDefaultEncoding()` Returns the default encoding.
    `String` | `getEncoding()` Returns the detected encoding.
    `void` | `write(char[] cbuf, int off, int len)` Writes the characters to the underlying writer, detecting encoding.

    - ### Methods inherited from class java.io.Writer

      `append, append, append, write, write, write, write`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### XmlStreamWriter

          @Deprecated
          public XmlStreamWriter(File file)
                                      throws FileNotFoundException

      Deprecated. Use `builder()`, `XmlStreamWriter.Builder`, and `XmlStreamWriter.Builder.get()`
      Constructs a new XML stream writer for the specified file with a default encoding of UTF-8.
      Parameters:
        `file` - The file to write to
      Throws:
        `FileNotFoundException` - if there is an error creating or opening the file

    - #### XmlStreamWriter

          @Deprecated
          public XmlStreamWriter(File file,
                                             String defaultEncoding)
                                      throws FileNotFoundException

      Deprecated. Use `builder()`, `XmlStreamWriter.Builder`, and `XmlStreamWriter.Builder.get()`
      Constructs a new XML stream writer for the specified file with the specified default encoding.
      Parameters:
        `file` - The file to write to
        `defaultEncoding` - The default encoding if not encoding could be detected
      Throws:
        `FileNotFoundException` - if there is an error creating or opening the file

    - #### XmlStreamWriter

          @Deprecated
          public XmlStreamWriter(OutputStream out)

      Deprecated. Use `builder()`, `XmlStreamWriter.Builder`, and `XmlStreamWriter.Builder.get()`
      Constructs a new XML stream writer for the specified output stream with a default encoding of UTF-8.
      Parameters:
        `out` - The output stream

    - #### XmlStreamWriter

          @Deprecated
          public XmlStreamWriter(OutputStream out,
                                             String defaultEncoding)

      Deprecated. Use `builder()`, `XmlStreamWriter.Builder`, and `XmlStreamWriter.Builder.get()`
      Constructs a new XML stream writer for the specified output stream with the specified default encoding.
      Parameters:
        `out` - The output stream
        `defaultEncoding` - The default encoding if not encoding could be detected

  - ### Method Detail

    - #### builder

          public static XmlStreamWriter.Builder builder()

      Constructs a new `XmlStreamWriter.Builder`.
      Returns:
        a new `XmlStreamWriter.Builder`.
      Since:
        2.12.0

    - #### close

          public void close()
                     throws IOException

      Closes the underlying writer.
      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Specified by:
        `close` in class `Writer`
      Throws:
        `IOException` - if an error occurs closing the underlying writer

    - #### flush

          public void flush()
                     throws IOException

      Flushes the underlying writer.
      Specified by:
        `flush` in interface `Flushable`
      Specified by:
        `flush` in class `Writer`
      Throws:
        `IOException` - if an error occurs flushing the underlying writer

    - #### getDefaultEncoding

          public String getDefaultEncoding()

      Returns the default encoding.
      Returns:
        the default encoding

    - #### getEncoding

          public String getEncoding()

      Returns the detected encoding.
      Returns:
        the detected encoding

    - #### write

          public void write(char[] cbuf,
                            int off,
                            int len)
                     throws IOException

      Writes the characters to the underlying writer, detecting encoding.
      Specified by:
        `write` in class `Writer`
      Parameters:
        `cbuf` - the buffer to write the characters from
        `off` - The start offset
        `len` - The number of characters to write
      Throws:
        `IOException` - if an error occurs detecting the encoding

Copyright © 2002–2023 The Apache Software Foundation. All rights reserved.