libdap Updated for version 3.21.1
libdap4 is an implementation of OPeNDAP's DAP protocol.
chunked_ostream.h
Go to the documentation of this file.
1// -*- mode: c++; c-basic-offset:4 -*-
2
3// This file is part of libdap, A C++ implementation of the OPeNDAP Data
4// Access Protocol.
5
6// Copyright (c) 2013 OPeNDAP, Inc.
7// Author: James Gallagher <jgallagher@opendap.org>
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or (at your option) any later version.
13//
14// This library is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17// Lesser General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22//
23// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24//
25// Portions of this code were taken verbatim from Josuttis,
26// "The C++ Standard Library," p.672
27
28#ifndef _chunkedostream_h
29#define _chunkedostream_h
30
31#include "chunked_stream.h"
32
33#include <ostream>
34#include <stdexcept> // std::out_of_range
35#include <streambuf>
36
37#include "util.h"
38
39namespace libdap {
40
41class chunked_ostream;
42
53class chunked_outbuf : public std::streambuf {
54 friend class chunked_ostream;
55
56protected:
57 std::ostream &d_os; // Write stuff here
58 unsigned int d_buf_size; // Size of the data buffer
59 char *d_buffer; // Data buffer
61
62public:
63 chunked_outbuf(std::ostream &os, unsigned int buf_size) : d_os(os), d_buf_size(buf_size), d_buffer(0) {
65 throw std::out_of_range(
66 "A chunked_outbuf (or chunked_ostream) was built using a buffer larger than 0x00ffffff");
67
69 d_buffer = new char[buf_size];
70 // Trick: making the pointers think the buffer is one char smaller than it
71 // really is ensures that overflow() will be called when there's space for
72 // one more character.
73 setp(d_buffer, d_buffer + (buf_size - 1));
74 }
75
76 virtual ~chunked_outbuf() {
77 // call end_chunk() and not sync()
78 end_chunk();
79
80 delete[] d_buffer;
81 }
82
83protected:
84 // data_chunk and end_chunk might not be needed because they
85 // are called via flush() and ~chunked_outbuf(), resp. jhrg 9/13/13
86 int_type data_chunk(); // sync() and overflow() call this
87 int_type end_chunk();
88
89 int_type err_chunk(const std::string &msg);
90
91 virtual std::streamsize xsputn(const char *s, std::streamsize num);
92 // Manipulate the buffer pointers using pbump() after filling the buffer
93 // and then call data_chunk(). Leave remainder in buffer. Or copy logic
94 // for data_chunk() into loop in this code.
95
96 virtual int_type overflow(int c);
97 virtual int_type sync();
98};
99
122class chunked_ostream : public std::ostream {
123protected:
125
126public:
132 chunked_ostream(std::ostream &os, unsigned int buf_size) : std::ostream(&d_cbuf), d_cbuf(os, buf_size) {}
133
142 int_type write_end_chunk() { return d_cbuf.end_chunk(); }
143
152 int_type write_data_chunk() { return d_cbuf.data_chunk(); }
153
162 int_type write_err_chunk(const std::string &msg) { return d_cbuf.err_chunk(msg); }
163};
164
165} // namespace libdap
166
167#endif // _chunkedostream_h
#define CHUNK_TYPE_MASK
A C++ stream class for chunked data. This class uses the chunked_outbuf class to provide for chunked ...
chunked_ostream(std::ostream &os, unsigned int buf_size)
int_type write_data_chunk()
Send the current contents of the buffer as a data chunk. Normally, the chunked_ostream object waits u...
int_type write_end_chunk()
Send an end chunk. Normally, an end chunk is sent by closing the chunked_ostream, but this method can...
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...
output buffer for a chunked stream This performs buffered output encoding the data in the stream usin...
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.
int_type end_chunk()
Send an end chunk.
int_type data_chunk()
Write out the contents of the buffer as a chunk.
chunked_outbuf(std::ostream &os, unsigned int buf_size)
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...
virtual int_type sync()
Synchronize the stream with its data sink.
virtual int_type overflow(int c)
Virtual method called when the internal buffer would overflow. When the internal buffer fills,...
top level DAP object to house generic methods
Definition AISConnect.cc:30
bool is_host_big_endian()
Does this host use big-endian byte order?
Definition util.cc:94