source: box/trunk/lib/common/ZeroStream.cpp @ 3105

Revision 3105, 4.0 KB checked in by chris, 4 weeks ago (diff)

Fix bug that caused sending a ZeroStream? to end early, breaking protocol.

  • Property svn:eol-style set to native
Line 
1// --------------------------------------------------------------------------
2//
3// File
4//              Name:    ZeroStream.cpp
5//              Purpose: An IOStream which returns all zeroes up to a certain size
6//              Created: 2007/04/28
7//
8// --------------------------------------------------------------------------
9
10#include "Box.h"
11#include "ZeroStream.h"
12#include "CommonException.h"
13
14#include <string.h>
15
16#include "MemLeakFindOn.h"
17
18// --------------------------------------------------------------------------
19//
20// Function
21//              Name:    ZeroStream::ZeroStream(IOStream::pos_type)
22//              Purpose: Constructor
23//              Created: 2007/04/28
24//
25// --------------------------------------------------------------------------
26ZeroStream::ZeroStream(IOStream::pos_type size)
27: mSize(size), mPosition(0)
28{ }
29
30
31// --------------------------------------------------------------------------
32//
33// Function
34//              Name:    ZeroStream::Read(void *, int)
35//              Purpose: Reads bytes from the file
36//              Created: 2007/01/16
37//
38// --------------------------------------------------------------------------
39int ZeroStream::Read(void *pBuffer, int NBytes, int Timeout)
40{
41        ASSERT(NBytes > 0);
42
43        int bytesToRead = NBytes;
44       
45        if (bytesToRead > mSize - mPosition)
46        {
47                bytesToRead = mSize - mPosition;
48        }
49
50        memset(pBuffer, 0, bytesToRead);
51        mPosition += bytesToRead;
52
53        return bytesToRead;
54}
55
56
57// --------------------------------------------------------------------------
58//
59// Function
60//              Name:    ZeroStream::BytesLeftToRead()
61//              Purpose: Returns number of bytes to read (may not be most efficient function ever)
62//              Created: 2007/01/16
63//
64// --------------------------------------------------------------------------
65IOStream::pos_type ZeroStream::BytesLeftToRead()
66{
67        return mSize - mPosition;
68}
69
70
71// --------------------------------------------------------------------------
72//
73// Function
74//              Name:    ZeroStream::Write(void *, int)
75//              Purpose: Writes bytes to the underlying stream (not supported)
76//              Created: 2003/07/31
77//
78// --------------------------------------------------------------------------
79void ZeroStream::Write(const void *pBuffer, int NBytes)
80{
81        THROW_EXCEPTION(CommonException, NotSupported);
82}
83
84
85// --------------------------------------------------------------------------
86//
87// Function
88//              Name:    ZeroStream::GetPosition()
89//              Purpose: Get position in stream
90//              Created: 2003/08/21
91//
92// --------------------------------------------------------------------------
93IOStream::pos_type ZeroStream::GetPosition() const
94{
95        return mPosition;
96}
97
98
99// --------------------------------------------------------------------------
100//
101// Function
102//              Name:    ZeroStream::Seek(pos_type, int)
103//              Purpose: Seeks within file, as lseek, invalidate buffer
104//              Created: 2003/07/31
105//
106// --------------------------------------------------------------------------
107void ZeroStream::Seek(IOStream::pos_type Offset, int SeekType)
108{
109        switch (SeekType)
110        {
111                case SeekType_Absolute:
112                {
113                        mPosition = Offset;
114                }
115                break;
116
117                case SeekType_Relative:
118                {
119                        mPosition += Offset;
120                }
121                break;
122
123                case SeekType_End:
124                {
125                        mPosition = mSize - Offset;
126                }
127        }
128}
129
130
131// --------------------------------------------------------------------------
132//
133// Function
134//              Name:    ZeroStream::Close()
135//              Purpose: Closes the underlying stream (not needed)
136//              Created: 2003/07/31
137//
138// --------------------------------------------------------------------------
139void ZeroStream::Close()
140{
141        THROW_EXCEPTION(CommonException, NotSupported);
142}
143
144
145// --------------------------------------------------------------------------
146//
147// Function
148//              Name:    ZeroStream::StreamDataLeft()
149//              Purpose: Any data left to write?
150//              Created: 2003/08/02
151//
152// --------------------------------------------------------------------------
153bool ZeroStream::StreamDataLeft()
154{
155        return (BytesLeftToRead() > 0);
156}
157
158// --------------------------------------------------------------------------
159//
160// Function
161//              Name:    ZeroStream::StreamClosed()
162//              Purpose: Is the stream closed?
163//              Created: 2003/08/02
164//
165// --------------------------------------------------------------------------
166bool ZeroStream::StreamClosed()
167{
168        return false;
169}
170
Note: See TracBrowser for help on using the repository browser.