comparison tango/tango/io/stream/DataStream.d @ 132:1700239cab2e trunk

[svn r136] MAJOR UNSTABLE UPDATE!!! Initial commit after moving to Tango instead of Phobos. Lots of bugfixes... This build is not suitable for most things.
author lindquist
date Fri, 11 Jan 2008 17:57:40 +0100
parents
children
comparison
equal deleted inserted replaced
131:5825d48b27d1 132:1700239cab2e
1 /*******************************************************************************
2
3 copyright: Copyright (c) 2007 Kris Bell. All rights reserved
4
5 license: BSD style: $(LICENSE)
6
7 version: Initial release: Oct 2007
8
9 author: Kris
10
11 *******************************************************************************/
12
13 module tango.io.stream.DataStream;
14
15 private import tango.io.Buffer;
16
17 private import tango.io.Conduit;
18
19 private import tango.core.ByteSwap;
20
21 /*******************************************************************************
22
23 A simple way to read binary data from an arbitrary InputStream,
24 such as a file:
25 ---
26 auto input = new DataInput (new FileInput("path"));
27 auto x = input.readInt;
28 auto y = input.readDouble;
29 input.read (new char[10]);
30 input.close;
31 ---
32
33 *******************************************************************************/
34
35 class DataInput : InputFilter, Buffered
36 {
37 private bool flip;
38 private IBuffer input;
39
40 /***********************************************************************
41
42 Propagate ctor to superclass
43
44 ***********************************************************************/
45
46 this (InputStream stream, uint buffer=uint.max, bool flip=false)
47 {
48 this.flip = flip;
49 super (input = Buffer.share (stream, buffer));
50 }
51
52 /***********************************************************************
53
54 Buffered interface
55
56 ***********************************************************************/
57
58 final IBuffer buffer ()
59 {
60 return input;
61 }
62
63 /***********************************************************************
64
65 Override this to give back a useful chaining reference
66
67 ***********************************************************************/
68
69 final override DataInput clear ()
70 {
71 host.clear;
72 return this;
73 }
74
75 /***********************************************************************
76
77 Read an array back into a user-provided workspace. The
78 space must be sufficiently large enough to house all of
79 the array, and the actual number of bytes is returned.
80
81 Note that the size of the array is written as an integer
82 prefixing the array content itself. Use read(void[]) to
83 eschew this prefix.
84
85 ***********************************************************************/
86
87 final override uint get (void[] dst)
88 {
89 auto len = getInt;
90 if (len > dst.length)
91 conduit.error ("DataInput.readArray :: dst array is too small");
92 input.readExact (dst.ptr, len);
93 return len;
94 }
95
96 /***********************************************************************
97
98 ***********************************************************************/
99
100 final bool getBool ()
101 {
102 bool x;
103 input.readExact (&x, x.sizeof);
104 return x;
105 }
106
107 /***********************************************************************
108
109 ***********************************************************************/
110
111 final byte getByte ()
112 {
113 byte x;
114 input.readExact (&x, x.sizeof);
115 return x;
116 }
117
118 /***********************************************************************
119
120 ***********************************************************************/
121
122 final short getShort ()
123 {
124 short x;
125 input.readExact (&x, x.sizeof);
126 if (flip)
127 ByteSwap.swap16(&x, x.sizeof);
128 return x;
129 }
130
131 /***********************************************************************
132
133 ***********************************************************************/
134
135 final int getInt ()
136 {
137 int x;
138 input.readExact (&x, x.sizeof);
139 if (flip)
140 ByteSwap.swap32(&x, x.sizeof);
141 return x;
142 }
143
144 /***********************************************************************
145
146 ***********************************************************************/
147
148 final long getLong ()
149 {
150 long x;
151 input.readExact (&x, x.sizeof);
152 if (flip)
153 ByteSwap.swap64(&x, x.sizeof);
154 return x;
155 }
156
157 /***********************************************************************
158
159 ***********************************************************************/
160
161 final float getFloat ()
162 {
163 float x;
164 input.readExact (&x, x.sizeof);
165 if (flip)
166 ByteSwap.swap32(&x, x.sizeof);
167 return x;
168 }
169
170 /***********************************************************************
171
172 ***********************************************************************/
173
174 final double getDouble ()
175 {
176 double x;
177 input.readExact (&x, x.sizeof);
178 if (flip)
179 ByteSwap.swap64(&x, x.sizeof);
180 return x;
181 }
182
183 }
184
185
186 /*******************************************************************************
187
188 A simple way to write binary data to an arbitrary OutputStream,
189 such as a file:
190 ---
191 auto output = new DataOutput (new FileOutput("path"));
192 output.writeInt (1024);
193 output.writeDouble (3.14159);
194 output.write ("hello world");
195 output.flush.close;
196 ---
197
198 *******************************************************************************/
199
200 class DataOutput : OutputFilter, Buffered
201 {
202 private bool flip;
203 private IBuffer output;
204
205 /***********************************************************************
206
207 Propagate ctor to superclass
208
209 ***********************************************************************/
210
211 this (OutputStream stream, uint buffer=uint.max, bool flip = false)
212 {
213 this.flip = flip;
214 super (output = Buffer.share (stream, buffer));
215 }
216
217 /***********************************************************************
218
219 Buffered interface
220
221 ***********************************************************************/
222
223 final IBuffer buffer ()
224 {
225 return output;
226 }
227
228 /***********************************************************************
229
230 Write an array to the target stream. Note that the size
231 of the array is written as an integer prefixing the array
232 content itself. Use write(void[]) to eschew this prefix.
233
234 ***********************************************************************/
235
236 final uint put (void[] src)
237 {
238 auto len = src.length;
239 putInt (len);
240 output.append (src.ptr, len);
241 return len;
242 }
243
244 /***********************************************************************
245
246 ***********************************************************************/
247
248 final void putBool (bool x)
249 {
250 output.append (&x, x.sizeof);
251 }
252
253 /***********************************************************************
254
255 ***********************************************************************/
256
257 final void putByte (byte x)
258 {
259 output.append (&x, x.sizeof);
260 }
261
262 /***********************************************************************
263
264 ***********************************************************************/
265
266 final void putShort (short x)
267 {
268 if (flip)
269 ByteSwap.swap16 (&x, x.sizeof);
270 output.append (&x, x.sizeof);
271 }
272
273 /***********************************************************************
274
275 ***********************************************************************/
276
277 final void putInt (int x)
278 {
279 if (flip)
280 ByteSwap.swap32 (&x, x.sizeof);
281 output.append (&x, uint.sizeof);
282 }
283
284 /***********************************************************************
285
286 ***********************************************************************/
287
288 final void putLong (long x)
289 {
290 if (flip)
291 ByteSwap.swap64 (&x, x.sizeof);
292 output.append (&x, x.sizeof);
293 }
294
295 /***********************************************************************
296
297 ***********************************************************************/
298
299 final void putFloat (float x)
300 {
301 if (flip)
302 ByteSwap.swap32 (&x, x.sizeof);
303 output.append (&x, x.sizeof);
304 }
305
306 /***********************************************************************
307
308 ***********************************************************************/
309
310 final void putDouble (double x)
311 {
312 if (flip)
313 ByteSwap.swap64 (&x, x.sizeof);
314 output.append (&x, x.sizeof);
315 }
316 }
317
318 /*******************************************************************************
319
320 *******************************************************************************/
321
322 debug (UnitTest)
323 {
324 import tango.io.Buffer;
325
326 unittest
327 {
328 auto buf = new Buffer(32);
329
330 auto output = new DataOutput (buf);
331 output.put ("blah blah");
332 output.putInt (1024);
333
334 auto input = new DataInput (buf);
335 assert (input.get(new char[9]) is 9);
336 assert (input.getInt is 1024);
337 }
338 }