XmlStreamWriter (Commons IO 2.5)
Date: 2016-12-01

org.apache.commons.io.output

## Class XmlStreamWriter

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

- All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.Appendable, java.lang.AutoCloseable

      public class XmlStreamWriter
      extends java.io.Writer

  Character stream that handles all the necessary Voodo to figure out the charset encoding of the XML document written to the stream.
  Since:
    2.0
  Version:
    $Id: XmlStreamWriter.java 1415850 2012-11-30 20:51:39Z ggregory $
  See Also:
    `XmlStreamReader`

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `private static int` | `BUFFER_SIZE`
    `private java.lang.String` | `defaultEncoding`
    `private java.lang.String` | `encoding`
    `(package private) static java.util.regex.Pattern` | `ENCODING_PATTERN`
    `private java.io.OutputStream` | `out`
    `private java.io.Writer` | `writer`
    `private java.io.StringWriter` | `xmlPrologWriter`

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

      `lock`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `XmlStreamWriter(java.io.File file)` Construct an new XML stream writer for the specified file with a default encoding of UTF-8.
    `XmlStreamWriter(java.io.File file, java.lang.String defaultEncoding)` Construct an new XML stream writer for the specified file with the specified default encoding.
    `XmlStreamWriter(java.io.OutputStream out)` Construct an new XML stream writer for the specified output stream with a default encoding of UTF-8.
    `XmlStreamWriter(java.io.OutputStream out, java.lang.String defaultEncoding)` Construct an new XML stream writer for the specified output stream with the specified default encoding.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()` Close the underlying writer.
    `private void` | `detectEncoding(char[] cbuf, int off, int len)` Detect the encoding.
    `void` | `flush()` Flush the underlying writer.
    `java.lang.String` | `getDefaultEncoding()` Return the default encoding.
    `java.lang.String` | `getEncoding()` Return the detected encoding.
    `void` | `write(char[] cbuf, int off, int len)` Write the characters to the underlying writer, detecing 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`

- - ### Field Detail

    - #### BUFFER_SIZE

          private static final int BUFFER_SIZE

      See Also:
        Constant Field Values

    - #### out

          private final java.io.OutputStream out

    - #### defaultEncoding

          private final java.lang.String defaultEncoding

    - #### xmlPrologWriter

          private java.io.StringWriter xmlPrologWriter

    - #### writer

          private java.io.Writer writer

    - #### encoding

          private java.lang.String encoding

    - #### ENCODING_PATTERN

          static final java.util.regex.Pattern ENCODING_PATTERN

  - ### Constructor Detail

    - #### XmlStreamWriter

          public XmlStreamWriter(java.io.OutputStream out)

      Construct an new XML stream writer for the specified output stream with a default encoding of UTF-8.
      Parameters:
        `out` - The output stream

    - #### XmlStreamWriter

          public XmlStreamWriter(java.io.OutputStream out,
                         java.lang.String defaultEncoding)

      Construct an 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

    - #### XmlStreamWriter

          public XmlStreamWriter(java.io.File file)
                          throws java.io.FileNotFoundException

      Construct an new XML stream writer for the specified file with a default encoding of UTF-8.
      Parameters:
        `file` - The file to write to
      Throws:
        `java.io.FileNotFoundException` - if there is an error creating or opening the file

    - #### XmlStreamWriter

          public XmlStreamWriter(java.io.File file,
                         java.lang.String defaultEncoding)
                          throws java.io.FileNotFoundException

      Construct an 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:
        `java.io.FileNotFoundException` - if there is an error creating or opening the file

  - ### Method Detail

    - #### getEncoding

          public java.lang.String getEncoding()

      Return the detected encoding.
      Returns:
        the detected encoding

    - #### getDefaultEncoding

          public java.lang.String getDefaultEncoding()

      Return the default encoding.
      Returns:
        the default encoding

    - #### close

          public void close()
                     throws java.io.IOException

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

    - #### flush

          public void flush()
                     throws java.io.IOException

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

    - #### detectEncoding

          private void detectEncoding(char[] cbuf,
                            int off,
                            int len)
                               throws java.io.IOException

      Detect the encoding.
      Parameters:
        `cbuf` - the buffer to write the characters from
        `off` - The start offset
        `len` - The number of characters to write
      Throws:
        `java.io.IOException` - if an error occurs detecting the encoding

    - #### write

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

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

Copyright (c) 2002-2016 Apache Software Foundation