19 #ifndef avro_Stream_hh__
20 #define avro_Stream_hh__
26 #include "boost/utility.hpp"
29 #include "Exception.hh"
56 virtual bool next(
const uint8_t** data,
size_t* len) = 0;
63 virtual void backup(
size_t len) = 0;
68 virtual void skip(
size_t len) = 0;
75 virtual size_t byteCount()
const = 0;
100 virtual bool next(uint8_t** data,
size_t* len) = 0;
106 virtual void backup(
size_t len) = 0;
113 virtual uint64_t byteCount()
const = 0;
119 virtual void flush() = 0;
125 AVRO_DECL std::auto_ptr<OutputStream>
memoryOutputStream(
size_t chunkSize = 4 * 1024);
132 AVRO_DECL std::auto_ptr<InputStream>
memoryInputStream(
const uint8_t* data,
size_t len);
141 AVRO_DECL std::auto_ptr<InputStream>
memoryInputStream(
const OutputStream& source);
148 AVRO_DECL boost::shared_ptr<std::vector<uint8_t> >
snapshot(
const OutputStream& source);
157 AVRO_DECL std::auto_ptr<OutputStream>
fileOutputStream(
const char* filename,
158 size_t bufferSize = 8 * 1024);
164 AVRO_DECL std::auto_ptr<InputStream>
fileInputStream(
const char* filename,
165 size_t bufferSize = 8 * 1024);
173 size_t bufferSize = 8 * 1024);
181 size_t bufferSize = 8 * 1024);
215 if (in_ != 0 && end_ != next_) {
216 in_->
backup(end_ - next_);
242 size_t q = end_ -
next_;
246 ::memcpy(b, next_, q);
258 if (n > static_cast<size_t>(end_ - next_)) {
275 while (in_->
next(&next_, &n)) {
297 return (next_ == end_) ?
fill() :
true;
335 if (out_ != 0 && end_ != next_) {
336 out_->
backup(end_ - next_);
360 size_t q = end_ -
next_;
364 ::memcpy(next_, b, q);
377 out_->
backup(end_ - next_);
388 while (out_->
next(&next_, &n)) {
405 const uint8_t *p = 0;
408 while (in.
next(&p, &n)) {
void copy(InputStream &in, OutputStream &out)
A convenience function to copy all the contents of an input stream into an output stream...
Definition: Stream.hh:403
StreamWriter(OutputStream &out)
Constructs a new writer with the given underlying stream.
Definition: Stream.hh:328
const uint8_t * next_
The next location to read from.
Definition: Stream.hh:193
void flush()
backs up upto the currently written data and flushes the underlying stream.
Definition: Stream.hh:375
StreamReader(InputStream &in)
Constructs a reader with the given underlying stream.
Definition: Stream.hh:208
uint8_t * end_
One past the last location one can write to.
Definition: Stream.hh:318
StreamReader()
Constructs an empty reader.
Definition: Stream.hh:203
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:31
void skipBytes(size_t n)
Skips the given number of bytes.
Definition: Stream.hh:257
AVRO_DECL std::auto_ptr< OutputStream > memoryOutputStream(size_t chunkSize=4 *1024)
Returns a new OutputStream, which grows in memory chunks of specified size.
void writeBytes(const uint8_t *b, size_t n)
Writes the specified number of bytes starting at b.
Definition: Stream.hh:355
void readBytes(uint8_t *b, size_t n)
Reads the given number of bytes from the underlying stream.
Definition: Stream.hh:237
bool hasMore()
Returns true if and only if the end of stream is not reached.
Definition: Stream.hh:296
void more()
Gets more space to write to.
Definition: Stream.hh:386
InputStream * in_
The underlying input stream.
Definition: Stream.hh:188
void write(uint8_t c)
Writes a single byte.
Definition: Stream.hh:345
StreamWriter()
Constructs a writer with no underlying stream.
Definition: Stream.hh:323
virtual bool next(uint8_t **data, size_t *len)=0
Returns a buffer that can be written into.
void reset(OutputStream &os)
Replaces the current underlying stream with a new one.
Definition: Stream.hh:334
AVRO_DECL std::auto_ptr< InputStream > istreamInputStream(std::istream &in, size_t bufferSize=8 *1024)
Returns a new InputStream whose contents come from the given std::istream.
AVRO_DECL std::auto_ptr< InputStream > fileInputStream(const char *filename, size_t bufferSize=8 *1024)
Returns a new InputStream whose contents come from the given file.
AVRO_DECL std::auto_ptr< InputStream > memoryInputStream(const uint8_t *data, size_t len)
Returns a new InputStream, with the data from the given byte array.
virtual void backup(size_t len)=0
"Returns" back to the stream some of the buffer obtained from in the last call to next()...
const uint8_t * end_
One past the last valid location.
Definition: Stream.hh:198
AVRO_DECL boost::shared_ptr< std::vector< uint8_t > > snapshot(const OutputStream &source)
Returns the contents written so far into the output stream, which should be a memory output stream...
uint8_t * next_
The next location to write to.
Definition: Stream.hh:313
void reset(InputStream &is)
Replaces the current input stream with the given one after backing up the original one if required...
Definition: Stream.hh:214
A convenience class for reading from an InputStream.
Definition: Stream.hh:184
A no-copy output stream.
Definition: Stream.hh:81
Wrapper for std::runtime_error that provides convenience constructor for boost::format objects...
Definition: Exception.hh:31
bool fill()
Get as many byes from the underlying stream as possible in a single chunk.
Definition: Stream.hh:273
virtual void flush()=0
Flushes any data remaining in the buffer to the stream's underlying store, if any.
OutputStream()
An empty constuctor.
Definition: Stream.hh:87
OutputStream * out_
The underlying output stream for this writer.
Definition: Stream.hh:308
AVRO_DECL std::auto_ptr< OutputStream > ostreamOutputStream(std::ostream &os, size_t bufferSize=8 *1024)
Returns a new OutputStream whose contents will be sent to the given std::ostream. ...
A convinience class to write data into an OutputStream.
Definition: Stream.hh:304
AVRO_DECL std::auto_ptr< OutputStream > fileOutputStream(const char *filename, size_t bufferSize=8 *1024)
Returns a new OutputStream whose contents would be stored in a file.
void more()
Tries to get more data and if it cannot, throws an exception.
Definition: Stream.hh:287
virtual ~OutputStream()
Destructor.
Definition: Stream.hh:93
uint8_t read()
Read just one byte from the underlying stream.
Definition: Stream.hh:226