ByteBufferDataIO.h

00001 /* This file is Copyright 2000-2008 Meyer Sound Laboratories Inc.  See the included LICENSE.txt file for details. */
00002 
00003 #ifndef MuscleByteBufferDataIO_h
00004 #define MuscleByteBufferDataIO_h
00005 
00006 #include "dataio/DataIO.h"
00007 #include "util/ByteBuffer.h"
00008 
00009 BEGIN_NAMESPACE(muscle);
00010 
00016 class ByteBufferDataIO : public DataIO
00017 {
00018 public:
00023    ByteBufferDataIO(const ByteBufferRef & buf = ByteBufferRef()) : _buf(buf), _seekPos(0) {/* empty */}
00024 
00026    virtual ~ByteBufferDataIO() {/* empty */}
00027 
00031    void SetBuffer(const ByteBufferRef & buf) {_buf = buf;}
00032 
00034    const ByteBufferRef & GetBuffer() const {return _buf;}
00035 
00042    virtual int32 Read(void * buffer, uint32 size)  
00043    {
00044       if (_buf())
00045       {
00046          int32 copyBytes = muscleMin((int32)size, muscleMax((int32)0, (int32)(_buf()->GetNumBytes()-_seekPos)));
00047          memcpy(buffer, _buf()->GetBuffer()+_seekPos, copyBytes);
00048          _seekPos += copyBytes;
00049          return copyBytes;
00050       }
00051       return -1;
00052    }
00053 
00060    virtual int32 Write(const void * buffer, uint32 size) 
00061    {
00062       if (_buf() == NULL) return -1;
00063 
00064       uint32 oldBufSize = _buf()->GetNumBytes();
00065       uint32 pastOffset = muscleMax(oldBufSize, _seekPos+size);
00066       if (pastOffset > oldBufSize)
00067       {
00068          uint32 preallocBytes = (pastOffset*2);  // exponential resize to avoid too many reallocs
00069          if (_buf()->SetNumBytes(preallocBytes, true) != B_NO_ERROR) return -1;   // allocate the memory
00070          memset(_buf()->GetBuffer()+oldBufSize, 0, preallocBytes-oldBufSize);  // make sure newly alloc'd memory is zeroed out!
00071          (void) _buf()->SetNumBytes(pastOffset, true);  // guaranteed not to fail
00072       }
00073       
00074       memcpy(_buf()->GetBuffer()+_seekPos, buffer, size);
00075       _seekPos += size;
00076       return size;
00077    }
00078 
00085    virtual status_t Seek(int64 offset, int whence)
00086    {
00087       uint32 fileLen = _buf() ? _buf()->GetNumBytes() : 0;
00088       int32 o = (int32) offset;
00089       int32 newSeekPos = -1;
00090       switch(whence)
00091       {
00092          case IO_SEEK_SET:  newSeekPos = o;          break;
00093          case IO_SEEK_CUR:  newSeekPos = _seekPos+o; break;
00094          case IO_SEEK_END:  newSeekPos = fileLen-o;  break;
00095          default:           return B_ERROR;
00096       }
00097       if (newSeekPos < 0) return B_ERROR;
00098       _seekPos = newSeekPos;
00099       return B_NO_ERROR;
00100    }
00101    
00102    virtual int64 GetPosition() const {return _seekPos;}
00103 
00108    virtual void FlushOutput() {/* empty */}
00109 
00111    virtual void Shutdown() {_buf.Reset();}
00112 
00114    virtual const SocketRef & GetSelectSocket() const {return GetNullSocket();}
00115 
00116 private:
00117    ByteBufferRef _buf;
00118    int32 _seekPos;
00119 };
00120 
00121 END_NAMESPACE(muscle);
00122 
00123 #endif

Generated on Thu Jun 5 17:47:52 2008 for MUSCLE by  doxygen 1.5.1