| 1 | // -------------------------------------------------------------------------- |
|---|
| 2 | // |
|---|
| 3 | // File |
|---|
| 4 | // Name: BufferedWriteStream.cpp |
|---|
| 5 | // Purpose: Buffering write-only wrapper around IOStreams |
|---|
| 6 | // Created: 2010/09/13 |
|---|
| 7 | // |
|---|
| 8 | // -------------------------------------------------------------------------- |
|---|
| 9 | |
|---|
| 10 | #include "Box.h" |
|---|
| 11 | #include "BufferedWriteStream.h" |
|---|
| 12 | #include "CommonException.h" |
|---|
| 13 | |
|---|
| 14 | #include <string.h> |
|---|
| 15 | |
|---|
| 16 | #include "MemLeakFindOn.h" |
|---|
| 17 | |
|---|
| 18 | // -------------------------------------------------------------------------- |
|---|
| 19 | // |
|---|
| 20 | // Function |
|---|
| 21 | // Name: BufferedWriteStream::BufferedWriteStream(const char *, int, int) |
|---|
| 22 | // Purpose: Constructor, set up buffer |
|---|
| 23 | // Created: 2007/01/16 |
|---|
| 24 | // |
|---|
| 25 | // -------------------------------------------------------------------------- |
|---|
| 26 | BufferedWriteStream::BufferedWriteStream(IOStream& rSink) |
|---|
| 27 | : mrSink(rSink), mBufferPosition(0) |
|---|
| 28 | { } |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | // -------------------------------------------------------------------------- |
|---|
| 32 | // |
|---|
| 33 | // Function |
|---|
| 34 | // Name: BufferedWriteStream::Read(void *, int) |
|---|
| 35 | // Purpose: Reads bytes from the file - throws exception |
|---|
| 36 | // Created: 2007/01/16 |
|---|
| 37 | // |
|---|
| 38 | // -------------------------------------------------------------------------- |
|---|
| 39 | int BufferedWriteStream::Read(void *pBuffer, int NBytes, int Timeout) |
|---|
| 40 | { |
|---|
| 41 | THROW_EXCEPTION(CommonException, NotSupported); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | // -------------------------------------------------------------------------- |
|---|
| 46 | // |
|---|
| 47 | // Function |
|---|
| 48 | // Name: BufferedWriteStream::BytesLeftToRead() |
|---|
| 49 | // Purpose: Returns number of bytes to read (may not be most efficient function ever) |
|---|
| 50 | // Created: 2007/01/16 |
|---|
| 51 | // |
|---|
| 52 | // -------------------------------------------------------------------------- |
|---|
| 53 | IOStream::pos_type BufferedWriteStream::BytesLeftToRead() |
|---|
| 54 | { |
|---|
| 55 | THROW_EXCEPTION(CommonException, NotSupported); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | // -------------------------------------------------------------------------- |
|---|
| 60 | // |
|---|
| 61 | // Function |
|---|
| 62 | // Name: BufferedWriteStream::Write(void *, int) |
|---|
| 63 | // Purpose: Writes bytes to the underlying stream (not supported) |
|---|
| 64 | // Created: 2003/07/31 |
|---|
| 65 | // |
|---|
| 66 | // -------------------------------------------------------------------------- |
|---|
| 67 | void BufferedWriteStream::Write(const void *pBuffer, int NBytes) |
|---|
| 68 | { |
|---|
| 69 | int numBytesRemain = NBytes; |
|---|
| 70 | |
|---|
| 71 | do |
|---|
| 72 | { |
|---|
| 73 | int maxWritable = sizeof(mBuffer) - mBufferPosition; |
|---|
| 74 | int numBytesToWrite = (numBytesRemain < maxWritable) ? |
|---|
| 75 | numBytesRemain : maxWritable; |
|---|
| 76 | |
|---|
| 77 | if(numBytesToWrite > 0) |
|---|
| 78 | { |
|---|
| 79 | memcpy(mBuffer + mBufferPosition, pBuffer, |
|---|
| 80 | numBytesToWrite); |
|---|
| 81 | mBufferPosition += numBytesToWrite; |
|---|
| 82 | pBuffer = ((const char *)pBuffer) + numBytesToWrite; |
|---|
| 83 | numBytesRemain -= numBytesToWrite; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | if(numBytesRemain > 0) |
|---|
| 87 | { |
|---|
| 88 | Flush(); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | while(numBytesRemain > 0); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | // -------------------------------------------------------------------------- |
|---|
| 95 | // |
|---|
| 96 | // Function |
|---|
| 97 | // Name: BufferedWriteStream::GetPosition() |
|---|
| 98 | // Purpose: Get position in stream |
|---|
| 99 | // Created: 2003/08/21 |
|---|
| 100 | // |
|---|
| 101 | // -------------------------------------------------------------------------- |
|---|
| 102 | IOStream::pos_type BufferedWriteStream::GetPosition() const |
|---|
| 103 | { |
|---|
| 104 | return mrSink.GetPosition() + mBufferPosition; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | // -------------------------------------------------------------------------- |
|---|
| 109 | // |
|---|
| 110 | // Function |
|---|
| 111 | // Name: BufferedWriteStream::Seek(pos_type, int) |
|---|
| 112 | // Purpose: Seeks within file, as lseek, invalidate buffer |
|---|
| 113 | // Created: 2003/07/31 |
|---|
| 114 | // |
|---|
| 115 | // -------------------------------------------------------------------------- |
|---|
| 116 | void BufferedWriteStream::Seek(IOStream::pos_type Offset, int SeekType) |
|---|
| 117 | { |
|---|
| 118 | // Always flush the buffer before seeking |
|---|
| 119 | Flush(); |
|---|
| 120 | |
|---|
| 121 | mrSink.Seek(Offset, SeekType); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | // -------------------------------------------------------------------------- |
|---|
| 125 | // |
|---|
| 126 | // Function |
|---|
| 127 | // Name: BufferedWriteStream::Flush(); |
|---|
| 128 | // Purpose: Write out current buffer contents and invalidate |
|---|
| 129 | // Created: 2010/09/13 |
|---|
| 130 | // |
|---|
| 131 | // -------------------------------------------------------------------------- |
|---|
| 132 | void BufferedWriteStream::Flush(int Timeout) |
|---|
| 133 | { |
|---|
| 134 | if(mBufferPosition > 0) |
|---|
| 135 | { |
|---|
| 136 | mrSink.Write(mBuffer, mBufferPosition); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | mBufferPosition = 0; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | // -------------------------------------------------------------------------- |
|---|
| 143 | // |
|---|
| 144 | // Function |
|---|
| 145 | // Name: BufferedWriteStream::Close() |
|---|
| 146 | // Purpose: Closes the underlying stream (not needed) |
|---|
| 147 | // Created: 2003/07/31 |
|---|
| 148 | // |
|---|
| 149 | // -------------------------------------------------------------------------- |
|---|
| 150 | void BufferedWriteStream::Close() |
|---|
| 151 | { |
|---|
| 152 | Flush(); |
|---|
| 153 | mrSink.Close(); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | // -------------------------------------------------------------------------- |
|---|
| 157 | // |
|---|
| 158 | // Function |
|---|
| 159 | // Name: BufferedWriteStream::StreamDataLeft() |
|---|
| 160 | // Purpose: Any data left to write? |
|---|
| 161 | // Created: 2003/08/02 |
|---|
| 162 | // |
|---|
| 163 | // -------------------------------------------------------------------------- |
|---|
| 164 | bool BufferedWriteStream::StreamDataLeft() |
|---|
| 165 | { |
|---|
| 166 | THROW_EXCEPTION(CommonException, NotSupported); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | // -------------------------------------------------------------------------- |
|---|
| 170 | // |
|---|
| 171 | // Function |
|---|
| 172 | // Name: BufferedWriteStream::StreamClosed() |
|---|
| 173 | // Purpose: Is the stream closed? |
|---|
| 174 | // Created: 2003/08/02 |
|---|
| 175 | // |
|---|
| 176 | // -------------------------------------------------------------------------- |
|---|
| 177 | bool BufferedWriteStream::StreamClosed() |
|---|
| 178 | { |
|---|
| 179 | return mrSink.StreamClosed(); |
|---|
| 180 | } |
|---|
| 181 | |
|---|