32#include "XDRStreamUnMarshaller.h"
44#include "InternalErr.h"
51char *XDRStreamUnMarshaller::d_buf = 0;
53XDRStreamUnMarshaller::XDRStreamUnMarshaller(istream &in)
57 d_buf = (
char *)malloc(XDR_DAP_BUFF_SIZE);
59 throw Error(internal_error,
"Failed to allocate memory for data serialization.");
62 xdrmem_create(&d_source, d_buf, XDR_DAP_BUFF_SIZE, XDR_DECODE);
65XDRStreamUnMarshaller::XDRStreamUnMarshaller() : UnMarshaller(), d_in(cin) {
66 throw InternalErr(__FILE__, __LINE__,
"Default constructor not implemented.");
69XDRStreamUnMarshaller::XDRStreamUnMarshaller(
const XDRStreamUnMarshaller &um)
70 : UnMarshaller(um), d_in(cin) {
71 throw InternalErr(__FILE__, __LINE__,
"Copy constructor not implemented.");
74XDRStreamUnMarshaller &XDRStreamUnMarshaller::operator=(
const XDRStreamUnMarshaller &) {
75 throw InternalErr(__FILE__, __LINE__,
"Copy operator not implemented.");
78XDRStreamUnMarshaller::~XDRStreamUnMarshaller() {
79 xdr_destroy(&d_source);
83void XDRStreamUnMarshaller::get_byte(dods_byte &val) {
84 if (xdr_setpos(&d_source, 0) < 0)
85 throw Error(
"Failed to reposition input stream");
86 if (!(d_in.read(d_buf, 4))) {
88 throw Error(
"Premature EOF in input stream");
90 ostringstream ss(
"Error reading from input stream: ");
92 throw Error(ss.str());
96 DBG2(std::cerr <<
"_in.gcount(): " << d_in.gcount() << std::endl);
97 DBG2(std::cerr <<
"_in.tellg(): " << d_in.tellg() << std::endl);
98 DBG2(std::cerr <<
"_buf[0]: " << hex << d_buf[0] <<
"; _buf[1]: " << d_buf[1] <<
"; _buf[2]: " << d_buf[2]
99 <<
"; _buf[3]: " << d_buf[3] << dec << std::endl);
101 if (!xdr_char(&d_source, (
char *)&val))
102 throw Error(
"Network I/O Error. Could not read byte data.");
104 DBG2(std::cerr <<
"get_byte: " << val << std::endl);
107void XDRStreamUnMarshaller::get_int16(dods_int16 &val) {
108 xdr_setpos(&d_source, 0);
111 if (!XDR_INT16(&d_source, &val))
112 throw Error(
"Network I/O Error. Could not read int 16 data.");
115void XDRStreamUnMarshaller::get_int32(dods_int32 &val) {
116 xdr_setpos(&d_source, 0);
119 if (!XDR_INT32(&d_source, &val))
120 throw Error(
"Network I/O Error. Could not read int 32 data.");
123void XDRStreamUnMarshaller::get_float32(dods_float32 &val) {
124 xdr_setpos(&d_source, 0);
127 if (!xdr_float(&d_source, &val))
128 throw Error(
"Network I/O Error. Could not read float 32 data.");
131void XDRStreamUnMarshaller::get_float64(dods_float64 &val) {
132 xdr_setpos(&d_source, 0);
135 if (!xdr_double(&d_source, &val))
136 throw Error(
"Network I/O Error. Could not read float 64 data.");
139void XDRStreamUnMarshaller::get_uint16(dods_uint16 &val) {
140 xdr_setpos(&d_source, 0);
143 if (!XDR_UINT16(&d_source, &val))
144 throw Error(
"Network I/O Error. Could not read uint 16 data.");
147void XDRStreamUnMarshaller::get_uint32(dods_uint32 &val) {
148 xdr_setpos(&d_source, 0);
151 if (!XDR_UINT32(&d_source, &val))
152 throw Error(
"Network I/O Error. Could not read uint 32 data.");
155void XDRStreamUnMarshaller::get_str(
string &val) {
158 DBG(std::cerr <<
"i: " << i << std::endl);
161 i = ((i + 3) / 4) * 4;
162 DBG(std::cerr <<
"i: " << i << std::endl);
168 if (i + 4 > XDR_DAP_BUFF_SIZE) {
170 char *buf = (
char *) malloc(i + 4);
172 throw InternalErr(__FILE__, __LINE__,
"Error allocating memory");
174 vector<char> buf(i + 4);
177 xdrmem_create(&source, buf.data(), i + 4, XDR_DECODE);
178 memcpy(buf.data(), d_buf, 4);
180 d_in.read(buf.data() + 4, i);
182 xdr_setpos(&source, 0);
183 if (!xdr_string(&source, &in_tmp, max_str_len)) {
184 xdr_destroy(&source);
185 throw Error(
"Network I/O Error. Could not read string data.");
188 xdr_destroy(&source);
190 d_in.read(d_buf + 4, i);
192 xdr_setpos(&d_source, 0);
193 if (!xdr_string(&d_source, &in_tmp, max_str_len))
194 throw Error(
"Network I/O Error. Could not read string data.");
202void XDRStreamUnMarshaller::get_url(
string &val) { get_str(val); }
204void XDRStreamUnMarshaller::get_opaque(
char *val,
unsigned int len) {
205 xdr_setpos(&d_source, 0);
210 if (
static_cast<int>(len) > XDR_DAP_BUFF_SIZE)
211 throw Error(
"Network I/O Error. Length of opaque data larger than allowed");
213 d_in.read(d_buf, len);
215 xdr_opaque(&d_source, val, len);
218void XDRStreamUnMarshaller::get_int(
int &val) {
219 xdr_setpos(&d_source, 0);
222 if (!xdr_int(&d_source, &val))
223 throw Error(
"Network I/O Error(1).");
225 DBG(std::cerr <<
"get_int: " << val << std::endl);
228void XDRStreamUnMarshaller::get_vector(
char **val,
unsigned int &num, Vector &) {
231 DBG(std::cerr <<
"i: " << i << std::endl);
235 DBG(std::cerr <<
"i: " << i << std::endl);
240 if (i + 4 > XDR_DAP_BUFF_SIZE) {
241 vector<char> buf(i + 4);
243 xdrmem_create(&source, buf.data(), i + 4, XDR_DECODE);
244 memcpy(buf.data(), d_buf, 4);
246 d_in.read(buf.data() + 4, i);
247 DBG2(cerr <<
"bytes read: " << d_in.gcount() << endl);
249 xdr_setpos(&source, 0);
250 if (!xdr_bytes(&d_source, val, &num, DODS_MAX_ARRAY)) {
251 xdr_destroy(&source);
252 throw Error(
"Network I/O Error. Could not read byte array data.");
255 xdr_destroy(&source);
257 d_in.read(d_buf + 4, i);
258 DBG2(cerr <<
"bytes read: " << d_in.gcount() << endl);
260 xdr_setpos(&d_source, 0);
261 if (!xdr_bytes(&d_source, val, &num, DODS_MAX_ARRAY))
262 throw Error(
"Network I/O Error. Could not read byte array data.");
266void XDRStreamUnMarshaller::get_vector(
char **val,
unsigned int &num,
int width, Vector &vec) {
267 get_vector(val, num, width, vec.var()->type());
270void XDRStreamUnMarshaller::get_vector(
char **val,
unsigned int &num,
int width, Type type) {
273 DBG(std::cerr <<
"i: " << i << std::endl);
276 DBG(std::cerr <<
"width: " << width << std::endl);
278 int size = i * width;
281 if (size > XDR_DAP_BUFF_SIZE) {
282 vector<char> buf(size + 4);
284 xdrmem_create(&source, buf.data(), size + 4, XDR_DECODE);
285 DBG(cerr <<
"size: " << size << endl);
286 memcpy(buf.data(), d_buf, 4);
288 d_in.read(buf.data() + 4, size);
289 DBG(cerr <<
"bytes read: " << d_in.gcount() << endl);
291 xdr_setpos(&source, 0);
292 if (!xdr_array(&source, val, &num, DODS_MAX_ARRAY, width, XDRUtils::xdr_coder(type))) {
293 xdr_destroy(&source);
294 throw Error(
"Network I/O Error. Could not read array data.");
297 xdr_destroy(&source);
299 d_in.read(d_buf + 4, size);
300 DBG(cerr <<
"bytes read (2): " << d_in.gcount() << endl);
302 xdr_setpos(&d_source, 0);
303 if (!xdr_array(&d_source, val, &num, DODS_MAX_ARRAY, width, XDRUtils::xdr_coder(type)))
304 throw Error(
"Network I/O Error. Could not read array data.");
309 strm << DapIndent::LMarg <<
"XDRStreamUnMarshaller::dump - (" << (
void *)
this <<
")" << endl;
virtual void dump(ostream &strm) const
dump the contents of this object to the specified ostream
top level DAP object to house generic methods