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.");
83 XDRStreamUnMarshaller::~XDRStreamUnMarshaller()
85 xdr_destroy( &d_source );
89 void XDRStreamUnMarshaller::get_byte(dods_byte &val)
91 if (xdr_setpos( &d_source, 0 ) < 0)
92 throw Error(
"Failed to reposition input stream");
93 if (!(d_in.read(d_buf, 4))) {
95 throw Error(
"Premature EOF in input stream");
97 ostringstream ss(
"Error reading from input stream: ");
99 throw Error(ss.str());
103 DBG2( std::cerr <<
"_in.gcount(): " << d_in.gcount() << std::endl );
104 DBG2( std::cerr <<
"_in.tellg(): " << d_in.tellg() << std::endl );
105 DBG2( std::cerr <<
"_buf[0]: " << hex << d_buf[0] <<
"; _buf[1]: " << d_buf[1]
106 <<
"; _buf[2]: " << d_buf[2] <<
"; _buf[3]: " << d_buf[3]
107 << dec << std::endl );
109 if (!xdr_char(&d_source, (
char *) &val))
110 throw Error(
"Network I/O Error. Could not read byte data.");
112 DBG2(std::cerr <<
"get_byte: " << val << std::endl );
115 void XDRStreamUnMarshaller::get_int16(dods_int16 &val)
117 xdr_setpos( &d_source, 0);
120 if (!XDR_INT16(&d_source, &val))
121 throw Error(
"Network I/O Error. Could not read int 16 data.");
124 void XDRStreamUnMarshaller::get_int32(dods_int32 &val)
126 xdr_setpos( &d_source, 0);
129 if (!XDR_INT32(&d_source, &val))
130 throw Error(
"Network I/O Error. Could not read int 32 data.");
133 void XDRStreamUnMarshaller::get_float32(dods_float32 &val)
135 xdr_setpos( &d_source, 0);
138 if (!xdr_float(&d_source, &val))
139 throw Error(
"Network I/O Error. Could not read float 32 data.");
142 void XDRStreamUnMarshaller::get_float64(dods_float64 &val)
144 xdr_setpos( &d_source, 0);
147 if (!xdr_double(&d_source, &val))
148 throw Error(
"Network I/O Error. Could not read float 64 data.");
151 void XDRStreamUnMarshaller::get_uint16(dods_uint16 &val)
153 xdr_setpos( &d_source, 0);
156 if (!XDR_UINT16(&d_source, &val))
157 throw Error(
"Network I/O Error. Could not read uint 16 data.");
160 void XDRStreamUnMarshaller::get_uint32(dods_uint32 &val)
162 xdr_setpos( &d_source, 0);
165 if (!XDR_UINT32(&d_source, &val))
166 throw Error(
"Network I/O Error. Could not read uint 32 data.");
169 void XDRStreamUnMarshaller::get_str(
string &val)
173 DBG(std::cerr <<
"i: " << i << std::endl);
176 i = ((i + 3) / 4) * 4;
177 DBG(std::cerr <<
"i: " << i << std::endl);
183 if (i + 4 > XDR_DAP_BUFF_SIZE) {
185 char *buf = (
char *) malloc(i + 4);
187 throw InternalErr(__FILE__, __LINE__,
"Error allocating memory");
189 vector<char> buf(i+4);
192 xdrmem_create(&source, &buf[0], i + 4, XDR_DECODE);
193 memcpy(&buf[0], d_buf, 4);
195 d_in.read(&buf[0] + 4, i);
197 xdr_setpos( &source, 0);
198 if (!xdr_string( &source, &in_tmp, max_str_len)) {
199 xdr_destroy( &source );
200 throw Error(
"Network I/O Error. Could not read string data.");
203 xdr_destroy( &source );
206 d_in.read(d_buf + 4, i);
208 xdr_setpos( &d_source, 0);
209 if (!xdr_string(&d_source, &in_tmp, max_str_len))
210 throw Error(
"Network I/O Error. Could not read string data.");
218 void XDRStreamUnMarshaller::get_url(
string &val)
223 void XDRStreamUnMarshaller::get_opaque(
char *val,
unsigned int len)
225 xdr_setpos( &d_source, 0);
230 if (static_cast<int>(len) > XDR_DAP_BUFF_SIZE)
231 throw Error(
"Network I/O Error. Length of opaque data larger than allowed");
233 d_in.read(d_buf, len);
235 xdr_opaque(&d_source, val, len);
238 void XDRStreamUnMarshaller::get_int(
int &val)
240 xdr_setpos( &d_source, 0);
243 if (!xdr_int(&d_source, &val))
244 throw Error(
"Network I/O Error(1).");
246 DBG(std::cerr <<
"get_int: " << val << std::endl);
249 void XDRStreamUnMarshaller::get_vector(
char **val,
unsigned int &num, Vector &)
253 DBG(std::cerr <<
"i: " << i << std::endl);
257 DBG(std::cerr <<
"i: " << i << std::endl);
262 if (i + 4 > XDR_DAP_BUFF_SIZE) {
263 vector<char> buf(i+4);
265 xdrmem_create(&source, &buf[0], i + 4, XDR_DECODE);
266 memcpy(&buf[0], d_buf, 4);
268 d_in.read(&buf[0] + 4, i);
269 DBG2(cerr <<
"bytes read: " << d_in.gcount() << endl);
271 xdr_setpos(&source, 0);
272 if (!xdr_bytes(&d_source, val, &num, DODS_MAX_ARRAY)) {
273 xdr_destroy(&source);
274 throw Error(
"Network I/O Error. Could not read byte array data.");
277 xdr_destroy( &source );
280 d_in.read(d_buf + 4, i);
281 DBG2(cerr <<
"bytes read: " << d_in.gcount() << endl);
283 xdr_setpos(&d_source, 0);
284 if (!xdr_bytes(&d_source, val, &num, DODS_MAX_ARRAY))
285 throw Error(
"Network I/O Error. Could not read byte array data.");
289 void XDRStreamUnMarshaller::get_vector(
char **val,
unsigned int &num,
int width, Vector &vec)
291 get_vector(val, num, width, vec.var()->type());
294 void XDRStreamUnMarshaller::get_vector(
char **val,
unsigned int &num,
int width,
Type type)
298 DBG(std::cerr <<
"i: " << i << std::endl);
301 DBG(std::cerr <<
"width: " << width << std::endl);
303 int size = i * width;
306 if (size > XDR_DAP_BUFF_SIZE) {
307 vector<char> buf(size+4);
309 xdrmem_create(&source, &buf[0], size + 4, XDR_DECODE);
310 DBG(cerr <<
"size: " << size << endl);
311 memcpy(&buf[0], d_buf, 4);
313 d_in.read(&buf[0] + 4, size);
314 DBG(cerr <<
"bytes read: " << d_in.gcount() << endl);
316 xdr_setpos(&source, 0);
318 xdr_destroy( &source );
319 throw Error(
"Network I/O Error. Could not read array data.");
322 xdr_destroy( &source );
325 d_in.read(d_buf + 4, size);
326 DBG(cerr <<
"bytes read (2): " << d_in.gcount() << endl);
328 xdr_setpos( &d_source, 0);
330 throw Error(
"Network I/O Error. Could not read array data.");
336 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.