root/trunk/whisperlib/common/io/buffer/io_memory_stream.h

Revision 7, 5.3 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Cosmin Tudorache
31
32 #ifndef __COMMON_IO_BUFFER_IO_MEMORY_STREAM_H__
33 #define __COMMON_IO_BUFFER_IO_MEMORY_STREAM_H__
34
35 #include <string>
36 #include <whisperlib/common/io/buffer/memory_stream.h>
37 #include <whisperlib/common/io/input_stream.h>
38 #include <whisperlib/common/io/output_stream.h>
39
40 //
41 // This exposes the MemoryStream using the InputStream and OutputStream
42 // interfaces..
43 //
44
45 namespace io {
46
47 class IOMemoryStream : public InputStream, public OutputStream {
48  public:
49   IOMemoryStream(common::ByteOrder byte_order = common::kByteOrder,
50                  BlockSize block_size = io::DataBlock::kDefaultBufferSize);
51   explicit IOMemoryStream(io::MemoryStream* ms_to_wrap);
52   ~IOMemoryStream();
53
54   inline MemoryStream& ms() { return *ms_; }
55   inline const MemoryStream& ms() const { return *ms_; }
56
57   // Returns the total amount of data bytes in the buffer.
58   int32 Size() const {
59     return ms_->Size();
60   }
61
62   // Tests if the stream is empty.
63   bool IsEmpty() const {
64     return ms_->IsEmpty();
65   }
66
67   // Remove all data in stream.
68   //  After this call the stream becomes empty:
69   //     Readable() == 0, IsEos() == true.
70   void Clear() {
71     ms_->Clear();
72   }
73
74   // Copy all readable data from 'in' (between crtReadHead .. streamEnd)
75   // to this stream. The 'in' stream is not modified.
76   // This stream will contain:
77   //   <previous data> <data copied from in>
78   //                  ^                     ^
79   //              read head            write head
80   // returns: the number of bytes copied.
81   int32 Append(const IOMemoryStream& in, int32 size = kMaxInt32) {
82     #ifdef _DEBUG
83     const int32 original_size = in.Size();
84     #endif
85     ms_->AppendStreamNonDestructive(in.ms_, size);
86     #ifdef _DEBUG
87     CHECK_EQ(original_size, in.Size());   // "in" should not be modified
88     #endif
89     return size;
90   }
91   // Same as Append
92   const IOMemoryStream& operator=(const IOMemoryStream& in) {
93     Append(in);
94     return *this;
95   }
96
97   // Test if both streams contain the same data
98   // TODO(cpopescu): take this out - is an ugly peach of crap
99   bool operator==(const io::IOMemoryStream& in) const;
100
101   // Returns a complete description of the internal state including
102   // internal data. Used for logging.
103   //
104   // !! NOTE !!
105   //   -- This is amazingly expensive - use it only in debug mode !!
106   string ToString() const;
107
108   //////////////////////////////////////////////////////////////////////
109   //
110   //              InputStream interface methods
111   //
112   int32 Read(void* buffer, int32 len) {
113     return ms_->Read(buffer, len);
114   }
115   int32 Peek(void* buffer, int32 len) {
116     return ms_->Peek(buffer, len);
117   }
118   int64 Skip(int64 len) {
119     return ms_->Skip(len);
120   }
121   int64 Readable() const {
122     return Size();
123   }
124   bool IsEos() const {
125     return IsEmpty();
126   }
127
128   // Sets the read marker in the current stream possition.
129   void MarkerSet() {
130     ms_->MarkerSet();
131   }
132
133   // Restores the read position of the associated stream to the position
134   // previously marked by set.
135   void MarkerRestore() {
136     ms_->MarkerRestore();
137   }
138
139   // Removes the last read mark for the underlying stream
140   void MarkerClear() {
141     ms_->MarkerClear();
142   }
143
144   //////////////////////////////////////////////////////////////////////
145   //
146   //              OutputStream interface methods
147   //
148   int32 Write(const void* buffer, int32 len) {
149     return ms_->Write(buffer, len);
150   }
151   int32 Write(io::InputStream& in, int32 len = kMaxInt32) {
152     return io::OutputStream::Write(in, len);
153   }
154   int32 Write(const char* text) {
155     return ms_->Write(text);
156   }
157   int64 Writable() const {
158     return kMaxInt64;
159   }
160
161  protected:
162   io::MemoryStream* ms_;
163   bool localms_;
164 };
165
166 inline ostream& operator<<(ostream& os,
167                            const io::IOMemoryStream& ms) {
168   return os << ms.ToString();
169 }
170 }
171 #endif  // __COMMON_IO_BUFFER_IO_MEMORY_STREAM_H__
Note: See TracBrowser for help on using the browser.