[Box Backup-commit] COMMIT r1585 - box/chris/merge/lib/common

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Sat Apr 28 18:11:36 BST 2007


Author: chris
Date: 2007-04-28 18:11:36 +0100 (Sat, 28 Apr 2007)
New Revision: 1585

Added:
   box/chris/merge/lib/common/ZeroStream.cpp
   box/chris/merge/lib/common/ZeroStream.h
Log:
Add a stream which provides a source of zero bytes of arbitrary size,
useful for testing support for files over 2GB. (refs #3)


Copied: box/chris/merge/lib/common/ZeroStream.cpp (from rev 1571, box/chris/merge/lib/common/BufferedStream.cpp)
===================================================================
--- box/chris/merge/lib/common/ZeroStream.cpp	                        (rev 0)
+++ box/chris/merge/lib/common/ZeroStream.cpp	2007-04-28 17:11:36 UTC (rev 1585)
@@ -0,0 +1,170 @@
+// --------------------------------------------------------------------------
+//
+// File
+//		Name:    ZeroStream.cpp
+//		Purpose: An IOStream which returns all zeroes up to a certain size
+//		Created: 2007/04/28
+//
+// --------------------------------------------------------------------------
+
+#include "Box.h"
+#include "ZeroStream.h"
+#include "CommonException.h"
+
+#include <string.h>
+
+#include "MemLeakFindOn.h"
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    ZeroStream::ZeroStream(IOStream::pos_type)
+//		Purpose: Constructor
+//		Created: 2007/04/28
+//
+// --------------------------------------------------------------------------
+ZeroStream::ZeroStream(IOStream::pos_type size)
+: mSize(size), mPosition(0)
+{ }
+
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    ZeroStream::Read(void *, int)
+//		Purpose: Reads bytes from the file
+//		Created: 2007/01/16
+//
+// --------------------------------------------------------------------------
+int ZeroStream::Read(void *pBuffer, int NBytes, int Timeout)
+{
+	ASSERT(NBytes > 0);
+
+	int bytesToRead = NBytes;
+	
+	if (bytesToRead > mSize - mPosition)
+	{
+		bytesToRead = mSize - mPosition;
+	}
+
+	memset(pBuffer, 0, bytesToRead);
+	mPosition += bytesToRead;
+
+	return bytesToRead;
+}
+
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    ZeroStream::BytesLeftToRead()
+//		Purpose: Returns number of bytes to read (may not be most efficient function ever)
+//		Created: 2007/01/16
+//
+// --------------------------------------------------------------------------
+IOStream::pos_type ZeroStream::BytesLeftToRead()
+{
+	return mSize - mPosition;
+}
+
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    ZeroStream::Write(void *, int)
+//		Purpose: Writes bytes to the underlying stream (not supported)
+//		Created: 2003/07/31
+//
+// --------------------------------------------------------------------------
+void ZeroStream::Write(const void *pBuffer, int NBytes)
+{
+	THROW_EXCEPTION(CommonException, NotSupported);
+}
+
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    ZeroStream::GetPosition()
+//		Purpose: Get position in stream
+//		Created: 2003/08/21
+//
+// --------------------------------------------------------------------------
+IOStream::pos_type ZeroStream::GetPosition() const
+{
+	return mPosition;
+}
+
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    ZeroStream::Seek(pos_type, int)
+//		Purpose: Seeks within file, as lseek, invalidate buffer
+//		Created: 2003/07/31
+//
+// --------------------------------------------------------------------------
+void ZeroStream::Seek(IOStream::pos_type Offset, int SeekType)
+{
+	switch (SeekType)
+	{
+		case SeekType_Absolute:
+		{
+			mPosition = Offset;
+		}
+		break;
+
+		case SeekType_Relative:
+		{
+			mPosition += Offset;
+		}
+		break;
+
+		case SeekType_End:
+		{
+			mPosition = mSize - Offset;
+		}
+	}
+}
+
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    ZeroStream::Close()
+//		Purpose: Closes the underlying stream (not needed)
+//		Created: 2003/07/31
+//
+// --------------------------------------------------------------------------
+void ZeroStream::Close()
+{
+	THROW_EXCEPTION(CommonException, NotSupported);
+}
+
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    ZeroStream::StreamDataLeft()
+//		Purpose: Any data left to write?
+//		Created: 2003/08/02
+//
+// --------------------------------------------------------------------------
+bool ZeroStream::StreamDataLeft()
+{
+	return false;
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    ZeroStream::StreamClosed()
+//		Purpose: Is the stream closed?
+//		Created: 2003/08/02
+//
+// --------------------------------------------------------------------------
+bool ZeroStream::StreamClosed()
+{
+	return false;
+}
+

Copied: box/chris/merge/lib/common/ZeroStream.h (from rev 1571, box/chris/merge/lib/common/BufferedStream.h)
===================================================================
--- box/chris/merge/lib/common/ZeroStream.h	                        (rev 0)
+++ box/chris/merge/lib/common/ZeroStream.h	2007-04-28 17:11:36 UTC (rev 1585)
@@ -0,0 +1,39 @@
+// --------------------------------------------------------------------------
+//
+// File
+//		Name:    ZeroStream.h
+//		Purpose: An IOStream which returns all zeroes up to a certain size
+//		Created: 2007/04/28
+//
+// --------------------------------------------------------------------------
+
+#ifndef ZEROSTREAM__H
+#define ZEROSTREAM__H
+
+#include "IOStream.h"
+
+class ZeroStream : public IOStream
+{
+private:
+	IOStream::pos_type mSize, mPosition;
+
+public:
+	ZeroStream(IOStream::pos_type mSize);
+	
+	virtual int Read(void *pBuffer, int NBytes, int Timeout = IOStream::TimeOutInfinite);
+	virtual pos_type BytesLeftToRead();
+	virtual void Write(const void *pBuffer, int NBytes);
+	virtual pos_type GetPosition() const;
+	virtual void Seek(IOStream::pos_type Offset, int SeekType);
+	virtual void Close();
+	
+	virtual bool StreamDataLeft();
+	virtual bool StreamClosed();
+
+private:
+	ZeroStream(const ZeroStream &rToCopy);
+};
+
+#endif // ZEROSTREAM__H
+
+




More information about the Boxbackup-commit mailing list