Package java.io
Class BufferedOutputStream
java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
java.io.BufferedOutputStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
public class BufferedOutputStream extends FilterOutputStream
Wraps an existing
OutputStream and buffers the output.
Expensive interaction with the underlying input stream is minimized, since
most (smaller) requests can be satisfied by accessing the buffer alone. The
drawback is that some extra space is required to hold the buffer and that
copying takes place when flushing that buffer, but this is usually outweighed
by the performance benefits.
A typical application pattern for the class looks like this:
BufferedOutputStream buf = new BufferedOutputStream(new FileOutputStream("file.java"));
- See Also:
BufferedInputStream
-
Field Summary
Fields Modifier and Type Field Description protected byte[]bufThe buffer containing the bytes to be written to the target stream.protected intcountThe total number of bytes inside the byte arraybuf.Fields inherited from class java.io.FilterOutputStream
out -
Constructor Summary
Constructors Constructor Description BufferedOutputStream(OutputStream out)Constructs a newBufferedOutputStream, providingoutwith a buffer of 8192 bytes.BufferedOutputStream(OutputStream out, int size)Constructs a newBufferedOutputStream, providingoutwithsizebytes of buffer. -
Method Summary
Modifier and Type Method Description voidclose()Closes this stream.voidflush()Flushes this stream to ensure all pending data is written out to the target stream.voidwrite(byte[] buffer, int offset, int length)Writescountbytes from the byte arraybufferstarting atoffsetto this stream.voidwrite(int oneByte)Writes one byte to this stream.Methods inherited from class java.io.OutputStream
write
-
Field Details
-
buf
protected byte[] bufThe buffer containing the bytes to be written to the target stream. -
count
protected int countThe total number of bytes inside the byte arraybuf.
-
-
Constructor Details
-
BufferedOutputStream
Constructs a newBufferedOutputStream, providingoutwith a buffer of 8192 bytes.- Parameters:
out- theOutputStreamthe buffer writes to.
-
BufferedOutputStream
Constructs a newBufferedOutputStream, providingoutwithsizebytes of buffer.- Parameters:
out- theOutputStreamthe buffer writes to.size- the size of buffer in bytes.- Throws:
IllegalArgumentException- ifsize <= 0.
-
-
Method Details
-
flush
Flushes this stream to ensure all pending data is written out to the target stream. In addition, the target stream is flushed.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classFilterOutputStream- Throws:
IOException- if an error occurs attempting to flush this stream.
-
write
Writescountbytes from the byte arraybufferstarting atoffsetto this stream. If there is room in the buffer to hold the bytes, they are copied in. If not, the buffered bytes plus the bytes inbufferare written to the target stream, the target is flushed, and the buffer is cleared.- Overrides:
writein classFilterOutputStream- Parameters:
buffer- the buffer to be written.offset- the start position inbufferfrom where to get bytes.length- the number of bytes frombufferto write to this stream.- Throws:
IndexOutOfBoundsException- ifoffset < 0orlength < 0, or ifoffset + lengthis greater than the size ofbuffer.IOException- if an error occurs attempting to write to this stream.NullPointerException- ifbufferisnull.ArrayIndexOutOfBoundsException- If offset or count is outside of bounds.
-
close
Description copied from class:FilterOutputStreamCloses this stream. This implementation closes the target stream.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classFilterOutputStream- Throws:
IOException- if an error occurs attempting to close this stream.
-
write
Writes one byte to this stream. Only the low order byte of the integeroneByteis written. If there is room in the buffer, the byte is copied into the buffer and the count incremented. Otherwise, the buffer plusoneByteare written to the target stream, the target is flushed, and the buffer is reset.- Overrides:
writein classFilterOutputStream- Parameters:
oneByte- the byte to be written.- Throws:
IOException- if an error occurs attempting to write to this stream.
-