chunked_ostream.h
// -*- mode: c++; c-basic-offset:4 -*-
// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.
// Copyright (c) 2013 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
//
// Portions of this code were taken verbatim from Josuttis,
// "The C++ Standard Library," p.672
#ifndef _chunkedostream_h
#define _chunkedostream_h
#include "chunked_stream.h"
#include <ostream>
#include <stdexcept> // std::out_of_range
#include <streambuf>
#include "util.h"
namespace libdap {
class chunked_ostream;
class chunked_outbuf : public std::streambuf {
friend class chunked_ostream;
protected:
std::ostream &d_os; // Write stuff here
unsigned int d_buf_size; // Size of the data buffer
char *d_buffer; // Data buffer
bool d_big_endian;
public:
chunked_outbuf(std::ostream &os, unsigned int buf_size) : d_os(os), d_buf_size(buf_size), d_buffer(0) {
if (d_buf_size & CHUNK_TYPE_MASK)
throw std::out_of_range(
"A chunked_outbuf (or chunked_ostream) was built using a buffer larger than 0x00ffffff");
d_big_endian = is_host_big_endian();
d_buffer = new char[buf_size];
// Trick: making the pointers think the buffer is one char smaller than it
// really is ensures that overflow() will be called when there's space for
// one more character.
setp(d_buffer, d_buffer + (buf_size - 1));
}
virtual ~chunked_outbuf() {
// call end_chunk() and not sync()
end_chunk();
delete[] d_buffer;
}
protected:
// data_chunk and end_chunk might not be needed because they
// are called via flush() and ~chunked_outbuf(), resp. jhrg 9/13/13
int_type data_chunk(); // sync() and overflow() call this
int_type end_chunk();
int_type err_chunk(const std::string &msg);
virtual std::streamsize xsputn(const char *s, std::streamsize num);
// Manipulate the buffer pointers using pbump() after filling the buffer
// and then call data_chunk(). Leave remainder in buffer. Or copy logic
// for data_chunk() into loop in this code.
virtual int_type overflow(int c);
virtual int_type sync();
};
class chunked_ostream : public std::ostream {
protected:
chunked_outbuf d_cbuf;
public:
chunked_ostream(std::ostream &os, unsigned int buf_size) : std::ostream(&d_cbuf), d_cbuf(os, buf_size) {}
int_type write_end_chunk() { return d_cbuf.end_chunk(); }
int_type write_data_chunk() { return d_cbuf.data_chunk(); }
int_type write_err_chunk(const std::string &msg) { return d_cbuf.err_chunk(msg); }
};
} // namespace libdap
#endif // _chunkedostream_h
libdap::chunked_ostream
A C++ stream class for chunked data. This class uses the chunked_outbuf class to provide for chunked ...
Definition chunked_ostream.h:122
libdap::chunked_ostream::chunked_ostream
chunked_ostream(std::ostream &os, unsigned int buf_size)
Definition chunked_ostream.h:132
libdap::chunked_ostream::write_data_chunk
int_type write_data_chunk()
Send the current contents of the buffer as a data chunk. Normally, the chunked_ostream object waits u...
Definition chunked_ostream.h:152
libdap::chunked_ostream::write_end_chunk
int_type write_end_chunk()
Send an end chunk. Normally, an end chunk is sent by closing the chunked_ostream, but this method can...
Definition chunked_ostream.h:142
libdap::chunked_ostream::write_err_chunk
int_type write_err_chunk(const std::string &msg)
Send an error message down the stream. When called, this method dumps all the data currently in the b...
Definition chunked_ostream.h:162
libdap::chunked_outbuf
output buffer for a chunked stream This performs buffered output encoding the data in the stream usin...
Definition chunked_ostream.h:53
libdap::chunked_outbuf::xsputn
virtual std::streamsize xsputn(const char *s, std::streamsize num)
Write bytes to the chunked stream Write the bytes in s to the chunked stream.
Definition chunked_ostream.cc:239
libdap::chunked_outbuf::end_chunk
int_type end_chunk()
Send an end chunk.
Definition chunked_ostream.cc:106
libdap::chunked_outbuf::data_chunk
int_type data_chunk()
Write out the contents of the buffer as a chunk.
Definition chunked_ostream.cc:53
libdap::chunked_outbuf::err_chunk
int_type err_chunk(const std::string &msg)
Send an error chunk While building up the next chunk, send an error chunk, ignoring the data currentl...
Definition chunked_ostream.cc:146
libdap::chunked_outbuf::sync
virtual int_type sync()
Synchronize the stream with its data sink.
Definition chunked_ostream.cc:321
libdap::chunked_outbuf::overflow
virtual int_type overflow(int c)
Virtual method called when the internal buffer would overflow. When the internal buffer fills,...
Definition chunked_ostream.cc:199
libdap
top level DAP object to house generic methods
Definition AlarmHandler.h:35
libdap::is_host_big_endian
bool is_host_big_endian()
Does this host use big-endian byte order?
Definition util.cc:94