1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/compact/src/main/java/java/io/BufferedOutputStream.java Tue Feb 26 16:54:16 2013 +0100
1.3 @@ -0,0 +1,143 @@
1.4 +/*
1.5 + * Copyright (c) 1994, 2003, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.io;
1.30 +
1.31 +/**
1.32 + * The class implements a buffered output stream. By setting up such
1.33 + * an output stream, an application can write bytes to the underlying
1.34 + * output stream without necessarily causing a call to the underlying
1.35 + * system for each byte written.
1.36 + *
1.37 + * @author Arthur van Hoff
1.38 + * @since JDK1.0
1.39 + */
1.40 +public
1.41 +class BufferedOutputStream extends FilterOutputStream {
1.42 + /**
1.43 + * The internal buffer where data is stored.
1.44 + */
1.45 + protected byte buf[];
1.46 +
1.47 + /**
1.48 + * The number of valid bytes in the buffer. This value is always
1.49 + * in the range <tt>0</tt> through <tt>buf.length</tt>; elements
1.50 + * <tt>buf[0]</tt> through <tt>buf[count-1]</tt> contain valid
1.51 + * byte data.
1.52 + */
1.53 + protected int count;
1.54 +
1.55 + /**
1.56 + * Creates a new buffered output stream to write data to the
1.57 + * specified underlying output stream.
1.58 + *
1.59 + * @param out the underlying output stream.
1.60 + */
1.61 + public BufferedOutputStream(OutputStream out) {
1.62 + this(out, 8192);
1.63 + }
1.64 +
1.65 + /**
1.66 + * Creates a new buffered output stream to write data to the
1.67 + * specified underlying output stream with the specified buffer
1.68 + * size.
1.69 + *
1.70 + * @param out the underlying output stream.
1.71 + * @param size the buffer size.
1.72 + * @exception IllegalArgumentException if size <= 0.
1.73 + */
1.74 + public BufferedOutputStream(OutputStream out, int size) {
1.75 + super(out);
1.76 + if (size <= 0) {
1.77 + throw new IllegalArgumentException("Buffer size <= 0");
1.78 + }
1.79 + buf = new byte[size];
1.80 + }
1.81 +
1.82 + /** Flush the internal buffer */
1.83 + private void flushBuffer() throws IOException {
1.84 + if (count > 0) {
1.85 + out.write(buf, 0, count);
1.86 + count = 0;
1.87 + }
1.88 + }
1.89 +
1.90 + /**
1.91 + * Writes the specified byte to this buffered output stream.
1.92 + *
1.93 + * @param b the byte to be written.
1.94 + * @exception IOException if an I/O error occurs.
1.95 + */
1.96 + public synchronized void write(int b) throws IOException {
1.97 + if (count >= buf.length) {
1.98 + flushBuffer();
1.99 + }
1.100 + buf[count++] = (byte)b;
1.101 + }
1.102 +
1.103 + /**
1.104 + * Writes <code>len</code> bytes from the specified byte array
1.105 + * starting at offset <code>off</code> to this buffered output stream.
1.106 + *
1.107 + * <p> Ordinarily this method stores bytes from the given array into this
1.108 + * stream's buffer, flushing the buffer to the underlying output stream as
1.109 + * needed. If the requested length is at least as large as this stream's
1.110 + * buffer, however, then this method will flush the buffer and write the
1.111 + * bytes directly to the underlying output stream. Thus redundant
1.112 + * <code>BufferedOutputStream</code>s will not copy data unnecessarily.
1.113 + *
1.114 + * @param b the data.
1.115 + * @param off the start offset in the data.
1.116 + * @param len the number of bytes to write.
1.117 + * @exception IOException if an I/O error occurs.
1.118 + */
1.119 + public synchronized void write(byte b[], int off, int len) throws IOException {
1.120 + if (len >= buf.length) {
1.121 + /* If the request length exceeds the size of the output buffer,
1.122 + flush the output buffer and then write the data directly.
1.123 + In this way buffered streams will cascade harmlessly. */
1.124 + flushBuffer();
1.125 + out.write(b, off, len);
1.126 + return;
1.127 + }
1.128 + if (len > buf.length - count) {
1.129 + flushBuffer();
1.130 + }
1.131 + System.arraycopy(b, off, buf, count, len);
1.132 + count += len;
1.133 + }
1.134 +
1.135 + /**
1.136 + * Flushes this buffered output stream. This forces any buffered
1.137 + * output bytes to be written out to the underlying output stream.
1.138 + *
1.139 + * @exception IOException if an I/O error occurs.
1.140 + * @see java.io.FilterOutputStream#out
1.141 + */
1.142 + public synchronized void flush() throws IOException {
1.143 + flushBuffer();
1.144 + out.flush();
1.145 + }
1.146 +}