Source Code Cross Referenced for BufferedOutputStream.java in  » JDK-Core » io-nio » java » io » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. JDK Core
2. JDK Modules
3. JDK Modules com.sun
4. JDK Modules com.sun.java
5. JDK Modules Platform
6. JDK Modules sun
7. Open Source Build
8. Open Source Graphic Library
9. Open Source IDE Eclipse
10. Open Source J2EE
11. Open Source JDBC Driver
12. Open Source Library
13. Open Source Library Database
14. Open Source Net
15. Open Source Script
16. Science
17. Security
18. Sevlet Container
19. SUN GlassFish
20. Swing Library
21. Web Services apache cxf 2.0.1
22. Web Services AXIS2
23. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java Source Code / Java Documentation » JDK Core » io nio » java.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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