libdap  Updated for version 3.20.6
libdap4 is an implementation of OPeNDAP's DAP protocol.
chunked_ostream.h
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 <streambuf>
34 #include <ostream>
35 #include <stdexcept> // std::out_of_range
36 
37 #include "util.h"
38 
39 namespace libdap {
40 
41 class chunked_ostream;
42 
53 class chunked_outbuf: public std::streambuf {
54  friend class chunked_ostream;
55 protected:
56  std::ostream &d_os; // Write stuff here
57  unsigned int d_buf_size; // Size of the data buffer
58  char *d_buffer; // Data buffer
59  bool d_big_endian;
60 
61 public:
62  chunked_outbuf(std::ostream &os, unsigned int buf_size) : d_os(os), d_buf_size(buf_size), d_buffer(0) {
63  if (d_buf_size & CHUNK_TYPE_MASK)
64  throw std::out_of_range("A chunked_outbuf (or chunked_ostream) was built using a buffer larger than 0x00ffffff");
65 
66  d_big_endian = is_host_big_endian();
67  d_buffer = new char[buf_size];
68  // Trick: making the pointers think the buffer is one char smaller than it
69  // really is ensures that overflow() will be called when there's space for
70  // one more character.
71  setp(d_buffer, d_buffer + (buf_size - 1));
72  }
73 
74  virtual ~chunked_outbuf() {
75  // call end_chunk() and not sync()
76  end_chunk();
77 
78  delete[] d_buffer;
79  }
80 
81 protected:
82  // data_chunk and end_chunk might not be needed because they
83  // are called via flush() and ~chunked_outbuf(), resp. jhrg 9/13/13
84  int_type data_chunk(); // sync() and overflow() call this
85  int_type end_chunk();
86 
87  int_type err_chunk(const std::string &msg);
88 
89  virtual std::streamsize xsputn(const char *s, std::streamsize num);
90  // Manipulate the buffer pointers using pbump() after filling the buffer
91  // and then call data_chunk(). Leave remainder in buffer. Or copy logic
92  // for data_chunk() into loop in this code.
93 
94  virtual int_type overflow(int c);
95  virtual int_type sync();
96 };
97 
120 class chunked_ostream: public std::ostream {
121 protected:
122  chunked_outbuf d_cbuf;
123 public:
129  chunked_ostream(std::ostream &os, unsigned int buf_size) : std::ostream(&d_cbuf), d_cbuf(os, buf_size) { }
130 
139  int_type write_end_chunk() { return d_cbuf.end_chunk(); }
140 
149  int_type write_data_chunk() { return d_cbuf.data_chunk(); }
150 
159  int_type write_err_chunk(const std::string &msg) { return d_cbuf.err_chunk(msg); }
160 };
161 
162 }
163 
164 #endif // _chunkedostream_h
int_type write_end_chunk()
Send an end chunk. Normally, an end chunk is sent by closing the chunked_ostream, but this method can...
STL namespace.
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
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 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.
chunked_ostream(std::ostream &os, unsigned int buf_size)
int_type data_chunk()
Write out the contents of the buffer as a chunk.
output buffer for a chunked stream This performs buffered output encoding the data in the stream usin...
int_type write_data_chunk()
Send the current contents of the buffer as a data chunk. Normally, the chunked_ostream object waits u...
A C++ stream class for chunked data. This class uses the chunked_outbuf class to provide for chunked ...
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...
bool is_host_big_endian()
Does this host use big-endian byte order?
Definition: util.cc:94