1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
#include "stdafx.h"
#include "BufferedOutputStream.h"
//Creates a new buffered output stream to write data to the specified underlying output stream with the specified buffer size.
//Parameters:
//out - the underlying output stream.
//size - the buffer size.
BufferedOutputStream::BufferedOutputStream(OutputStream *out, int size)
{
stream = out;
buf = byteArray( size );
count = 0;
}
BufferedOutputStream::~BufferedOutputStream()
{
delete buf.data;
}
//Flushes this buffered output stream. This forces any buffered output bytes to be written out to the underlying output stream.
void BufferedOutputStream::flush()
{
stream->write( buf, 0, count );
count = 0;
}
//Closes this output stream and releases any system resources associated with the stream.
//The close method of FilterOutputStream calls its flush method, and then calls the close method of its underlying output stream.
void BufferedOutputStream::close()
{
flush();
stream->close();
}
//Writes len bytes from the specified byte array starting at offset off to this buffered output stream.
//Ordinarily this method stores bytes from the given array into this stream's buffer, flushing the buffer to the
//underlying output stream as needed. If the requested length is at least as large as this stream's buffer, however,
//then this method will flush the buffer and write the bytes directly to the underlying output stream.
//Thus redundant BufferedOutputStreams will not copy data unnecessarily.
//
//Overrides:
//write in class FilterOutputStream
//Parameters:
//b - the data.
//off - the start offset in the data.
//len - the number of bytes to write.
void BufferedOutputStream::write(byteArray b, unsigned int offset, unsigned int length)
{
// Over the length of what we can store in our buffer - just flush the buffer and output directly
if( length >= buf.length )
{
flush();
stream->write(b, offset, length);
}
else
{
for(unsigned int i = 0; i < length; i++ )
{
write( b[offset+i] );
}
}
}
//Writes b.length bytes to this output stream.
//The write method of FilterOutputStream calls its write method of three arguments with the arguments b, 0, and b.length.
//
//Note that this method does not call the one-argument write method of its underlying stream with the single argument b.
void BufferedOutputStream::write(byteArray b)
{
write( b, 0, b.length );
}
//Writes the specified byte to this buffered output stream.
//Overrides:
//write in class FilterOutputStream
//Parameters:
//b - the byte to be written.
void BufferedOutputStream::write(unsigned int b)
{
buf[count++] = static_cast<byte>(b);
if( count == buf.length )
{
flush();
}
}
|