KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > unimi > dsi > fastutil > io > FastBufferedOutputStream


1 package it.unimi.dsi.fastutil.io;
2
3 /*
4  * fastutil: Fast & compact type-specific collections for Java
5  *
6  * Copyright (C) 2005, 2006 Sebastiano Vigna
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */

23
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26
27 /** Lightweight, unsynchronized, aligned output stream buffering class.
28  *
29  * <P>This class provides buffering for output streams, but it does so with
30  * purposes and an internal logic that are radically different from the ones
31  * adopted in {@link java.io.BufferedOutputStream}.
32  *
33  * <P>All methods are unsychronized. Moreover,
34  * it is guaranteed that
35  * <em>all writes performed by this class will be
36  * multiples of the given buffer size</em>. If, for instance, you use the
37  * default buffer size, writes will be performed on the underlying input stream
38  * in multiples of 16384 bytes. This is very important on operating systems
39  * that optimize disk reads on disk block boundaries. If you {@link #flush()} the stream,
40  * the buffer will be emptied, but it will realign again as soon as possible.
41  *
42  * @since 4.4
43  */

44
45 public class FastBufferedOutputStream extends OutputStream JavaDoc {
46
47     /** The default size of the internal buffer in bytes (8Ki). */
48     public final static int DEFAULT_BUFFER_SIZE = 8 * 1024;
49
50     /** The internal buffer. */
51     protected byte buffer[];
52
53     /** The current position in the buffer. */
54     protected int pos;
55
56     /** The number of buffer bytes available starting from {@link #pos}. Note that in case
57      * {@link #flush()} has been called, the number of available buffer bytes might be less
58      * than {@link #buffer buffer.length}&minus;{@link #pos}. */

59     protected int avail;
60
61     /** The underlying output stream. */
62     protected OutputStream JavaDoc os;
63
64     /** Creates a new fast buffered output stream by wrapping a given output stream with a given buffer size.
65      *
66      * @param os an output stream to wrap.
67      * @param bufSize the size in bytes of the internal buffer.
68      */

69
70     public FastBufferedOutputStream( final OutputStream JavaDoc os, final int bufSize ) {
71         this.os = os;
72         buffer = new byte[ bufSize ];
73         avail = bufSize;
74     }
75
76     /** Creates a new fast buffered ouptut stream by wrapping a given output stream with a buffer of {@link #DEFAULT_BUFFER_SIZE} bytes.
77      *
78      * @param os an output stream to wrap.
79      */

80     public FastBufferedOutputStream( final OutputStream JavaDoc os ) {
81         this( os, DEFAULT_BUFFER_SIZE );
82     }
83
84     private void dumpBufferIfFull() throws IOException JavaDoc {
85         if ( avail == 0 ) {
86             os.write( buffer, 0, pos );
87             pos = 0;
88             avail = buffer.length;
89         }
90     }
91
92     public void write( final int b ) throws IOException JavaDoc {
93         avail--;
94         buffer[ pos++ ] = (byte)b;
95         dumpBufferIfFull();
96     }
97
98
99     public void write( final byte b[], int offset, int length ) throws IOException JavaDoc {
100         if ( length <= avail ) {
101             System.arraycopy( b, offset, buffer, pos, length );
102             pos += length;
103             avail -= length;
104             dumpBufferIfFull();
105             return;
106         }
107     
108         System.arraycopy( b, offset, buffer, pos, avail );
109         os.write( buffer, 0, pos + avail );
110
111         offset += avail;
112         length -= avail;
113
114         final int residual = length % buffer.length;
115
116         os.write( b, offset, length - residual );
117         System.arraycopy( b, offset + length - residual, buffer, 0, residual );
118         pos = residual;
119         avail = buffer.length - residual;
120     }
121
122     public void flush() throws IOException JavaDoc {
123         if ( pos != 0 ) os.write( buffer, 0, pos );
124         // Note that avail is unchanged, so we will realign at the next dump.
125
pos = 0;
126         os.flush();
127     }
128     
129     public void close() throws IOException JavaDoc {
130         if ( os == null ) return;
131         if ( pos != 0 ) os.write( buffer, 0, pos );
132         if ( os != System.out ) os.close();
133         os = null;
134         buffer = null;
135     }
136
137 }
138
Popular Tags