| 1 | // -------------------------------------------------------------------------- |
|---|
| 2 | // |
|---|
| 3 | // File |
|---|
| 4 | // Name: PartialReadStream.h |
|---|
| 5 | // Purpose: Read part of another stream |
|---|
| 6 | // Created: 2003/08/26 |
|---|
| 7 | // |
|---|
| 8 | // -------------------------------------------------------------------------- |
|---|
| 9 | |
|---|
| 10 | #include "Box.h" |
|---|
| 11 | #include "PartialReadStream.h" |
|---|
| 12 | #include "CommonException.h" |
|---|
| 13 | |
|---|
| 14 | #include "MemLeakFindOn.h" |
|---|
| 15 | |
|---|
| 16 | // -------------------------------------------------------------------------- |
|---|
| 17 | // |
|---|
| 18 | // Function |
|---|
| 19 | // Name: PartialReadStream::PartialReadStream(IOStream &, |
|---|
| 20 | // pos_type) |
|---|
| 21 | // Purpose: Constructor, taking another stream and the number of |
|---|
| 22 | // bytes to be read from it. |
|---|
| 23 | // Created: 2003/08/26 |
|---|
| 24 | // |
|---|
| 25 | // -------------------------------------------------------------------------- |
|---|
| 26 | PartialReadStream::PartialReadStream(IOStream &rSource, |
|---|
| 27 | pos_type BytesToRead) |
|---|
| 28 | : mrSource(rSource), |
|---|
| 29 | mBytesLeft(BytesToRead) |
|---|
| 30 | { |
|---|
| 31 | ASSERT(BytesToRead > 0); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | // -------------------------------------------------------------------------- |
|---|
| 35 | // |
|---|
| 36 | // Function |
|---|
| 37 | // Name: PartialReadStream::~PartialReadStream() |
|---|
| 38 | // Purpose: Destructor. Won't absorb any unread bytes. |
|---|
| 39 | // Created: 2003/08/26 |
|---|
| 40 | // |
|---|
| 41 | // -------------------------------------------------------------------------- |
|---|
| 42 | PartialReadStream::~PartialReadStream() |
|---|
| 43 | { |
|---|
| 44 | // Warn in debug mode |
|---|
| 45 | if(mBytesLeft != 0) |
|---|
| 46 | { |
|---|
| 47 | BOX_TRACE("PartialReadStream destroyed with " << mBytesLeft << |
|---|
| 48 | " bytes remaining"); |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | // -------------------------------------------------------------------------- |
|---|
| 53 | // |
|---|
| 54 | // Function |
|---|
| 55 | // Name: PartialReadStream::Read(void *, int, int) |
|---|
| 56 | // Purpose: As interface. |
|---|
| 57 | // Created: 2003/08/26 |
|---|
| 58 | // |
|---|
| 59 | // -------------------------------------------------------------------------- |
|---|
| 60 | int PartialReadStream::Read(void *pBuffer, int NBytes, int Timeout) |
|---|
| 61 | { |
|---|
| 62 | // Finished? |
|---|
| 63 | if(mBytesLeft <= 0) |
|---|
| 64 | { |
|---|
| 65 | return 0; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | // Asking for more than is allowed? |
|---|
| 69 | if(NBytes > mBytesLeft) |
|---|
| 70 | { |
|---|
| 71 | // Adjust downwards |
|---|
| 72 | NBytes = mBytesLeft; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | // Route the request to the source |
|---|
| 76 | int read = mrSource.Read(pBuffer, NBytes, Timeout); |
|---|
| 77 | ASSERT(read <= mBytesLeft); |
|---|
| 78 | |
|---|
| 79 | // Adjust the count |
|---|
| 80 | mBytesLeft -= read; |
|---|
| 81 | |
|---|
| 82 | // Return the number read |
|---|
| 83 | return read; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | // -------------------------------------------------------------------------- |
|---|
| 87 | // |
|---|
| 88 | // Function |
|---|
| 89 | // Name: PartialReadStream::BytesLeftToRead() |
|---|
| 90 | // Purpose: As interface. |
|---|
| 91 | // Created: 2003/08/26 |
|---|
| 92 | // |
|---|
| 93 | // -------------------------------------------------------------------------- |
|---|
| 94 | IOStream::pos_type PartialReadStream::BytesLeftToRead() |
|---|
| 95 | { |
|---|
| 96 | return mBytesLeft; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | // -------------------------------------------------------------------------- |
|---|
| 100 | // |
|---|
| 101 | // Function |
|---|
| 102 | // Name: PartialReadStream::Write(const void *, int) |
|---|
| 103 | // Purpose: As interface. But will exception. |
|---|
| 104 | // Created: 2003/08/26 |
|---|
| 105 | // |
|---|
| 106 | // -------------------------------------------------------------------------- |
|---|
| 107 | void PartialReadStream::Write(const void *pBuffer, int NBytes) |
|---|
| 108 | { |
|---|
| 109 | THROW_EXCEPTION(CommonException, CantWriteToPartialReadStream) |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | // -------------------------------------------------------------------------- |
|---|
| 113 | // |
|---|
| 114 | // Function |
|---|
| 115 | // Name: PartialReadStream::StreamDataLeft() |
|---|
| 116 | // Purpose: As interface. |
|---|
| 117 | // Created: 2003/08/26 |
|---|
| 118 | // |
|---|
| 119 | // -------------------------------------------------------------------------- |
|---|
| 120 | bool PartialReadStream::StreamDataLeft() |
|---|
| 121 | { |
|---|
| 122 | return mBytesLeft != 0; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | // -------------------------------------------------------------------------- |
|---|
| 126 | // |
|---|
| 127 | // Function |
|---|
| 128 | // Name: PartialReadStream::StreamClosed() |
|---|
| 129 | // Purpose: As interface. |
|---|
| 130 | // Created: 2003/08/26 |
|---|
| 131 | // |
|---|
| 132 | // -------------------------------------------------------------------------- |
|---|
| 133 | bool PartialReadStream::StreamClosed() |
|---|
| 134 | { |
|---|
| 135 | // always closed |
|---|
| 136 | return true; |
|---|
| 137 | } |
|---|
| 138 | |
|---|