コード例 #1
0
        public void WriteTo(Stream s)
        {
            BinaryWriter bW = new BinaryWriter(s);

            bW.Write((byte)2); //version

            byte[] buffer;

            buffer = Encoding.UTF8.GetBytes(_name);
            bW.Write(Convert.ToByte(buffer.Length));
            bW.Write(buffer, 0, buffer.Length);

            bW.Write(Convert.ToUInt64((_lastModifiedUTC - _epoch).TotalSeconds));
            bW.Write((byte)_attributes);

            bW.Write((byte)_extractTo);
            if (_extractTo == ExtractLocation.Custom)
            {
                buffer = Encoding.UTF8.GetBytes(_extractToCustomLocation);
                bW.Write(Convert.ToByte(buffer.Length));
                bW.Write(buffer, 0, buffer.Length);
            }

            bW.Write(_data.Length);
            OffsetStream.StreamCopy(_data, bW);
        }
コード例 #2
0
        public BincodingDecoder(Stream s, string format)
        {
            if (format.Length != 2)
            {
                throw new ArgumentException("Argument 'format' must be of 2 characters.");
            }

            byte[] buffer = new byte[2];
            OffsetStream.StreamRead(s, buffer, 0, 2);

            if (format != Encoding.ASCII.GetString(buffer))
            {
                throw new InvalidDataException("Unable to decode: invalid data format.");
            }

            _s      = s;
            _format = format;

            int version = s.ReadByte();

            if (version < 0)
            {
                throw new EndOfStreamException();
            }

            _version = (byte)version;
        }
コード例 #3
0
        public BinaryNumber(Stream s)
        {
            int length = s.ReadByte();

            if (length < 0)
            {
                throw new EndOfStreamException();
            }

            _number = new byte[length];
            OffsetStream.StreamRead(s, _number, 0, length);
        }
コード例 #4
0
 private void ForwardData2To1()
 {
     try
     {
         OffsetStream.StreamCopy(_stream2, _stream1, BUFFER_SIZE, true);
     }
     catch
     { }
     finally
     {
         Dispose();
     }
 }
コード例 #5
0
        public Bincoding DecodeNext()
        {
            if (_lastStream != null)
            {
                if ((_lastStream.Length - _lastStream.Position) > 0)
                {
                    OffsetStream.StreamCopy(_lastStream, new NullStream());
                }

                _lastStream = null;
            }

            BincodingType type = (BincodingType)_s.ReadByte();

            switch (type)
            {
            case BincodingType.NULL:
                return(new Bincoding(type, null));

            case BincodingType.BOOLEAN:
            case BincodingType.BYTE:
            {
                byte value = (byte)_s.ReadByte();
                return(new Bincoding(type, new byte[] { value }));
            }

            case BincodingType.SHORT:
            case BincodingType.USHORT:
            {
                byte[] value = new byte[2];
                OffsetStream.StreamRead(_s, value, 0, 2);

                return(new Bincoding(type, value));
            }

            case BincodingType.INTEGER:
            case BincodingType.UINTEGER:
            {
                byte[] value = new byte[4];
                OffsetStream.StreamRead(_s, value, 0, 4);

                return(new Bincoding(type, value));
            }

            case BincodingType.LONG:
            case BincodingType.ULONG:
            case BincodingType.DATETIME:
            {
                byte[] value = new byte[8];
                OffsetStream.StreamRead(_s, value, 0, 8);

                return(new Bincoding(type, value));
            }

            case BincodingType.BINARY:
            case BincodingType.STRING:
            {
                int count = ReadLength(_s);

                byte[] value = new byte[count];
                OffsetStream.StreamRead(_s, value, 0, count);

                return(new Bincoding(type, value));
            }

            case BincodingType.STREAM:
            {
                int count = ReadLength(_s);

                _lastStream = new OffsetStream(_s, _s.Position, count, true, false);

                return(Bincoding.GetValue(_lastStream));
            }

            case BincodingType.LIST:
            {
                int count = ReadLength(_s);

                List <Bincoding> list = new List <Bincoding>(count);

                for (int i = 0; i < count; i++)
                {
                    list.Add(DecodeNext());
                }

                return(Bincoding.GetValue(list));
            }

            case BincodingType.KEY_VALUE_PAIR:
            {
                int    keyLen    = _s.ReadByte();
                byte[] keyBuffer = new byte[keyLen];
                OffsetStream.StreamRead(_s, keyBuffer, 0, keyLen);

                string    key   = Encoding.UTF8.GetString(keyBuffer, 0, keyLen);
                Bincoding value = DecodeNext();

                return(Bincoding.GetValue(new KeyValuePair <string, Bincoding>(key, value)));
            }

            case BincodingType.DICTIONARY:
            {
                int count = ReadLength(_s);

                Dictionary <string, Bincoding> dictionary = new Dictionary <string, Bincoding>(count);
                int    keyLen;
                byte[] keyBuffer = new byte[255];

                for (int i = 0; i < count; i++)
                {
                    keyLen = _s.ReadByte();
                    OffsetStream.StreamRead(_s, keyBuffer, 0, keyLen);

                    string    key   = Encoding.UTF8.GetString(keyBuffer, 0, keyLen);
                    Bincoding value = DecodeNext();

                    dictionary.Add(key, value);
                }

                return(Bincoding.GetValue(dictionary));
            }

            default:
                throw new InvalidDataException("Invalid bincoding type encountered while decoding data.");
            }
        }
コード例 #6
0
        public void Encode(Bincoding value)
        {
            _s.WriteByte((byte)value.Type);

            switch (value.Type)
            {
            case BincodingType.NULL:
                break;

            case BincodingType.BINARY:
            case BincodingType.STRING:
                WriteLength(_s, value.Value.Length);
                _s.Write(value.Value, 0, value.Value.Length);
                break;

            case BincodingType.STREAM:
                Stream stream = value.GetValueStream();

                WriteLength(_s, Convert.ToInt32(stream.Length - stream.Position));
                OffsetStream.StreamCopy(stream, _s);
                break;

            case BincodingType.LIST:
                List <Bincoding> list = value.GetList();

                WriteLength(_s, list.Count);

                foreach (Bincoding item in list)
                {
                    Encode(item);
                }

                break;

            case BincodingType.KEY_VALUE_PAIR:
                KeyValuePair <string, Bincoding> keyValue = value.GetKeyValuePair();

                byte[] keyBuffer = Encoding.UTF8.GetBytes(keyValue.Key);
                _s.WriteByte(Convert.ToByte(keyBuffer.Length));
                _s.Write(keyBuffer, 0, keyBuffer.Length);

                Encode(keyValue.Value);

                break;

            case BincodingType.DICTIONARY:
                Dictionary <string, Bincoding> dictionary = value.GetDictionary();

                WriteLength(_s, dictionary.Count);

                foreach (KeyValuePair <string, Bincoding> item in dictionary)
                {
                    Encode(item);
                }

                break;

            default:
                _s.Write(value.Value, 0, value.Value.Length);
                break;
            }
        }