comparison src/main/csharp/jp.ac.u-ryukyu.ie.cr/jungle-main/persistent/msgpack/src/MsgPackWriter.cs @ 10:abe0c247f5a5

Add Network module. but, unComplete NetworkDefaultJungleTreeEditor.cs
author Kazuma Takeda <kazuma-arashi@hotmail.co.jp>
date Sun, 23 Oct 2016 07:40:50 +0900
parents
children
comparison
equal deleted inserted replaced
9:e6ad9016601c 10:abe0c247f5a5
1 //
2 // Copyright 2011 Kazuki Oikawa, Kazunari Kida
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 using System;
18 using System.IO;
19 using System.Text;
20
21 namespace MsgPack
22 {
23 public class MsgPackWriter
24 {
25 Stream _strm;
26 //Encoding _encoding = Encoding.UTF8;
27 Encoder _encoder = Encoding.UTF8.GetEncoder ();
28 byte[] _tmp = new byte[9];
29 byte[] _buf = new byte[64];
30
31 public MsgPackWriter (Stream strm)
32 {
33 _strm = strm;
34 }
35
36 public void Write (byte x)
37 {
38 if (x < 128) {
39 _strm.WriteByte (x);
40 } else {
41 byte[] tmp = _tmp;
42 tmp[0] = 0xcc;
43 tmp[1] = x;
44 _strm.Write (tmp, 0, 2);
45 }
46 }
47
48 public void Write (ushort x)
49 {
50 if (x < 0x100) {
51 Write ((byte)x);
52 } else {
53 byte[] tmp = _tmp;
54 tmp[0] = 0xcd;
55 tmp[1] = (byte)(x >> 8);
56 tmp[2] = (byte)x;
57 _strm.Write (tmp, 0, 3);
58 }
59 }
60
61 public void Write (char x)
62 {
63 Write ((ushort)x);
64 }
65
66 public void Write (uint x)
67 {
68 if (x < 0x10000) {
69 Write ((ushort)x);
70 } else {
71 byte[] tmp = _tmp;
72 tmp[0] = 0xce;
73 tmp[1] = (byte)(x >> 24);
74 tmp[2] = (byte)(x >> 16);
75 tmp[3] = (byte)(x >> 8);
76 tmp[4] = (byte)x;
77 _strm.Write (tmp, 0, 5);
78 }
79 }
80
81 public void Write (ulong x)
82 {
83 if (x < 0x100000000) {
84 Write ((uint)x);
85 } else {
86 byte[] tmp = _tmp;
87 tmp[0] = 0xcf;
88 tmp[1] = (byte)(x >> 56);
89 tmp[2] = (byte)(x >> 48);
90 tmp[3] = (byte)(x >> 40);
91 tmp[4] = (byte)(x >> 32);
92 tmp[5] = (byte)(x >> 24);
93 tmp[6] = (byte)(x >> 16);
94 tmp[7] = (byte)(x >> 8);
95 tmp[8] = (byte)x;
96 _strm.Write (tmp, 0, 9);
97 }
98 }
99
100 public void Write (sbyte x)
101 {
102 if (x >= -32 && x <= -1) {
103 _strm.WriteByte ((byte)(0xe0 | (byte)x));
104 } else if (x >= 0 && x <= 127) {
105 _strm.WriteByte ((byte)x);
106 } else {
107 byte[] tmp = _tmp;
108 tmp[0] = 0xd0;
109 tmp[1] = (byte)x;
110 _strm.Write (tmp, 0, 2);
111 }
112 }
113
114 public void Write (short x)
115 {
116 if (x >= sbyte.MinValue && x <= sbyte.MaxValue) {
117 Write ((sbyte)x);
118 } else {
119 byte[] tmp = _tmp;
120 tmp[0] = 0xd1;
121 tmp[1] = (byte)(x >> 8);
122 tmp[2] = (byte)x;
123 _strm.Write (tmp, 0, 3);
124 }
125 }
126
127 public void Write (int x)
128 {
129 if (x >= short.MinValue && x <= short.MaxValue) {
130 Write ((short)x);
131 } else {
132 byte[] tmp = _tmp;
133 tmp[0] = 0xd2;
134 tmp[1] = (byte)(x >> 24);
135 tmp[2] = (byte)(x >> 16);
136 tmp[3] = (byte)(x >> 8);
137 tmp[4] = (byte)x;
138 _strm.Write (tmp, 0, 5);
139 }
140 }
141
142 public void Write (long x)
143 {
144 if (x >= int.MinValue && x <= int.MaxValue) {
145 Write ((int)x);
146 } else {
147 byte[] tmp = _tmp;
148 tmp[0] = 0xd3;
149 tmp[1] = (byte)(x >> 56);
150 tmp[2] = (byte)(x >> 48);
151 tmp[3] = (byte)(x >> 40);
152 tmp[4] = (byte)(x >> 32);
153 tmp[5] = (byte)(x >> 24);
154 tmp[6] = (byte)(x >> 16);
155 tmp[7] = (byte)(x >> 8);
156 tmp[8] = (byte)x;
157 _strm.Write (tmp, 0, 9);
158 }
159 }
160
161 public void WriteNil ()
162 {
163 _strm.WriteByte (0xc0);
164 }
165
166 public void Write (bool x)
167 {
168 _strm.WriteByte ((byte)(x ? 0xc3 : 0xc2));
169 }
170
171 public void Write (float x)
172 {
173 byte[] raw = BitConverter.GetBytes (x); // unsafeコードを使う?
174 byte[] tmp = _tmp;
175
176 tmp[0] = 0xca;
177 if (BitConverter.IsLittleEndian) {
178 tmp[1] = raw[3];
179 tmp[2] = raw[2];
180 tmp[3] = raw[1];
181 tmp[4] = raw[0];
182 } else {
183 tmp[1] = raw[0];
184 tmp[2] = raw[1];
185 tmp[3] = raw[2];
186 tmp[4] = raw[3];
187 }
188 _strm.Write (tmp, 0, 5);
189 }
190
191 public void Write (double x)
192 {
193 byte[] raw = BitConverter.GetBytes (x); // unsafeコードを使う?
194 byte[] tmp = _tmp;
195
196 tmp[0] = 0xcb;
197 if (BitConverter.IsLittleEndian) {
198 tmp[1] = raw[7];
199 tmp[2] = raw[6];
200 tmp[3] = raw[5];
201 tmp[4] = raw[4];
202 tmp[5] = raw[3];
203 tmp[6] = raw[2];
204 tmp[7] = raw[1];
205 tmp[8] = raw[0];
206 } else {
207 tmp[1] = raw[0];
208 tmp[2] = raw[1];
209 tmp[3] = raw[2];
210 tmp[4] = raw[3];
211 tmp[5] = raw[4];
212 tmp[6] = raw[5];
213 tmp[7] = raw[6];
214 tmp[8] = raw[7];
215 }
216 _strm.Write (tmp, 0, 9);
217 }
218
219 public void Write (byte[] bytes)
220 {
221 WriteRawHeader (bytes.Length);
222 _strm.Write (bytes, 0, bytes.Length);
223 }
224
225 public void WriteRawHeader (int N)
226 {
227 WriteLengthHeader (N, 32, 0xa0, 0xda, 0xdb);
228 }
229
230 public void WriteArrayHeader (int N)
231 {
232 WriteLengthHeader (N, 16, 0x90, 0xdc, 0xdd);
233 }
234
235 public void WriteMapHeader (int N)
236 {
237 WriteLengthHeader (N, 16, 0x80, 0xde, 0xdf);
238 }
239
240 void WriteLengthHeader (int N, int fix_length, byte fix_prefix, byte len16bit_prefix, byte len32bit_prefix)
241 {
242 if (N < fix_length) {
243 _strm.WriteByte ((byte)(fix_prefix | N));
244 } else {
245 byte[] tmp = _tmp;
246 int header_len;
247 if (N < 0x10000) {
248 tmp[0] = len16bit_prefix;
249 tmp[1] = (byte)(N >> 8);
250 tmp[2] = (byte)N;
251 header_len = 3;
252 } else {
253 tmp[0] = len32bit_prefix;
254 tmp[1] = (byte)(N >> 24);
255 tmp[2] = (byte)(N >> 16);
256 tmp[3] = (byte)(N >> 8);
257 tmp[4] = (byte)N;
258 header_len = 5;
259 }
260 _strm.Write (tmp, 0, header_len);
261 }
262 }
263
264 public void Write (string x)
265 {
266 Write (x, false);
267 }
268
269 public void Write (string x, bool highProbAscii)
270 {
271 Write (x, _buf, highProbAscii);
272 }
273
274 public void Write (string x, byte[] buf)
275 {
276 Write (x, buf, false);
277 }
278
279 public void Write (string x, byte[] buf, bool highProbAscii)
280 {
281 Encoder encoder = _encoder;
282 //fixed (char *pstr = x)
283 //fixed (byte *pbuf = buf) {
284 char[] str = x.ToCharArray();
285 if (highProbAscii && x.Length <= buf.Length) {
286 bool isAsciiFullCompatible = true;
287 for (int i = 0; i < x.Length; i ++) {
288 //int v = (int)pstr[i];
289 int v = (int)(x[i]);
290 if (v > 0x7f) {
291 isAsciiFullCompatible = false;
292 break;
293 }
294 buf[i] = (byte)v;
295 }
296 if (isAsciiFullCompatible) {
297 WriteRawHeader (x.Length);
298 _strm.Write (buf, 0, x.Length);
299 return;
300 }
301 }
302
303 //WriteRawHeader (encoder.GetByteCount (pstr, x.Length, true));
304 WriteRawHeader (encoder.GetByteCount (str, 0, x.Length, true));
305 int str_len = x.Length;
306 //char *p = pstr;
307 int convertedChars, bytesUsed;
308 bool completed = true;
309 int j = 0;
310 while (str_len > 0 || !completed) {
311 //encoder.Convert (p, str_len, pbuf, buf.Length, false, out convertedChars, out bytesUsed, out completed);
312 encoder.Convert (str, j, str_len, buf, 0, buf.Length, false, out convertedChars, out bytesUsed, out completed);
313 _strm.Write (buf, 0, bytesUsed);
314 str_len -= convertedChars;
315 //p += convertedChars;
316 j += convertedChars;
317 }
318 //}
319 }
320 }
321 }