001: /*
002: * @(#)BufferedOutputStream.java 1.35 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.io;
029:
030: /**
031: * The class implements a buffered output stream. By setting up such
032: * an output stream, an application can write bytes to the underlying
033: * output stream without necessarily causing a call to the underlying
034: * system for each byte written.
035: *
036: * @version 1.27, 02/02/00
037: * @since JDK1.0
038: */
039: public class BufferedOutputStream extends FilterOutputStream {
040: /**
041: * The internal buffer where data is stored.
042: */
043: protected byte buf[];
044:
045: /**
046: * The number of valid bytes in the buffer. This value is always
047: * in the range <tt>0</tt> through <tt>buf.length</tt>; elements
048: * <tt>buf[0]</tt> through <tt>buf[count-1]</tt> contain valid
049: * byte data.
050: */
051: protected int count;
052:
053: /**
054: * Creates a new buffered output stream to write data to the
055: * specified underlying output stream with a default 512-byte
056: * buffer size.
057: *
058: * @param out the underlying output stream.
059: */
060: public BufferedOutputStream(OutputStream out) {
061: this (out, 512);
062: }
063:
064: /**
065: * Creates a new buffered output stream to write data to the
066: * specified underlying output stream with the specified buffer
067: * size.
068: *
069: * @param out the underlying output stream.
070: * @param size the buffer size.
071: * @exception IllegalArgumentException if size <= 0.
072: */
073: public BufferedOutputStream(OutputStream out, int size) {
074: super (out);
075: if (size <= 0) {
076: throw new IllegalArgumentException("Buffer size <= 0");
077: }
078: buf = new byte[size];
079: }
080:
081: /** Flush the internal buffer */
082: private void flushBuffer() throws IOException {
083: if (count > 0) {
084: out.write(buf, 0, count);
085: count = 0;
086: }
087: }
088:
089: /**
090: * Writes the specified byte to this buffered output stream.
091: *
092: * @param b the byte to be written.
093: * @exception IOException if an I/O error occurs.
094: */
095: public synchronized void write(int b) throws IOException {
096: if (count >= buf.length) {
097: flushBuffer();
098: }
099: buf[count++] = (byte) b;
100: }
101:
102: /**
103: * Writes <code>len</code> bytes from the specified byte array
104: * starting at offset <code>off</code> to this buffered output stream.
105: *
106: * <p> Ordinarily this method stores bytes from the given array into this
107: * stream's buffer, flushing the buffer to the underlying output stream as
108: * needed. If the requested length is at least as large as this stream's
109: * buffer, however, then this method will flush the buffer and write the
110: * bytes directly to the underlying output stream. Thus redundant
111: * <code>BufferedOutputStream</code>s will not copy data unnecessarily.
112: *
113: * @param b the data.
114: * @param off the start offset in the data.
115: * @param len the number of bytes to write.
116: * @exception IOException if an I/O error occurs.
117: */
118: public synchronized void write(byte b[], int off, int len)
119: throws IOException {
120: if (len >= buf.length) {
121: /* If the request length exceeds the size of the output buffer,
122: flush the output buffer and then write the data directly.
123: In this way buffered streams will cascade harmlessly. */
124: flushBuffer();
125: out.write(b, off, len);
126: return;
127: }
128: if (len > buf.length - count) {
129: flushBuffer();
130: }
131: System.arraycopy(b, off, buf, count, len);
132: count += len;
133: }
134:
135: /**
136: * Flushes this buffered output stream. This forces any buffered
137: * output bytes to be written out to the underlying output stream.
138: *
139: * @exception IOException if an I/O error occurs.
140: * @see java.io.FilterOutputStream#out
141: */
142: public synchronized void flush() throws IOException {
143: flushBuffer();
144: out.flush();
145: }
146: }
|