jungerl /lib/otp.net/Otp/OtpOutputStream.cs

Language C# Lines 786
MD5 Hash f4e6388cf3c2ac2efb97e0a67d2f6f6e Estimated Cost $8,301 (why?)
Repository git://github.com/gebi/jungerl.git View Raw File View Project SPDX
  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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/*``The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved via the world wide web at http://www.erlang.org/.
* 
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
* 
* The Initial Developer of the Original Code is Ericsson Utvecklings AB.
* Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
* AB. All Rights Reserved.''
* 
 * Converted from Java to C# by Vlad Dumitrescu (vlad_Dumitrescu@hotmail.com)
*/
namespace Otp
{
	using System;

	/*
	* Provides a stream for encoding Erlang terms to external format, for
	* transmission or storage.
	* 
	* <p> Note that this class is not synchronized, if you need
	* synchronization you must provide it yourself.
	*
	**/
	public class OtpOutputStream
	{
		/*The default initial size of the stream. **/
		public const int defaultInitialSize = 2048;
		
		/*The default increment used when growing the stream. **/
		public const int defaultIncrement = 2048;
		
		private byte[] buf = null;
		private int _size = 0;
		private int _count = 0;

		/*
		* Create a stream with the default initial size.
		**/
		public OtpOutputStream():this(defaultInitialSize)
		{
		}
		
		/*
		* Create a stream with the specified initial size.
		**/
		public OtpOutputStream(int size)
		{
			this._size = size;
			buf = new byte[size];
			_count = 0;
		}
		
		/*
		* Create a stream containing the encoded version of the given
		* Erlang term.
		**/
		public OtpOutputStream(Erlang.Object o):this()
		{
			this.write_any(o);
		}
		
		// package scope
		/*
		* Get the contents of the output stream as an input stream instead.
		* This is used internally in {@link OtpCconnection} for tracing
		* outgoing packages.
		*
		* @param offset where in the output stream to read data from when
		* creating the input stream. The offset is necessary because header
		* contents start 5 bytes into the header buffer, whereas payload
		* contents start at the beginning
		*
		* @return an input stream containing the same raw data.
		**/
		internal virtual OtpInputStream getOtpInputStream(int offset)
		{
			return new OtpInputStream(buf, offset, _count - offset);
		}
		
		/*
		* Reset the stream so that it can be reused.
		**/
		public virtual void  reset()
		{
			_count = 0;
		}
		
		/*
		* Get the current position in the stream.
		*
		* @return the current position in the stream.
		**/
		public virtual int getPos()
		{
			return _count;
		}

		/*
		* Write one byte to the stream.
		*
		* @param b the byte to write.
		*
		**/
		public virtual void  write(byte b)
		{
			if (_count >= _size)
			{
				// System.err.println("Expanding buffer from " + size + " to " + size + defaultIncrement);
				byte[] tmp = new byte[_size + defaultIncrement];
				Array.Copy(buf, 0, tmp, 0, _count);
				_size += defaultIncrement;
				buf = tmp;
			}
			
			buf[_count++] = b;
		}
		
		/*
		* Write an array of bytes to the stream.
		*
		* @param buf the array of bytes to write.
		*
		**/
		public virtual void  write(byte[] buf)
		{
			if (_count + buf.Length > _size)
			{
				// System.err.println("Expanding buffer from " +
				// size + " to " + buf.length + size + defaultIncrement);
				byte[] tmp = new byte[_size + buf.Length + defaultIncrement];
				Array.Copy(this.buf, 0, tmp, 0, _count);
				_size += defaultIncrement + buf.Length;
				this.buf = tmp;
			}
			
			Array.Copy(buf, 0, this.buf, _count, buf.Length);
			_count += (int) (buf.Length);
		}
		
		/*
		* Write the contents of the stream to an OutputStream.
		*
		* @param os the OutputStream to write to.
		*
		* @exception C#.io.IOException if there is an error writing to
		* the OutputStream.
		**/
		public virtual void  writeTo(System.IO.Stream os)
		{
			os.Write(buf, 0, _count);
			os.Flush();
		}
		
		/*
		* Write the low byte of a value to the stream.
		*
		* @param n the value to use.
		*
		**/
		public virtual void  write1(long n)
		{
			write((byte) (n & 0xff));
		}
		
		/*
		* Write an array of bytes to the stream.
		*
		* @param buf the array of bytes to write.
		*
		**/
		public virtual void  writeN(byte[] bytes)
		{
			write(bytes);
		}
		
		/*
		* Get the current capacity of the stream. As bytes are added the
		* capacity of the stream is increased automatically, however this
		* method returns the current size.
		*
		* @return the size of the internal buffer used by the stream.
		**/
		public virtual int size()
		{
			return _size;
		}
		
		/*
		* Get the number of bytes in the stream.
		*
		* @return the number of bytes in the stream.
		**/
		public virtual int count()
		{
			return _count;
		}
		
		/*
		* Write the low two bytes of a value to the stream in big endian
		* order.
		*
		* @param n the value to use.
		**/
		public virtual void  write2BE(long n)
		{
			write((byte) ((n & 0xff00) >> 8));
			write((byte) (n & 0xff));
		}
		
		/*
		* Write the low four bytes of a value to the stream in big endian
		* order.
		*
		* @param n the value to use.
		**/
		public virtual void  write4BE(long n)
		{
			write((byte) ((n & 0xff000000) >> 24));
			write((byte) ((n & 0xff0000) >> 16));
			write((byte) ((n & 0xff00) >> 8));
			write((byte) (n & 0xff));
		}
		
		/*
		* Write the low two bytes of a value to the stream in little endian
		* order.
		*
		* @param n the value to use.
		**/
		public virtual void  write2LE(long n)
		{
			write((byte) (n & 0xff));
			write((byte) ((n & 0xff00) >> 8));
		}
		
		/*
		* Write the low four bytes of a value to the stream in little
		* endian order.
		*
		* @param n the value to use.
		**/
		public virtual void  write4LE(long n)
		{
			write((byte) (n & 0xff));
			write((byte) ((n & 0xff00) >> 8));
			write((byte) ((n & 0xff0000) >> 16));
			write((byte) ((n & 0xff000000) >> 24));
		}
		
		/*
		* Write the low four bytes of a value to the stream in bif endian
		* order, at the specified position. If the position specified is
		* beyond the end of the stream, this method will have no effect.
		*
		* Normally this method should be used in conjunction with {@link
		* #getPos() getPos()}, when is is necessary to insert data into the
		* stream before it is known what the actual value should be. For
		* example:
		*
		<pre>
		int pos = s.getPos();
		s.write4BE(0); // make space for length data,
		// but final value is not yet known
		
		[ ...more write statements...]
		
		// later... when we know the length value
		s.poke4BE(pos, length);
		</pre>
		
		*
		* @param offset the position in the stream.
		* @param n the value to use.
		**/
		public virtual void  poke4BE(int offset, long n)
		{
			if (offset < _count)
			{
				buf[offset + 0] = ((byte) ((n & 0xff000000) >> 24));
				buf[offset + 1] = ((byte) ((n & 0xff0000) >> 16));
				buf[offset + 2] = ((byte) ((n & 0xff00) >> 8));
				buf[offset + 3] = ((byte) (n & 0xff));
			}
		}
		
		/*
		* Write a string to the stream as an Erlang atom.
		*
		* @param atom the string to write.
		**/
		public virtual void  write_atom(System.String atom)
		{
			this.write1(OtpExternal.atomTag);
			this.write2BE(atom.Length);
			//UPGRADE_NOTE: This code will be optimized in the future;
			byte[] tmpBytes;
			int i;
			string tmpStr;
			tmpStr = atom;
			tmpBytes = new byte[tmpStr.Length];
			i = 0;
			while (i < tmpStr.Length)
			{
				tmpBytes[i] = (byte) tmpStr[i];
				i++;
			}
			this.writeN(tmpBytes);
		}
		
		/*
		* Write an array of bytes to the stream as an Erlang binary.
		*
		* @param bin the array of bytes to write.
		**/
		public virtual void  write_binary(byte[] bin)
		{
			this.write1(OtpExternal.binTag);
			this.write4BE(bin.Length);
			this.writeN(bin);
		}
		
		/*
		* Write a boolean value to the stream as the Erlang atom 'true' or
		* 'false'.
		*
		* @param b the boolean value to write.
		**/
		public virtual void  write_boolean(bool b)
		{
			this.write_atom((b ? Erlang.Boolean.s_true : Erlang.Boolean.s_false).atomValue());
		}
		
		/*
		* Write a single byte to the stream as an Erlang integer.
		*
		* @param b the byte to use.
		**/
		public virtual void  write_byte(byte b)
		{
			this.write_long(b);
		}
		
		/*
		* Write a character to the stream as an Erlang integer.
		*
		* @param c the character to use.
		**/
		public virtual void  write_char(char c)
		{
			this.write_long(c);
		}
		
		/*
		* Write a double value to the stream. 
		*
		* @param d the double to use.
		**/
		public virtual void  write_double(double d)
		{
            this.write1(OtpExternal.newFloatTag);

            byte[] data = BitConverter.GetBytes(d);
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(data);
            }
            this.write(data);
            /*
            double val;
			int exp = 0;
			int sign = 0;
			System.String str;
			
			// remove sign to simplify decimal shift
			if (d >= 0)
			{
				val = d;
			}
			else
			{
				sign = 1;
				val = - d;
			}
			
			// move the decimal point until we have a single units digit
			if (System.Math.Sign(val) != 0)
			{
				// until greater than or equal to 1.0  -> multiply by 10
				while (val < 1.0)
				{
					val *= 10;
					exp--;
				}
				// until strictly less than 10.0 -> divide by 10
				while (val >= 10.0)
				{
					val /= 10;
					exp++;
				}
			}

			// get 20 decimal digits, put sign back, add new exponent
			//UPGRADE_TODO: The equivalent in .NET for Class C#.math.BigDecimal.ROUND_HALF_EVEN will be considered in a future release.;
			//val = val.setScale(20, BigDecimal.ROUND_HALF_EVEN);
			//UPGRADE_TODO: The equivalent in .NET for Class C#.math.BigDecimal.toString will be considered in a future release.;
			str = (sign == 1?"-":"") + System.Convert.ToString(val) + "e" + System.Convert.ToString(exp);

			// write the value
			this.write1(OtpExternal.floatTag);
			//UPGRADE_NOTE: This code will be optimized in the future;
			byte[] tmpBytes;
			int i;
			string tmpStr;
			tmpStr = str;
			tmpBytes = new byte[tmpStr.Length];
			i = 0;
			while (i < tmpStr.Length)
			{
				tmpBytes[i] = (byte) tmpStr[i];
				i++;
			}
			this.writeN(tmpBytes);
			
			// pad with zeros to 31 bytes
			//UPGRADE_NOTE: This code will be optimized in the future;
			byte[] tmpBytes2;
			int i2;
			string tmpStr2;
			tmpStr2 = str;
			tmpBytes2 = new byte[tmpStr2.Length];
			i2 = 0;
			while (i2 < tmpStr2.Length)
			{
				tmpBytes2[i2] = (byte) tmpStr2[i2];
				i2++;
			}
			int i3 = (int) (tmpBytes2.Length);
			 for (; i3 < 31; i3++)
				this.write1(0);
            */
		}
		
		
		/*
		* Write a float value to the stream. 
		*
		* @param f the float to use.
		**/
		public virtual void  write_float(float f)
		{
            this.write_double(f);
		}
		
		/*
		* Write a long to the stream.
		*
		* @param l the long to use.
		**/
		public virtual void  write_long(long l)
		{
			if ((l & 0xff) == l)
			{
				// will fit in one byte
				this.write1(OtpExternal.smallIntTag);
				this.write1(l);
			}
			else if ((l <= OtpExternal.erlMax) && (l >= OtpExternal.erlMin))
			{
				this.write1(OtpExternal.intTag);
				this.write4BE(l);
			}
			else
			{
				this.write1(OtpExternal.smallBigTag);
				this.write1(4); // length
				
				// obs! little endian here
				if (l < 0)
				{
					this.write1(1); // sign
					this.write4LE(- l); // value
				}
				else
				{
					this.write1(0); // sign
					this.write4LE(l); //value
				}
			}
		}
		
		/*
		* Write a positive long to the stream.
		*
		* @param ul the long to use.
		**/
		public virtual void  write_ulong(long ul)
		{
			this.write_long(ul);
		}
		
		/*
		* Write an integer to the stream.
		*
		* @param i the integer to use.
		**/
		public virtual void  write_int(int i)
		{
			this.write_long(i);
		}
		
		/*
		* Write a positive integer to the stream.
		*
		* @param ui the integer to use.
		**/
		public virtual void  write_uint(int ui)
		{
			this.write_long(ui);
		}
		
		/*
		* Write a short to the stream.
		*
		* @param s the short to use.
		**/
		public virtual void  write_short(short s)
		{
			this.write_long(s);
		}
		
		/*
		* Write a positive short to the stream.
		*
		* @param s the short to use.
		**/
		public virtual void  write_ushort(short us)
		{
			this.write_long(us);
		}
		
		/*
		* Write an Erlang list header to the stream. After calling this
		* method, you must write 'arity' elements to the stream followed by
		* nil, or it will not be possible to decode it later.
		*
		* @param arity the number of elements in the list.
		**/
		public virtual void  write_list_head(int arity)
		{
			if (arity == 0)
			{
				this.write_nil();
			}
			else
			{
				this.write1(OtpExternal.listTag);
				this.write4BE(arity);
			}
		}
		
		/*
		* Write an empty Erlang list to the stream.
		**/
		public virtual void  write_nil()
		{
			this.write1(OtpExternal.nilTag);
		}
		
		/*
		* Write an Erlang tuple header to the stream. After calling this
		* method, you must write 'arity' elements to the stream or it will
		* not be possible to decode it later.
		*
		* @param arity the number of elements in the tuple.
		**/
		public virtual void  write_tuple_head(int arity)
		{
			if (arity < 0xff)
			{
				this.write1(OtpExternal.smallTupleTag);
				this.write1(arity);
			}
			else
			{
				this.write1(OtpExternal.largeTupleTag);
				this.write4BE(arity);
			}
		}
		
		/*
		* Write an Erlang PID to the stream. 
		*
		* @param node the nodename.
		*
		* @param id an arbitrary number. Only the low order 15 bits will
		* be used.
		*
		* @param serial another arbitrary number. Only the low order 3 bits
		* will be used.
		*
		* @param creation yet another arbitrary number. Only the low order
		* 2 bits will be used.
		* 
		**/
		public virtual void  write_pid(System.String node, int id, int serial, int creation)
		{
			this.write1(OtpExternal.pidTag);
			this.write_atom(node);
			this.write4BE(id & 0x7fff); // 15 bits
			this.write4BE(serial & 0x7); // 3 bits
			this.write1(creation & 0x3); // 2 bits
		}
		
		/*
		* Write an Erlang port to the stream. 
		*
		* @param node the nodename.
		*
		* @param id an arbitrary number. Only the low order 18 bits will
		* be used.
		*
		* @param creation another arbitrary number. Only the low order 2
		* bits will be used.
		* 
		**/
		public virtual void  write_port(System.String node, int id, int creation)
		{
			this.write1(OtpExternal.portTag);
			this.write_atom(node);
			this.write4BE(id & 0x3ffff); // 18 bits
			this.write1(creation & 0x3); // 2 bits
		}
		
		/*
		* Write an old style Erlang ref to the stream. 
		*
		* @param node the nodename.
		*
		* @param id an arbitrary number. Only the low order 18 bits will
		* be used.
		*
		* @param creation another arbitrary number. Only the low order 2
		* bits will be used.
		* 
		**/
		public virtual void  write_ref(System.String node, int id, int creation)
		{
			this.write1(OtpExternal.refTag);
			this.write_atom(node);
			this.write4BE(id & 0x3ffff); // 18 bits
			this.write1(creation & 0x3); // 2 bits
		}
		
		/*
		* Write a new style (R6 and later) Erlang ref to the stream.
		*
		* @param node the nodename.
		*
		* @param ids an array of arbitrary numbers. Only the low order 18
		* bits of the first number will be used. If the array contains only
		* one number, an old style ref will be written instead. At most
		* three numbers will be read from the array.
		*
		* @param creation another arbitrary number. Only the low order
		* 2 bits will be used.
		* 
		**/
		public virtual void  write_ref(System.String node, int[] ids, int creation)
		{
			int arity = (int) (ids.Length);
			if (arity > 3)
				arity = 3;
			// max 3 words in ref
			
			if (arity == 1)
			{
				// use old method
				this.write_ref(node, ids[0], creation);
			}
			else
			{
				// r6 ref
				this.write1(OtpExternal.newRefTag);
				
				// how many id values
				this.write2BE(arity);
				
				this.write_atom(node);
				
				// note: creation BEFORE id in r6 ref
				this.write1(creation & 0x3); // 2 bits
				
				// first int gets truncated to 18 bits
				this.write4BE(ids[0] & 0x3ffff);
				
				// remaining ones are left as is
				 for (int i = 1; i < arity; i++)
					this.write4BE(ids[i]);
			}
		}
		
		/*
		* Write a string to the stream.
		*
		* @param s the string to write.
		**/
		public virtual void  write_string(System.String s)
		{
			int len = s.Length;
			
			switch (len)
			{
				case 0: 
					this.write_nil();
					break;
				
				default: 
					//UPGRADE_NOTE: This code will be optimized in the future;
					byte[] tmpBytes;
					int i;
					string tmpStr;
					tmpStr = s;
					tmpBytes = new byte[tmpStr.Length];
					i = 0;
					while (i < tmpStr.Length)
					{
						tmpBytes[i] = (byte) tmpStr[i];
						i++;
					}
					byte[] bytebuf = tmpBytes;
					
					/*switch to se if the length of
					the byte array is equal to the 
					length of the list */
					if (bytebuf.Length == len)
					{
						/*Usual */
						this.write1(OtpExternal.stringTag);
						this.write2BE(len);
						this.writeN(bytebuf);
					}
					else
					{
						/*Unicode */
						char[] charbuf = s.ToCharArray();
						
						this.write_list_head(len);
						
						 for (int i2 = 0; i2 < len; i2++)
							this.write_char(charbuf[i2]);
						
						this.write_nil();
					}
					break;
				
			}
		}
		
		
		/*
		This does not work when char > 1 byte Unicode is used
		
		public void write_string(String s) {
		this.write1(OtpExternal.stringTag);
		this.write2BE(s.length());
		this.writeN(s.getBytes());
		}*/
		
		/*
		* Write an arbitrary Erlang term to the stream.
		*
		* @param o the Erlang term to write.
		*/
		public virtual void write_any(Erlang.Object o)
		{
			// calls one of the above functions, depending on o
			o.encode(this);
		}
	}
}
Back to Top