33 #include "XDRStreamUnMarshaller.h" 45 #include "InternalErr.h" 47 #include "DapIndent.h" 51 char *XDRStreamUnMarshaller::d_buf = 0;
53 XDRStreamUnMarshaller::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);
65 XDRStreamUnMarshaller::XDRStreamUnMarshaller() :
66 UnMarshaller(), d_in(cin)
68 throw InternalErr(__FILE__, __LINE__,
"Default constructor not implemented.");
71 XDRStreamUnMarshaller::XDRStreamUnMarshaller(
const XDRStreamUnMarshaller &um) :
72 UnMarshaller(um), d_in(cin)
74 throw InternalErr(__FILE__, __LINE__,
"Copy constructor not implemented.");
77 XDRStreamUnMarshaller &
78 XDRStreamUnMarshaller::operator=(
const XDRStreamUnMarshaller &)
80 throw InternalErr(__FILE__, __LINE__,
"Copy operator not implemented.");
85 XDRStreamUnMarshaller::~XDRStreamUnMarshaller()
87 xdr_destroy( &d_source );
91 void XDRStreamUnMarshaller::get_byte(dods_byte &val)
93 if (xdr_setpos( &d_source, 0 ) < 0)
94 throw Error(
"Failed to reposition input stream");
95 if (!(d_in.read(d_buf, 4))) {
97 throw Error(
"Premature EOF in input stream");
99 ostringstream ss(
"Error reading from input stream: ");
100 ss << d_in.rdstate();
101 throw Error(ss.str());
105 DBG2( std::cerr <<
"_in.gcount(): " << d_in.gcount() << std::endl );
106 DBG2( std::cerr <<
"_in.tellg(): " << d_in.tellg() << std::endl );
107 DBG2( std::cerr <<
"_buf[0]: " << hex << d_buf[0] <<
"; _buf[1]: " << d_buf[1]
108 <<
"; _buf[2]: " << d_buf[2] <<
"; _buf[3]: " << d_buf[3]
109 << dec << std::endl );
111 if (!xdr_char(&d_source, (
char *) &val))
112 throw Error(
"Network I/O Error. Could not read byte data.");
114 DBG2(std::cerr <<
"get_byte: " << val << std::endl );
117 void XDRStreamUnMarshaller::get_int16(dods_int16 &val)
119 xdr_setpos( &d_source, 0);
122 if (!XDR_INT16(&d_source, &val))
123 throw Error(
"Network I/O Error. Could not read int 16 data.");
126 void XDRStreamUnMarshaller::get_int32(dods_int32 &val)
128 xdr_setpos( &d_source, 0);
131 if (!XDR_INT32(&d_source, &val))
132 throw Error(
"Network I/O Error. Could not read int 32 data.");
135 void XDRStreamUnMarshaller::get_float32(dods_float32 &val)
137 xdr_setpos( &d_source, 0);
140 if (!xdr_float(&d_source, &val))
141 throw Error(
"Network I/O Error. Could not read float 32 data.");
144 void XDRStreamUnMarshaller::get_float64(dods_float64 &val)
146 xdr_setpos( &d_source, 0);
149 if (!xdr_double(&d_source, &val))
150 throw Error(
"Network I/O Error. Could not read float 64 data.");
153 void XDRStreamUnMarshaller::get_uint16(dods_uint16 &val)
155 xdr_setpos( &d_source, 0);
158 if (!XDR_UINT16(&d_source, &val))
159 throw Error(
"Network I/O Error. Could not read uint 16 data.");
162 void XDRStreamUnMarshaller::get_uint32(dods_uint32 &val)
164 xdr_setpos( &d_source, 0);
167 if (!XDR_UINT32(&d_source, &val))
168 throw Error(
"Network I/O Error. Could not read uint 32 data.");
171 void XDRStreamUnMarshaller::get_str(
string &val)
175 DBG(std::cerr <<
"i: " << i << std::endl);
178 i = ((i + 3) / 4) * 4;
179 DBG(std::cerr <<
"i: " << i << std::endl);
185 if (i + 4 > XDR_DAP_BUFF_SIZE) {
187 char *buf = (
char *) malloc(i + 4);
189 throw InternalErr(__FILE__, __LINE__,
"Error allocating memory");
191 vector<char> buf(i+4);
194 xdrmem_create(&source, &buf[0], i + 4, XDR_DECODE);
195 memcpy(&buf[0], d_buf, 4);
197 d_in.read(&buf[0] + 4, i);
199 xdr_setpos( &source, 0);
200 if (!xdr_string( &source, &in_tmp, max_str_len)) {
201 xdr_destroy( &source );
202 throw Error(
"Network I/O Error. Could not read string data.");
205 xdr_destroy( &source );
208 d_in.read(d_buf + 4, i);
210 xdr_setpos( &d_source, 0);
211 if (!xdr_string(&d_source, &in_tmp, max_str_len))
212 throw Error(
"Network I/O Error. Could not read string data.");
220 void XDRStreamUnMarshaller::get_url(
string &val)
225 void XDRStreamUnMarshaller::get_opaque(
char *val,
unsigned int len)
227 xdr_setpos( &d_source, 0);
232 if (static_cast<int>(len) > XDR_DAP_BUFF_SIZE)
233 throw Error(
"Network I/O Error. Length of opaque data larger than allowed");
235 d_in.read(d_buf, len);
237 xdr_opaque(&d_source, val, len);
240 void XDRStreamUnMarshaller::get_int(
int &val)
242 xdr_setpos( &d_source, 0);
245 if (!xdr_int(&d_source, &val))
246 throw Error(
"Network I/O Error(1).");
248 DBG(std::cerr <<
"get_int: " << val << std::endl);
251 void XDRStreamUnMarshaller::get_vector(
char **val,
unsigned int &num, Vector &)
255 DBG(std::cerr <<
"i: " << i << std::endl);
259 DBG(std::cerr <<
"i: " << i << std::endl);
264 if (i + 4 > XDR_DAP_BUFF_SIZE) {
265 vector<char> buf(i+4);
267 xdrmem_create(&source, &buf[0], i + 4, XDR_DECODE);
268 memcpy(&buf[0], d_buf, 4);
270 d_in.read(&buf[0] + 4, i);
271 DBG2(cerr <<
"bytes read: " << d_in.gcount() << endl);
273 xdr_setpos(&source, 0);
274 if (!xdr_bytes(&d_source, val, &num, DODS_MAX_ARRAY)) {
275 xdr_destroy(&source);
276 throw Error(
"Network I/O Error. Could not read byte array data.");
279 xdr_destroy( &source );
282 d_in.read(d_buf + 4, i);
283 DBG2(cerr <<
"bytes read: " << d_in.gcount() << endl);
285 xdr_setpos(&d_source, 0);
286 if (!xdr_bytes(&d_source, val, &num, DODS_MAX_ARRAY))
287 throw Error(
"Network I/O Error. Could not read byte array data.");
291 void XDRStreamUnMarshaller::get_vector(
char **val,
unsigned int &num,
int width, Vector &vec)
293 get_vector(val, num, width, vec.var()->type());
296 void XDRStreamUnMarshaller::get_vector(
char **val,
unsigned int &num,
int width,
Type type)
300 DBG(std::cerr <<
"i: " << i << std::endl);
303 DBG(std::cerr <<
"width: " << width << std::endl);
305 int size = i * width;
308 if (size > XDR_DAP_BUFF_SIZE) {
309 vector<char> buf(size+4);
311 xdrmem_create(&source, &buf[0], size + 4, XDR_DECODE);
312 DBG(cerr <<
"size: " << size << endl);
313 memcpy(&buf[0], d_buf, 4);
315 d_in.read(&buf[0] + 4, size);
316 DBG(cerr <<
"bytes read: " << d_in.gcount() << endl);
318 xdr_setpos(&source, 0);
320 xdr_destroy( &source );
321 throw Error(
"Network I/O Error. Could not read array data.");
324 xdr_destroy( &source );
327 d_in.read(d_buf + 4, size);
328 DBG(cerr <<
"bytes read (2): " << d_in.gcount() << endl);
330 xdr_setpos( &d_source, 0);
332 throw Error(
"Network I/O Error. Could not read array data.");
338 strm << DapIndent::LMarg <<
"XDRStreamUnMarshaller::dump - (" << (
void *)
this <<
")" << endl;
Type
Identifies the data type.
virtual void dump(ostream &strm) const
dump the contents of this object to the specified ostream
top level DAP object to house generic methods
static xdrproc_t xdr_coder(const Type &t)
Returns a function used to encode elements of an array.