Changeset 1585


Ignore:
Timestamp:
28/04/2007 18:11:36 (5 years ago)
Author:
chris
Message:

Add a stream which provides a source of zero bytes of arbitrary size,
useful for testing support for files over 2GB. (refs #3)

Location:
box/chris/merge/lib/common
Files:
2 copied

Legend:

Unmodified
Added
Removed
  • box/chris/merge/lib/common/ZeroStream.cpp

    r1263 r1585  
    22// 
    33// File 
    4 //              Name:    BufferedStream.cpp 
    5 //              Purpose: Buffering wrapper around IOStreams 
    6 //              Created: 2007/01/16 
     4//              Name:    ZeroStream.cpp 
     5//              Purpose: An IOStream which returns all zeroes up to a certain size 
     6//              Created: 2007/04/28 
    77// 
    88// -------------------------------------------------------------------------- 
    99 
    1010#include "Box.h" 
    11 #include "BufferedStream.h" 
     11#include "ZeroStream.h" 
    1212#include "CommonException.h" 
    1313 
     
    1919// 
    2020// Function 
    21 //              Name:    BufferedStream::BufferedStream(const char *, int, int) 
    22 //              Purpose: Constructor, set up buffer 
    23 //              Created: 2007/01/16 
     21//              Name:    ZeroStream::ZeroStream(IOStream::pos_type) 
     22//              Purpose: Constructor 
     23//              Created: 2007/04/28 
    2424// 
    2525// -------------------------------------------------------------------------- 
    26 BufferedStream::BufferedStream(IOStream& rSource) 
    27 : mrSource(rSource), mBufferSize(0), mBufferPosition(0) 
     26ZeroStream::ZeroStream(IOStream::pos_type size) 
     27: mSize(size), mPosition(0) 
    2828{ } 
    2929 
     
    3232// 
    3333// Function 
    34 //              Name:    BufferedStream::Read(void *, int) 
     34//              Name:    ZeroStream::Read(void *, int) 
    3535//              Purpose: Reads bytes from the file 
    3636//              Created: 2007/01/16 
    3737// 
    3838// -------------------------------------------------------------------------- 
    39 int BufferedStream::Read(void *pBuffer, int NBytes, int Timeout) 
     39int ZeroStream::Read(void *pBuffer, int NBytes, int Timeout) 
    4040{ 
    41         if (mBufferSize == mBufferPosition) 
     41        ASSERT(NBytes > 0); 
     42 
     43        int bytesToRead = NBytes; 
     44         
     45        if (bytesToRead > mSize - mPosition) 
    4246        { 
    43                 // buffer is empty, fill it. 
    44  
    45                 int numBytesRead = mrSource.Read(mBuffer, sizeof(mBuffer),  
    46                         Timeout); 
    47  
    48                 if (numBytesRead < 0) 
    49                 { 
    50                         return numBytesRead; 
    51                 } 
    52  
    53                 mBufferSize = numBytesRead; 
     47                bytesToRead = mSize - mPosition; 
    5448        } 
    5549 
    56         int sizeToReturn = mBufferSize - mBufferPosition; 
     50        memset(pBuffer, 0, bytesToRead); 
     51        mPosition += bytesToRead; 
    5752 
    58         if (sizeToReturn > NBytes) 
    59         { 
    60                 sizeToReturn = NBytes; 
    61         } 
    62  
    63         memcpy(pBuffer, mBuffer + mBufferPosition, sizeToReturn); 
    64         mBufferPosition += sizeToReturn; 
    65  
    66         if (mBufferPosition == mBufferSize) 
    67         { 
    68                 // clear out the buffer 
    69                 mBufferSize = 0; 
    70                 mBufferPosition = 0; 
    71         } 
    72  
    73         return sizeToReturn; 
     53        return bytesToRead; 
    7454} 
    7555 
     
    7858// 
    7959// Function 
    80 //              Name:    BufferedStream::BytesLeftToRead() 
     60//              Name:    ZeroStream::BytesLeftToRead() 
    8161//              Purpose: Returns number of bytes to read (may not be most efficient function ever) 
    8262//              Created: 2007/01/16 
    8363// 
    8464// -------------------------------------------------------------------------- 
    85 IOStream::pos_type BufferedStream::BytesLeftToRead() 
     65IOStream::pos_type ZeroStream::BytesLeftToRead() 
    8666{ 
    87         return mrSource.BytesLeftToRead() + mBufferSize - mBufferPosition; 
     67        return mSize - mPosition; 
    8868} 
    8969 
     
    9272// 
    9373// Function 
    94 //              Name:    BufferedStream::Write(void *, int) 
     74//              Name:    ZeroStream::Write(void *, int) 
    9575//              Purpose: Writes bytes to the underlying stream (not supported) 
    9676//              Created: 2003/07/31 
    9777// 
    9878// -------------------------------------------------------------------------- 
    99 void BufferedStream::Write(const void *pBuffer, int NBytes) 
     79void ZeroStream::Write(const void *pBuffer, int NBytes) 
    10080{ 
    10181        THROW_EXCEPTION(CommonException, NotSupported); 
     
    10686// 
    10787// Function 
    108 //              Name:    BufferedStream::GetPosition() 
     88//              Name:    ZeroStream::GetPosition() 
    10989//              Purpose: Get position in stream 
    11090//              Created: 2003/08/21 
    11191// 
    11292// -------------------------------------------------------------------------- 
    113 IOStream::pos_type BufferedStream::GetPosition() const 
     93IOStream::pos_type ZeroStream::GetPosition() const 
    11494{ 
    115         return mrSource.GetPosition() - mBufferSize + mBufferPosition; 
     95        return mPosition; 
    11696} 
    11797 
     
    120100// 
    121101// Function 
    122 //              Name:    BufferedStream::Seek(pos_type, int) 
     102//              Name:    ZeroStream::Seek(pos_type, int) 
    123103//              Purpose: Seeks within file, as lseek, invalidate buffer 
    124104//              Created: 2003/07/31 
    125105// 
    126106// -------------------------------------------------------------------------- 
    127 void BufferedStream::Seek(IOStream::pos_type Offset, int SeekType) 
     107void ZeroStream::Seek(IOStream::pos_type Offset, int SeekType) 
    128108{ 
    129109        switch (SeekType) 
     
    131111                case SeekType_Absolute: 
    132112                { 
    133                         // just go there 
    134                         mrSource.Seek(Offset, SeekType); 
     113                        mPosition = Offset; 
    135114                } 
    136115                break; 
     
    138117                case SeekType_Relative: 
    139118                { 
    140                         // Actual underlying file position is  
    141                         // (mBufferSize - mBufferPosition) ahead of us. 
    142                         // Need to subtract that amount from the seek 
    143                         // to seek forward that much less, putting the  
    144                         // real pointer in the right place. 
    145                         mrSource.Seek(Offset - mBufferSize + mBufferPosition,  
    146                                 SeekType); 
     119                        mPosition += Offset; 
    147120                } 
    148121                break; 
     
    150123                case SeekType_End: 
    151124                { 
    152                         // Actual underlying file position is  
    153                         // (mBufferSize - mBufferPosition) ahead of us. 
    154                         // Need to add that amount to the seek 
    155                         // to seek backwards that much more, putting the  
    156                         // real pointer in the right place. 
    157                         mrSource.Seek(Offset + mBufferSize - mBufferPosition,  
    158                                 SeekType); 
     125                        mPosition = mSize - Offset; 
    159126                } 
    160127        } 
    161  
    162         // always clear the buffer for now (may be slightly wasteful) 
    163         mBufferSize = 0; 
    164         mBufferPosition = 0; 
    165128} 
    166129 
     
    169132// 
    170133// Function 
    171 //              Name:    BufferedStream::Close() 
     134//              Name:    ZeroStream::Close() 
    172135//              Purpose: Closes the underlying stream (not needed) 
    173136//              Created: 2003/07/31 
    174137// 
    175138// -------------------------------------------------------------------------- 
    176 void BufferedStream::Close() 
     139void ZeroStream::Close() 
    177140{ 
    178141        THROW_EXCEPTION(CommonException, NotSupported); 
     
    183146// 
    184147// Function 
    185 //              Name:    BufferedStream::StreamDataLeft() 
     148//              Name:    ZeroStream::StreamDataLeft() 
    186149//              Purpose: Any data left to write? 
    187150//              Created: 2003/08/02 
    188151// 
    189152// -------------------------------------------------------------------------- 
    190 bool BufferedStream::StreamDataLeft() 
     153bool ZeroStream::StreamDataLeft() 
    191154{ 
    192         return mrSource.StreamDataLeft(); 
     155        return false; 
    193156} 
    194157 
     
    196159// 
    197160// Function 
    198 //              Name:    BufferedStream::StreamClosed() 
     161//              Name:    ZeroStream::StreamClosed() 
    199162//              Purpose: Is the stream closed? 
    200163//              Created: 2003/08/02 
    201164// 
    202165// -------------------------------------------------------------------------- 
    203 bool BufferedStream::StreamClosed() 
     166bool ZeroStream::StreamClosed() 
    204167{ 
    205         return mrSource.StreamClosed(); 
     168        return false; 
    206169} 
    207170 
  • box/chris/merge/lib/common/ZeroStream.h

    r1263 r1585  
    22// 
    33// File 
    4 //              Name:    BufferedStream.h 
    5 //              Purpose: Buffering wrapper around IOStreams 
    6 //              Created: 2007/01/16 
     4//              Name:    ZeroStream.h 
     5//              Purpose: An IOStream which returns all zeroes up to a certain size 
     6//              Created: 2007/04/28 
    77// 
    88// -------------------------------------------------------------------------- 
    99 
    10 #ifndef BUFFEREDSTREAM__H 
    11 #define BUFFEREDSTREAM__H 
     10#ifndef ZEROSTREAM__H 
     11#define ZEROSTREAM__H 
    1212 
    1313#include "IOStream.h" 
    1414 
    15 class BufferedStream : public IOStream 
     15class ZeroStream : public IOStream 
    1616{ 
    1717private: 
    18         IOStream& mrSource; 
    19         char mBuffer[4096]; 
    20         int  mBufferSize; 
    21         int  mBufferPosition; 
     18        IOStream::pos_type mSize, mPosition; 
    2219 
    2320public: 
    24         BufferedStream(IOStream& rSource); 
     21        ZeroStream(IOStream::pos_type mSize); 
    2522         
    2623        virtual int Read(void *pBuffer, int NBytes, int Timeout = IOStream::TimeOutInfinite); 
     
    3532 
    3633private: 
    37         BufferedStream(const BufferedStream &rToCopy)  
    38         : mrSource(rToCopy.mrSource) { /* do not call */ } 
     34        ZeroStream(const ZeroStream &rToCopy); 
    3935}; 
    4036 
    41 #endif // BUFFEREDSTREAM__H 
     37#endif // ZEROSTREAM__H 
    4238 
    4339 
Note: See TracChangeset for help on using the changeset viewer.