123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- using System.Globalization;
- using CommonLang.Xml;
- using System.Xml;
- namespace CommonLang.IO
- {
- public class TextOutputStream : IOutputStream
- {
- private StringWriter output;
- public TextOutputStream(StringWriter output, IExternalizableFactory factory = null)
- : base(factory)
- {
- this.output = output;
- }
- private void PutNext(Object src)
- {
- output.Write(src + ",");
- }
- public override long GetBuffPos()
- {
- return 0;
- }
- public override void PutUTF(String str)
- {
- if (string.IsNullOrEmpty(str))
- {
- output.Write("0,,");
- }
- else
- {
- output.Write(str.Length + ",");
- output.Write(str + ",");
- }
- }
- public override void PutS8(sbyte value)
- {
- PutNext(value);
- }
- public override void PutU8(byte value)
- {
- PutNext(value);
- }
- public override void PutBool(bool value)
- {
- PutNext(value);
- }
- public override void PutS16(short value)
- {
- PutNext(value);
- }
- public override void PutU16(ushort value)
- {
- PutNext(value);
- }
- public override void PutS32(int value)
- {
- PutNext(value);
- }
- public override void PutU32(uint value)
- {
- PutNext(value);
- }
- public override void PutS64(long value)
- {
- PutNext(value);
- }
- public override void PutU64(ulong value)
- {
- PutNext(value);
- }
- public override void PutF32(float value)
- {
- PutNext(value);
- }
- public override void PutF64(double value)
- {
- PutNext(value);
- }
- public override void PutUnicode(char value)
- {
- PutNext(value);
- }
- public override void PutBytes(byte[] bytes)
- {
- PutS32(bytes.Length);
- if (bytes != null && bytes.Length > 0)
- {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < bytes.Length; i++)
- {
- sb.Append(bytes[i].ToString());
- if (i < bytes.Length - 1)
- {
- sb.Append(',');
- }
- }
- PutUTF(sb.ToString());
- }
- }
- public override void PutRawData(byte[] bytes, int offset, int count)
- {
- throw new NotImplementedException();
- }
- }
- public class TextInputStream : IInputStream
- {
- private StringReader input;
- public TextInputStream(StringReader input, IExternalizableFactory factory = null)
- : base(factory)
- {
- this.input = input;
- }
- private String GetNext()
- {
- StringBuilder sb = new StringBuilder();
- while (true)
- {
- int r = input.Read();
- if (r == -1)
- break;
- if (r == ',')
- break;
- sb.Append((char)r);
- }
- return sb.ToString();
- }
- public override String GetUTF()
- {
- int stringLen = GetS32();
- char[] chars = new char[stringLen];
- input.Read(chars, 0, stringLen);
- if (input.Read() != ',')
- {
- //System.err.println("bat ending for : " + in);
- }
- return new String(chars);
- }
- public override sbyte GetS8()
- {
- return sbyte.Parse(GetNext());
- }
- public override byte GetU8()
- {
- return byte.Parse(GetNext());
- }
- public override bool GetBool()
- {
- String read = GetNext();
- try
- {
- return bool.Parse(read);
- }
- catch (Exception err)
- {
- Console.WriteLine("GetBool 1: " + read + ", catch: " + err);
- return Byte.Parse(read) != 0;
- }
- }
- public override short GetS16()
- {
- return short.Parse(GetNext());
- }
- public override ushort GetU16()
- {
- return ushort.Parse(GetNext());
- }
- public override int GetS32()
- {
- return int.Parse(GetNext());
- }
- public override uint GetU32()
- {
- return uint.Parse(GetNext());
- }
- public override long GetS64()
- {
- return long.Parse(GetNext(), System.Globalization.CultureInfo.InvariantCulture);
- }
- public override ulong GetU64()
- {
- return ulong.Parse(GetNext());
- }
- public override float GetF32()
- {
- return float.Parse(GetNext(), System.Globalization.CultureInfo.InvariantCulture);
- }
- public override double GetF64()
- {
- return double.Parse(GetNext(), System.Globalization.CultureInfo.InvariantCulture);
- }
- public override char GetUnicode()
- {
- return char.Parse(GetNext());
- }
- public override byte[] GetBytes()
- {
- int count = GetS32();
- byte[] ret = new byte[count];
- string utf = GetUTF();
- string[] utfs = utf.Split(',');
- for (int i = 0; i < utfs.Length; i++)
- {
- ret[i] = byte.Parse(utfs[i]);
- }
- return ret;
- }
- public override void GetRawData(byte[] buff, int offset, int count)
- {
- throw new NotImplementedException();
- }
- }
- }
|