Package org.ka2ddo.util
Class UTF8StreamWriter
java.lang.Object
java.io.Writer
org.ka2ddo.util.UTF8StreamWriter
- All Implemented Interfaces:
Closeable
,Flushable
,Appendable
,AutoCloseable
This class implements a memory-optimized optimized writer-to-stream conversion assuming only UTF-8.
It is recommended that a NonshareableBufferedOutputStream be used below this class
due to the large number of calls to the encapsulated OutputStream's write(int) method.
- Author:
- Andrew Pavlin, KA2DDO
-
Field Summary
-
Constructor Summary
ConstructorDescriptionCreate a UTF8StreamWriter around the specified OutputStream. -
Method Summary
Modifier and TypeMethodDescriptionappend
(char c) Appends the specified character to this writer.void
close()
Closes the stream, flushing it first.void
flush()
Flushes the stream.void
write
(char[] cbuf, int off, int len) Writes a portion of an array of characters.void
write
(int c) Writes a single character.void
Writes a portion of a string.
-
Constructor Details
-
UTF8StreamWriter
Create a UTF8StreamWriter around the specified OutputStream.- Parameters:
out
- OutputStream to feed UTF-8 encoded data
-
-
Method Details
-
write
Writes a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.- Overrides:
write
in classWriter
- Parameters:
c
- int specifying a character to be written- Throws:
IOException
- If an I/O error occurs
-
append
Appends the specified character to this writer.An invocation of this method of the form
out.append(c)
behaves in exactly the same way as the invocationout.write(c)
- Specified by:
append
in interfaceAppendable
- Overrides:
append
in classWriter
- Parameters:
c
- The 16-bit character to append- Returns:
- This writer
- Throws:
IOException
- If an I/O error occurs- Since:
- 1.5
-
write
Writes a portion of an array of characters.- Specified by:
write
in classWriter
- Parameters:
cbuf
- Array of charactersoff
- Offset from which to start writing characterslen
- Number of characters to write- Throws:
IOException
- If an I/O error occurs
-
write
Writes a portion of a string.- Overrides:
write
in classWriter
- Parameters:
str
- A Stringoff
- Offset from which to start writing characterslen
- Number of characters to write- Throws:
IndexOutOfBoundsException
- Ifoff
is negative, orlen
is negative, oroff+len
is negative or greater than the length of the given stringIOException
- If an I/O error occurs
-
flush
Flushes the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.- Specified by:
flush
in interfaceFlushable
- Specified by:
flush
in classWriter
- Throws:
IOException
- If an I/O error occurs
-
close
Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in classWriter
- Throws:
IOException
- If an I/O error occurs
-