50 |
*/ |
*/ |
51 |
public class BufferedOutputStream extends FilterOutputStream |
public class BufferedOutputStream extends FilterOutputStream |
52 |
{ |
{ |
|
/* |
|
|
* Class Variables |
|
|
*/ |
|
|
|
|
53 |
/** |
/** |
54 |
* This is the default buffer size |
* This is the default buffer size |
|
*/ |
|
|
private static final int DEFAULT_BUFFER_SIZE = 512; |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Instance Variables |
|
55 |
*/ |
*/ |
56 |
|
private static final int DEFAULT_BUFFER_SIZE = 512; |
57 |
|
|
58 |
/** |
/** |
59 |
* This is the internal byte array used for buffering output before |
* This is the internal byte array used for buffering output before |
60 |
* writing it. |
* writing it. |
61 |
*/ |
*/ |
62 |
protected byte[] buf; |
protected byte[] buf; |
63 |
|
|
64 |
/** |
/** |
65 |
* This is the number of bytes that are currently in the buffer and |
* This is the number of bytes that are currently in the buffer and |
66 |
* are waiting to be written to the underlying stream. It always points to |
* are waiting to be written to the underlying stream. It always points to |
67 |
* the index into the buffer where the next byte of data will be stored |
* the index into the buffer where the next byte of data will be stored |
|
*/ |
|
|
protected int count; |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Constructors |
|
68 |
*/ |
*/ |
69 |
|
protected int count; |
70 |
|
|
71 |
/** |
/** |
72 |
* This method initializes a new <code>BufferedOutputStream</code> instance |
* This method initializes a new <code>BufferedOutputStream</code> instance |
73 |
* that will write to the specified subordinate <code>OutputStream</code> |
* that will write to the specified subordinate <code>OutputStream</code> |
74 |
* and which will use a default buffer size of 512 bytes. |
* and which will use a default buffer size of 512 bytes. |
75 |
* |
* |
76 |
* @param out The underlying <code>OutputStream</code> to write data to |
* @param out The underlying <code>OutputStream</code> to write data to |
77 |
*/ |
*/ |
78 |
public BufferedOutputStream(OutputStream out) |
public BufferedOutputStream(OutputStream out) |
79 |
{ |
{ |
80 |
this(out, DEFAULT_BUFFER_SIZE); |
this(out, DEFAULT_BUFFER_SIZE); |
81 |
} |
} |
82 |
|
|
|
/*************************************************************************/ |
|
|
|
|
83 |
/** |
/** |
84 |
* This method initializes a new <code>BufferedOutputStream</code> instance |
* This method initializes a new <code>BufferedOutputStream</code> instance |
85 |
* that will write to the specified subordinate <code>OutputStream</code> |
* that will write to the specified subordinate <code>OutputStream</code> |
86 |
* and which will use the specified buffer size |
* and which will use the specified buffer size |
87 |
* |
* |
88 |
* @param out The underlying <code>OutputStream</code> to write data to |
* @param out The underlying <code>OutputStream</code> to write data to |
89 |
* @param size The size of the internal buffer |
* @param size The size of the internal buffer |
90 |
*/ |
*/ |
91 |
public BufferedOutputStream(OutputStream out, int size) |
public BufferedOutputStream(OutputStream out, int size) |
92 |
{ |
{ |
93 |
super(out); |
super(out); |
95 |
buf = new byte[size]; |
buf = new byte[size]; |
96 |
} |
} |
97 |
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Instance Methods |
|
|
*/ |
|
|
|
|
98 |
/** |
/** |
99 |
* This method causes any currently buffered bytes to be immediately |
* This method causes any currently buffered bytes to be immediately |
100 |
* written to the underlying output stream. |
* written to the underlying output stream. |
101 |
* |
* |
102 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
103 |
*/ |
*/ |
104 |
public synchronized void flush() throws IOException |
public synchronized void flush() throws IOException |
105 |
{ |
{ |
106 |
if (count == 0) |
if (count == 0) |
111 |
out.flush(); |
out.flush(); |
112 |
} |
} |
113 |
|
|
114 |
/*************************************************************************/ |
/** |
115 |
|
* This method flushes any remaining buffered bytes then closes the |
116 |
/* |
* underlying output stream. Any further attempts to write to this stream |
117 |
* This method flushes any remaining buffered bytes then closes the |
* may throw an exception |
118 |
* underlying output stream. Any further attempts to write to this stream |
* |
|
* may throw an exception |
|
|
* |
|
119 |
public synchronized void close() throws IOException |
public synchronized void close() throws IOException |
120 |
{ |
{ |
121 |
flush(); |
flush(); |
123 |
} |
} |
124 |
*/ |
*/ |
125 |
|
|
126 |
/*************************************************************************/ |
/** |
127 |
|
* This method runs when the object is garbage collected. It is |
128 |
/* |
* responsible for ensuring that all buffered bytes are written and |
129 |
* This method runs when the object is garbage collected. It is |
* for closing the underlying stream. |
130 |
* responsible for ensuring that all buffered bytes are written and |
* |
131 |
* for closing the underlying stream. |
* @exception IOException If an error occurs (ignored by the Java runtime) |
132 |
* |
* |
|
* @exception IOException If an error occurs (ignored by the Java runtime) |
|
|
* |
|
133 |
protected void finalize() throws IOException |
protected void finalize() throws IOException |
134 |
{ |
{ |
135 |
close(); |
close(); |
136 |
} |
} |
137 |
*/ |
*/ |
138 |
|
|
|
/*************************************************************************/ |
|
|
|
|
139 |
/** |
/** |
140 |
* This method writes a single byte of data. This will be written to the |
* This method writes a single byte of data. This will be written to the |
141 |
* buffer instead of the underlying data source. However, if the buffer |
* buffer instead of the underlying data source. However, if the buffer |
142 |
* is filled as a result of this write request, it will be flushed to the |
* is filled as a result of this write request, it will be flushed to the |
143 |
* underlying output stream. |
* underlying output stream. |
144 |
* |
* |
145 |
* @param b The byte of data to be written, passed as an int |
* @param b The byte of data to be written, passed as an int |
146 |
* |
* |
147 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
148 |
*/ |
*/ |
149 |
public synchronized void write(int b) throws IOException |
public synchronized void write(int b) throws IOException |
150 |
{ |
{ |
151 |
if (count == buf.length) |
if (count == buf.length) |
155 |
++count; |
++count; |
156 |
} |
} |
157 |
|
|
|
/*************************************************************************/ |
|
|
|
|
158 |
/** |
/** |
159 |
* This method writes <code>len</code> bytes from the byte array |
* This method writes <code>len</code> bytes from the byte array |
160 |
* <code>buf</code> starting at position <code>offset</code> in the buffer. |
* <code>buf</code> starting at position <code>offset</code> in the buffer. |
161 |
* These bytes will be written to the internal buffer. However, if this |
* These bytes will be written to the internal buffer. However, if this |
162 |
* write operation fills the buffer, the buffer will be flushed to the |
* write operation fills the buffer, the buffer will be flushed to the |
163 |
* underlying output stream. |
* underlying output stream. |
164 |
* |
* |
165 |
* @param buf The array of bytes to write. |
* @param buf The array of bytes to write. |
166 |
* @param offset The index into the byte array to start writing from. |
* @param offset The index into the byte array to start writing from. |
167 |
* @param len The number of bytes to write. |
* @param len The number of bytes to write. |
168 |
* |
* |
169 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
170 |
*/ |
*/ |
171 |
public synchronized void write(byte[] buf, int offset, int len) |
public synchronized void write(byte[] buf, int offset, int len) |
172 |
throws IOException |
throws IOException |
173 |
{ |
{ |