KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > trove > io > FastBufferedOutputStream


1 /*
2  * @(#)BufferedOutputStream.java 1.27 00/02/02
3  *
4  * Copyright 1994-2000 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * This software is the proprietary information of Sun Microsystems, Inc.
7  * Use is subject to license terms.
8  *
9  */

10
11 package com.go.trove.io;
12
13 import java.io.*;
14
15 /**
16  * FastBufferedOutputStream is just a slightly modified version of
17  * {@link java.io.BufferedOutputStream}. The synchronization is gone, and so
18  * writes are faster. Refer to the original BufferedOutputStream for
19  * documentation.
20  */

21 /*
22  * @author Arthur van Hoff
23  * @version 1.27, 02/02/00
24  * @since JDK1.0
25  */

26 public class FastBufferedOutputStream extends FilterOutputStream {
27     // These fields have been renamed and made private. In the original, they
28
// are protected.
29
private byte[] mBuffer;
30     private int mCount;
31
32     public FastBufferedOutputStream(OutputStream out) {
33         this(out, 512);
34     }
35
36     public FastBufferedOutputStream(OutputStream out, int size) {
37         super(out);
38         if (size <= 0) {
39             throw new IllegalArgumentException JavaDoc("Buffer size <= 0");
40         }
41         mBuffer = new byte[size];
42     }
43
44     private void flushBuffer() throws IOException {
45         if (mCount > 0) {
46             out.write(mBuffer, 0, mCount);
47             mCount = 0;
48         }
49     }
50
51     public void write(int b) throws IOException {
52         if (mCount >= mBuffer.length) {
53             flushBuffer();
54         }
55         mBuffer[mCount++] = (byte)b;
56     }
57
58     public void write(byte b[], int off, int len) throws IOException {
59         if (len >= mBuffer.length) {
60             flushBuffer();
61             out.write(b, off, len);
62             return;
63         }
64         if (len > mBuffer.length - mCount) {
65             flushBuffer();
66         }
67         System.arraycopy(b, off, mBuffer, mCount, len);
68         mCount += len;
69     }
70
71     public synchronized void flush() throws IOException {
72         flushBuffer();
73         out.flush();
74     }
75 }
76
Popular Tags