1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
namespace IceInternal
{
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
//
// Class to provide a System.IO.Stream interface on top of a BasicStream.
// We use this to serialize arbitrary .NET serializable classes into
// a Slice byte sequence.
//
// For input streams, this class is a wrapper around the BasicStream
// class that passes all methods through.
//
// For output streams, we use a different stragegy:
// Slice sequences are encoded on the wire as a count of elements, followed
// by the sequence contents. For arbitrary .NET classes, we do not know how
// big the sequence that is eventually written will be. To avoid excessive
// data copying, this class mantains a private bytes_ array of 254 bytes and,
// initially, writes data into that array. If more than 254 bytes end up being
// written, we write a dummy sequence size of 255 (which occupies five bytes
// on the wire) into the BasicStream and, once this class is disposed, patch
// that size to match the actual size. Otherwise, if the bytes_ buffer contains
// fewer than 255 bytes when this class is disposed, we write the sequence size
// as a single byte, followed by the contents of the bytes_ buffer.
//
enum StreamType { Read, Write };
public class StreamWrapper : System.IO.Stream, System.IDisposable
{
//
// Writeable stream constructor
//
public StreamWrapper(BasicStream s)
{
type_ = StreamType.Write;
s_ = s;
spos_ = s.pos();
bytes_ = new byte[254];
pos_ = 0;
length_ = 0;
}
//
// Readable stream constructor
//
public StreamWrapper(int size, BasicStream s)
{
type_ = StreamType.Read;
s_ = s;
spos_ = 0;
bytes_ = null;
pos_ = 0;
length_ = size;
}
public override int Read(byte[] buffer, int offset, int count)
{
Debug.Assert(buffer != null && offset >= 0 && count >= 0 && offset + count <= buffer.Length);
try
{
s_.getBuffer().b.get(buffer, offset, count);
}
catch(System.Exception ex)
{
throw new IOException("could not read from stream", ex);
}
return count;
}
public override int ReadByte()
{
try
{
return s_.getBuffer().b.get();
}
catch(System.Exception ex)
{
throw new IOException("could not read from stream", ex);
}
}
public override void Write(byte[] array, int offset, int count)
{
Debug.Assert(type_ == StreamType.Write);
Debug.Assert(array != null && offset >= 0 && count >= 0 && offset + count <= array.Length);
try
{
if(bytes_ != null)
{
//
// If we can fit the data into the first 254 bytes, write it to bytes_.
//
if(count <= bytes_.Length - pos_)
{
System.Buffer.BlockCopy(array, offset, bytes_, pos_, count);
pos_ += count;
return;
}
s_.writeSize(255); // Dummy size, until we know how big the stream
// really is and can patch the size.
if(pos_ > 0)
{
//
// Write the current contents of bytes_.
//
s_.expand(pos_);
s_.getBuffer().b.put(bytes_, 0, pos_);
}
bytes_ = null;
}
//
// Write data passed by caller.
//
s_.expand(count);
s_.getBuffer().b.put(array, offset, count);
pos_ += count;
}
catch(System.Exception ex)
{
throw new IOException("could not write to stream", ex);
}
}
public override void WriteByte(byte value)
{
Debug.Assert(type_ == StreamType.Write);
try
{
if(bytes_ != null)
{
//
// If we can fit the data into the first 254 bytes, write it to bytes_.
//
if(pos_ < bytes_.Length)
{
bytes_[pos_++] = value;
return;
}
s_.writeSize(255); // Dummy size, until we know how big the stream
// really is and can patch the size.
if(pos_ > 0)
{
//
// Write the current contents of bytes_.
//
s_.expand(pos_);
s_.getBuffer().b.put(bytes_, 0, pos_);
}
bytes_ = null;
}
//
// Write data passed by caller.
//
s_.expand(1);
s_.getBuffer().b.put(value);
pos_ += 1;
}
catch(System.Exception ex)
{
throw new IOException("could not write to stream", ex);
}
}
public override bool CanRead
{
get
{
return true;
}
}
public override bool CanWrite
{
get
{
return type_ == StreamType.Write;
}
}
public override bool CanSeek
{
get
{
if(AssemblyUtil.runtime_ == AssemblyUtil.Runtime.Mono)
{
//
// The Mono deserialization implementation has a bug that causes a call to Seek() such
// that the reading position is set to -1.
//
return false;
}
else
{
return type_ == StreamType.Read;
}
}
}
public override void Flush()
{
try
{
if(bytes_ != null)
{
Debug.Assert(pos_ <= bytes_.Length);
s_.pos(spos_);
s_.writeSize(pos_);
s_.expand(pos_);
s_.getBuffer().b.put(bytes_, 0, pos_);
}
else
{
int currentPos = s_.pos();
s_.pos(spos_);
s_.writeSize(pos_); // Patch previously-written dummy value.
s_.pos(currentPos);
}
}
catch(System.Exception ex)
{
throw new IOException("could not flush stream", ex);
}
}
public override long Length
{
get
{
return length_;
}
}
public override long Position
{
get
{
return pos_;
}
set
{
Seek(value, SeekOrigin.Begin);
}
}
public override long Seek(long offset, SeekOrigin origin)
{
Debug.Assert(type_ != StreamType.Write);
// Deliberately no size check here--positioning beyond the limit of the stream is legal.
switch(origin)
{
case SeekOrigin.Begin:
{
pos_ = (int)offset;
break;
}
case SeekOrigin.Current:
{
pos_ += (int)offset;
break;
}
case SeekOrigin.End:
{
pos_ = (int)length_ + (int)offset;
break;
}
default:
{
Debug.Assert(false);
break;
}
}
s_.pos(pos_);
return pos_;
}
public override void SetLength(long value)
{
Debug.Assert(type_ == StreamType.Write && value >= 0);
length_ = value;
}
private StreamType type_;
private BasicStream s_;
private int spos_;
private byte[] bytes_;
private int pos_;
private long length_;
}
}
|