[Box Backup-dev] COMMIT r532 - in box/chris/general: lib/common test/common

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Tue Feb 28 00:06:23 GMT 2006


Author: chris
Date: 2006-02-28 00:05:51 +0000 (Tue, 28 Feb 2006)
New Revision: 532

Added:
   box/chris/general/lib/common/MemBufferStream.cpp
   box/chris/general/lib/common/MemBufferStream.h
Modified:
   box/chris/general/test/common/testcommon.cpp
Log:
* test/common/testcommon.cpp
* lib/common/MemBufferStream.cpp
* lib/common/MemBufferStream.h
- Added a read/write memory buffer and test cases for it


Added: box/chris/general/lib/common/MemBufferStream.cpp
===================================================================
--- box/chris/general/lib/common/MemBufferStream.cpp	2006-02-27 00:21:57 UTC (rev 531)
+++ box/chris/general/lib/common/MemBufferStream.cpp	2006-02-28 00:05:51 UTC (rev 532)
@@ -0,0 +1,193 @@
+// --------------------------------------------------------------------------
+//
+// File
+//		Name:    MemBufferStream.cpp
+//		Purpose: Stream to and from an encapsulated memory block
+//		Created: 2006/02/27
+//
+// --------------------------------------------------------------------------
+
+#include "Box.h"
+#include "MemBufferStream.h"
+#include "CommonException.h"
+#include "Guards.h"
+
+#include "MemLeakFindOn.h"
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    MemBufferStream::MemBufferStream()
+//		Purpose: Constructor
+//		Created: 2006/02/27
+//
+// --------------------------------------------------------------------------
+MemBufferStream::MemBufferStream()
+: mReadPosition(0),
+  mWritePosition(0)
+{
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    MemBufferStream::MemBufferStream(const MemBufferStream &)
+//		Purpose: Copy constructor (exceptions)
+//		Created: 2003/07/31
+//
+// --------------------------------------------------------------------------
+MemBufferStream::MemBufferStream(const MemBufferStream &rToCopy)
+{
+	THROW_EXCEPTION(CommonException, NotSupported)
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    MemBufferStream::MemBufferStream(const StreamableMemBlock &)
+//		Purpose: Copy an existing StreamableMemBlock
+//		Created: 2006/02/27
+//
+// --------------------------------------------------------------------------
+MemBufferStream::MemBufferStream(const StreamableMemBlock &rSource)
+: mBuffer(rSource),
+  mReadPosition(0),
+  mWritePosition(rSource.GetSize())
+{
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    MemBufferStream::~MemBufferStream()
+//		Purpose: Destructor
+//		Created: 2003/07/31
+//
+// --------------------------------------------------------------------------
+MemBufferStream::~MemBufferStream()
+{
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    MemBufferStream::Close()
+//		Purpose: Close the stream
+//		Created: 2003/07/31
+//
+// --------------------------------------------------------------------------
+void MemBufferStream::Close()
+{
+	// Do nothing by default -- let the destructor clear everything up.
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    MemBufferStream::GetPosition()
+//		Purpose: Returns current position in stream 
+//			(just after the last byte read)
+//		Created: 2003/08/21
+//
+// --------------------------------------------------------------------------
+MemBufferStream::pos_type MemBufferStream::GetPosition() const
+{
+	return mReadPosition;
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    MemBufferStream::WriteAllBuffered()
+//		Purpose: Ensures that any data which has been buffered is written to the stream
+//		Created: 2003/08/26
+//
+// --------------------------------------------------------------------------
+void MemBufferStream::WriteAllBuffered()
+{
+	// These aren't the buffers you're looking for.
+	// Nothing to see here, move along.
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    MemBufferStream::BytesLeftToRead()
+//		Purpose: Numbers of bytes left to read in the stream, or
+//				 MemBufferStream::SizeOfStreamUnknown if this isn't known.
+//		Created: 2003/08/26
+//
+// --------------------------------------------------------------------------
+MemBufferStream::pos_type MemBufferStream::BytesLeftToRead()
+{
+	return mWritePosition - mReadPosition;
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    MemBufferStream::Read(void *pBuffer, int NBytes, 
+//			int Timeout)
+//		Purpose: Read some bytes from the buffer, up to the maximum
+//			number available to read.
+//		Created: 2006/02/27
+//
+// --------------------------------------------------------------------------
+int MemBufferStream::Read(void *pBuffer, int NBytes, int Timeout)
+{
+	if (NBytes > BytesLeftToRead())
+	{
+		NBytes = BytesLeftToRead();
+	}
+
+	uint8_t* source = (uint8_t *)( mBuffer.GetBuffer() );
+	memcpy(pBuffer, source + mReadPosition, NBytes);
+	mReadPosition += NBytes;
+
+	return NBytes;
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    MemBufferStream::Write(const void *pBuffer, 
+//			int NBytes)
+//		Purpose: Write some bytes to the buffer, resizing it if
+//			necessary, increasing the number available to read.
+//		Created: 2006/02/27
+//
+// --------------------------------------------------------------------------
+void MemBufferStream::Write(const void *pBuffer, int NBytes)
+{
+	mBuffer.ResizeBlock(mWritePosition + NBytes);
+	uint8_t* dest = (uint8_t *)( mBuffer.GetBuffer() );
+	memcpy(dest + mWritePosition, pBuffer, NBytes);
+	mWritePosition += NBytes;
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    MemBufferStream::StreamDataLeft()
+//		Purpose: Tell whether any bytes are still available to read.
+//		Created: 2006/02/27
+//
+// --------------------------------------------------------------------------
+bool MemBufferStream::StreamDataLeft()
+{
+	return BytesLeftToRead() > 0;
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    MemBufferStream::StreamClosed()
+//		Purpose: Tell whether the stream is closed (no effect)
+//		Created: 2006/02/27
+//
+// --------------------------------------------------------------------------
+bool MemBufferStream::StreamClosed()
+{
+	return false;
+}
+

Added: box/chris/general/lib/common/MemBufferStream.h
===================================================================
--- box/chris/general/lib/common/MemBufferStream.h	2006-02-27 00:21:57 UTC (rev 531)
+++ box/chris/general/lib/common/MemBufferStream.h	2006-02-28 00:05:51 UTC (rev 532)
@@ -0,0 +1,62 @@
+// --------------------------------------------------------------------------
+//
+// File
+//		Name:    MemBufferStream.h
+//		Purpose: Stream to and from an encapsulated memory block
+//		Created: 2006/02/27
+//
+// --------------------------------------------------------------------------
+
+#ifndef MEMBUFFERSTREAM__H
+#define MEMBUFFERSTREAM__H
+
+#include "IOStream.h"
+#include "StreamableMemBlock.h"
+
+// --------------------------------------------------------------------------
+//
+// Class
+//		Name:    MemBufferStream
+//		Purpose: Stream to and from an encapsulated memory block
+//		Created: 2006/02/27
+//
+// --------------------------------------------------------------------------
+class MemBufferStream : public IOStream
+{
+public:
+	MemBufferStream();
+	MemBufferStream(const StreamableMemBlock& rSource);
+
+private:
+	MemBufferStream(const MemBufferStream &rToCopy); // do not call
+
+public:
+	virtual ~MemBufferStream();
+	
+	// Timeout in milliseconds
+	// Read may return 0 -- does not mean end of stream.
+	typedef int64_t pos_type;
+	virtual int Read(void *pBuffer, int NBytes, int Timeout = IOStream::TimeOutInfinite);
+	virtual pos_type BytesLeftToRead();	// may return IOStream::SizeOfStreamUnknown (and will for most stream types)
+	virtual void Write(const void *pBuffer, int NBytes);
+	virtual void WriteAllBuffered();
+	virtual pos_type GetPosition() const;
+	virtual void Close();
+	
+	// Has all data that can be read been read?
+	virtual bool StreamDataLeft();
+	// Has the stream been closed (writing not possible)
+	virtual bool StreamClosed();
+	
+	const StreamableMemBlock& GetBuffer() { return mBuffer; }
+	
+private:
+	StreamableMemBlock mBuffer;
+	pos_type mReadPosition;
+	pos_type mWritePosition; // always equals buffer size
+};
+
+
+#endif // MEMBUFFERSTREAM__H
+
+

Modified: box/chris/general/test/common/testcommon.cpp
===================================================================
--- box/chris/general/test/common/testcommon.cpp	2006-02-27 00:21:57 UTC (rev 531)
+++ box/chris/general/test/common/testcommon.cpp	2006-02-28 00:05:51 UTC (rev 532)
@@ -24,6 +24,7 @@
 #include "CommonException.h"
 #include "Conversion.h"
 #include "autogen_ConversionException.h"
+#include "MemBufferStream.h"
 
 #include "MemLeakFindOn.h"
 
@@ -565,5 +566,55 @@
 
 	test_conversions();
 
+	{
+		StreamableMemBlock temp;
+
+		{
+			MemBufferStream buf;
+
+			TEST_THAT(buf.GetPosition() == 0);
+			TEST_THAT(buf.BytesLeftToRead() == 0);
+
+			buf.Write("hello ", 6);
+			TEST_THAT(buf.GetPosition() == 0);
+			TEST_THAT(buf.BytesLeftToRead() == 6);
+
+			buf.Write("world!", 7);
+			TEST_THAT(buf.GetPosition() == 0);
+			TEST_THAT(buf.BytesLeftToRead() == 13);
+
+			temp = buf.GetBuffer();
+			TEST_THAT(temp.GetSize() == 13);
+		}
+
+		{
+			MemBufferStream buf(temp);
+
+			TEST_THAT(buf.GetPosition() == 0);
+			TEST_THAT(buf.BytesLeftToRead() == 13);
+			TEST_THAT(buf.StreamDataLeft());
+
+			char target[13];
+			memset(target, 0, sizeof(target));
+
+			TEST_THAT(buf.Read(target, 6, 0) == 6);
+			TEST_THAT(strcmp(target, "hello ") == 0);
+			TEST_THAT(buf.GetPosition() == 6);
+			TEST_THAT(buf.BytesLeftToRead() == 7);
+			TEST_THAT(buf.StreamDataLeft());
+
+			TEST_THAT(buf.Read(target, 12, 0) == 7);
+			TEST_THAT(strcmp(target, "world!") == 0);
+			TEST_THAT(buf.GetPosition() == 13);
+			TEST_THAT(buf.BytesLeftToRead() == 0);
+			
+			TEST_THAT(buf.Read(target, 12, 0) == 0);
+			TEST_THAT(buf.GetPosition() == 13);
+			TEST_THAT(buf.BytesLeftToRead() == 0);
+			TEST_THAT(!buf.StreamDataLeft());
+			TEST_THAT(!buf.StreamClosed());
+		}
+	}
+
 	return 0;
 }




More information about the Boxbackup-dev mailing list