PageRenderTime 52ms CodeModel.GetById 30ms app.highlight 6ms RepoModel.GetById 14ms app.codeStats 0ms

/mordor/streams/buffered.h

http://github.com/mozy/mordor
C Header | 65 lines | 40 code | 13 blank | 12 comment | 2 complexity | 30bf23d4deb6a5d03c8385a5aaa916f5 MD5 | raw file
 1#ifndef __MORDOR_BUFFERED_STREAM_H__
 2#define __MORDOR_BUFFERED_STREAM_H__
 3// Copyright (c) 2009 - Mozy, Inc.
 4
 5#include "buffer.h"
 6#include "filter.h"
 7
 8namespace Mordor {
 9
10/* NOTE:
11 *  In current implementation, BufferedStream inherits parent stream's
12 *  read-write thread-safe ability only when parent stream is seekless.
13 * Problem:
14 *  When parent stream is seekable, read() operation will operate both
15 *  m_readBuffer and m_writeBuffer, and so does write(). Read-write
16 *  thread-safe ability can't be held anymore inside BufferedStream itself.
17 * TODO:
18 *  enhance the implementaton to allow BufferedStream inherits parent stream's
19 *  read-write thread-safe ability also for seekable stream.
20 */
21class BufferedStream : public FilterStream
22{
23public:
24    typedef boost::shared_ptr<BufferedStream> ptr;
25
26    BufferedStream(Stream::ptr parent, bool own = true);
27
28    size_t bufferSize() { return m_bufferSize; }
29    void bufferSize(size_t bufferSize) { m_bufferSize = bufferSize; }
30
31    bool allowPartialReads() { return m_allowPartialReads; }
32    void allowPartialReads(bool allowPartialReads) { m_allowPartialReads = allowPartialReads; }
33
34    bool flushMultiplesOfBuffer() { return m_flushMultiplesOfBuffer; }
35    void flushMultiplesOfBuffer(bool flushMultiplesOfBuffer ) { m_flushMultiplesOfBuffer = flushMultiplesOfBuffer; }
36
37    bool supportsFind() { return supportsRead(); }
38    bool supportsUnread() { return supportsRead() && (!supportsWrite() || !supportsSeek()); }
39
40    void close(CloseType type = BOTH);
41    size_t read(Buffer &buffer, size_t length);
42    size_t read(void *buffer, size_t length);
43    size_t write(const Buffer &buffer, size_t length);
44    size_t write(const void *buffer, size_t length);
45    long long seek(long long offset, Anchor anchor = BEGIN);
46    long long size();
47    void truncate(long long size);
48    void flush(bool flushParent = true);
49    ptrdiff_t find(char delim, size_t sanitySize = ~0, bool throwIfNotFound = true);
50    ptrdiff_t find(const std::string &str, size_t sanitySize = ~0, bool throwIfNotFound = true);
51    void unread(const Buffer &b, size_t len = ~0);
52
53private:
54    template <class T> size_t readInternal(T &buffer, size_t length);
55    size_t flushWrite(size_t length);
56
57private:
58    size_t m_bufferSize;
59    bool m_allowPartialReads, m_flushMultiplesOfBuffer;
60    Buffer m_readBuffer, m_writeBuffer;
61};
62
63}
64
65#endif