Stream.h

// Stream.h
//
// Author David Barrett-Lennard
// (C)opyright Cedanet Pty Ltd 2004

#pragma once
#ifndef Ceda_cxUtils_Stream_H
#define Ceda_cxUtils_Stream_H

#include "cxUtils.h"
#include "xvector.h"
#include "xstring.h"
#include "IException.h"
#include "StreamInterfaces.h"

#ifdef _MSC_VER
    // non dll-interface struct 'X' used as base for dll-interface class 'Y'
    #pragma warning(disable:4275)
#endif

namespace ceda
{

class xostream;

cxUtils_API void DumpStream(xostream& os, ssize_t numPerLine, IInputStream* src);

cxUtils_API void CopyStream(xostream& dst, IInputStream& src);

// Copy input stream to output stream.  Returns number of bytes transferred.
cxUtils_API ssize_t CopyStream(IOutputStream* dst, IInputStream* src);

// Copy the given number of bytes from the src to the destination.  Returns the actual number of bytes
// copied (if met EOF on the src)
cxUtils_API ssize_t CopyStream(IOutputStream* dst, IInputStream* src, ssize_t numBytes);

cxUtils_API void CopyStreamToBuffer(ISeekableInputStream* src, xvector<octet_t>& buffer);

// Compare the two given input streams
// Returns 
//      -1   if is1 < is2
//      0    if is1 == is2
//      +1   if is1 > is2
cxUtils_API int StreamCompare(IInputStream* is1, IInputStream* is2);


///////////////////////////////////////////////////////////////////////////////////////////////////
// BufferToInputStreamAdapter

class cxUtils_API BufferToInputStreamAdapter : public ISeekableInputStream
{
    cxNotCloneable(BufferToInputStreamAdapter)

public:
    // Note that this adapter doesn't take any responsibility for lifetime management of the given
    // buffer or string.
    // The string must not be modified while the adapter is used.
    BufferToInputStreamAdapter(const void* buffer, ssize_t size);
    BufferToInputStreamAdapter(const xstring& str);

    // Implementation of IInputStream
	virtual ssize_t ReadStream(void* buffer, ssize_t numBytesRequested);

    // Implementation of ISeekableInputStream
    virtual ssize_t GetStreamLength() const { return size_; }
    virtual ssize_t GetStreamPosition() const;
    virtual void SetStreamPosition(ssize_t pos);

    void* GetBufferPtr() { return (void*) m_buffer; }
    ssize_t GetNumBytesRemaining() const { return m_numBytesRemaining; }

private:
    const octet_t* m_start;
    const octet_t* m_buffer;
    ssize_t size_;
    ssize_t m_numBytesRemaining;
};

///////////////////////////////////////////////////////////////////////////////////////////////////
// BufferOverflowException

class cxUtils_API BufferOverflowException : public IException
{
public:
    virtual void Write(xostream& os) const;
};

///////////////////////////////////////////////////////////////////////////////////////////////////
// BufferToOutputStreamAdapter

class cxUtils_API BufferToOutputStreamAdapter : public IOutputStream
{
    cxNotCloneable(BufferToOutputStreamAdapter)

public:
    BufferToOutputStreamAdapter(void* buffer, ssize_t size);

    // Implementation of IOutputStream
    virtual void WriteStream(const void* buffer, ssize_t numBytes);     // May throw BufferOverflowException
    virtual void FlushStream() {}

    void* GetBufferPtr() { return m_buffer; }
    virtual ssize_t GetNumBytesRemaining() const { return m_numBytesRemaining; }

private:
    octet_t* m_buffer;
    ssize_t m_numBytesWritten;
    ssize_t m_numBytesRemaining;
};

///////////////////////////////////////////////////////////////////////////////////////////////////
// ByteVectorToOutputStreamAdapter

class cxUtils_API ByteVectorToOutputStreamAdapter : public IOutputStream
{
    cxNotCloneable(ByteVectorToOutputStreamAdapter)

public:
    ByteVectorToOutputStreamAdapter(xvector<octet_t>& v) : m_v(v) {}

    // Implementation of IOutputStream
    virtual void WriteStream(const void* buffer, ssize_t numBytes);
    virtual void FlushStream() {}

private:    
    xvector<octet_t>& m_v;
};

///////////////////////////////////////////////////////////////////////////////////////////////////
// VectorOfByteToOutputStreamAdapter

class cxUtils_API VectorOfByteToOutputStreamAdapter : public IOutputStream
{
    cxNotCloneable(VectorOfByteToOutputStreamAdapter)

public:
    VectorOfByteToOutputStreamAdapter(VectorOfByte& v) : m_v(v) {}

    // Implementation of IOutputStream
    virtual void WriteStream(const void* buffer, ssize_t numBytes);
    virtual void FlushStream() {}

private:    
    VectorOfByte& m_v;
};

} // namespace ceda

#endif // include guard