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

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 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        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.