Package com.helger.commons.io.stream
Class NonBlockingBufferedWriter
- java.lang.Object
-
- java.io.Writer
-
- com.helger.commons.io.stream.NonBlockingBufferedWriter
-
- All Implemented Interfaces:
Closeable,Flushable,Appendable,AutoCloseable
@NotThreadSafe public class NonBlockingBufferedWriter extends Writer
This is a non-blocking version ofBufferedWriter. It is 1:1 rip without the synchronized statements.- Author:
- Philip Helger
-
-
Constructor Summary
Constructors Constructor Description NonBlockingBufferedWriter(Writer aWriter)Creates a buffered character-output stream that uses a default-sized output buffer.NonBlockingBufferedWriter(Writer aWriter, int nBufSize)Creates a new buffered character-output stream that uses an output buffer of the given size.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voidflush()Flushes the stream.protected voidflushBuffer()Flushes the output buffer to the underlying character stream, without flushing the stream itself.StringgetAsString()Converts input data to a string.StringgetAsString(int nLength)Converts input data to a string.intgetBufferSize()intgetSize()booleanisEmpty()voidnewLine()Writes a line separator.voidreset()Remove all content of the buffer.voidwrite(char[] cbuf, int nOfs, int nLen)Writes a portion of an array of characters.voidwrite(int c)Writes a single character.voidwrite(String s, int off, int len)Writes a portion of a String.
-
-
-
Constructor Detail
-
NonBlockingBufferedWriter
public NonBlockingBufferedWriter(@Nonnull Writer aWriter)
Creates a buffered character-output stream that uses a default-sized output buffer.- Parameters:
aWriter- A Writer
-
NonBlockingBufferedWriter
public NonBlockingBufferedWriter(@Nonnull Writer aWriter, @Nonnegative int nBufSize)
Creates a new buffered character-output stream that uses an output buffer of the given size.- Parameters:
aWriter- A WriternBufSize- Output-buffer size, a positive integer- Throws:
IllegalArgumentException- If size is ≤ 0
-
-
Method Detail
-
flushBuffer
protected void flushBuffer() throws IOExceptionFlushes the output buffer to the underlying character stream, without flushing the stream itself. This method is non-private only so that it may be invoked by PrintStream.- Throws:
IOException- of the writer is not open
-
write
public void write(int c) throws IOExceptionWrites a single character.- Overrides:
writein classWriter- Throws:
IOException- If an I/O error occurs
-
write
public void write(char[] cbuf, int nOfs, int nLen) throws IOExceptionWrites a portion of an array of characters.Ordinarily this method stores characters from the given array into this stream's buffer, flushing the buffer to the underlying stream as needed. If the requested length is at least as large as the buffer, however, then this method will flush the buffer and write the characters directly to the underlying stream. Thus redundant
BufferedWriters will not copy data unnecessarily.- Specified by:
writein classWriter- Parameters:
cbuf- A character arraynOfs- Offset from which to start reading charactersnLen- Number of characters to write- Throws:
IOException- If an I/O error occurs
-
write
public void write(String s, int off, int len) throws IOException
Writes a portion of a String.If the value of the
lenparameter is negative then no characters are written. This is contrary to the specification of this method in the superclass, which requires that anIndexOutOfBoundsExceptionbe thrown.- Overrides:
writein classWriter- Parameters:
s- String to be writtenoff- Offset from which to start reading characterslen- Number of characters to be written- Throws:
IOException- If an I/O error occurs
-
reset
public void reset()
Remove all content of the buffer.
-
getSize
@Nonnegative public int getSize()
-
getBufferSize
@Nonnegative public int getBufferSize()
-
isEmpty
public boolean isEmpty()
-
getAsString
@Nonnull @ReturnsMutableCopy public String getAsString()
Converts input data to a string.- Returns:
- the string.
-
getAsString
@Nonnull public String getAsString(@Nonnegative int nLength)
Converts input data to a string.- Parameters:
nLength- The number of characters to convert. Must be ≤ thangetSize().- Returns:
- the string.
-
newLine
public void newLine() throws IOExceptionWrites a line separator. The line separator string is defined by the system propertyline.separator, and is not necessarily a single newline ('\n') character.- Throws:
IOException- If an I/O error occurs
-
flush
public void flush() throws IOExceptionFlushes the stream.- Specified by:
flushin interfaceFlushable- Specified by:
flushin classWriter- Throws:
IOException- If an I/O error occurs
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classWriter- Throws:
IOException
-
-