internal ExperimentalPacket(
     PacketTag		tag,
     BcpgInputStream	bcpgIn)
 {
     _tag = tag;
     _contents = bcpgIn.ReadAll();
 }
        /// <summary>Create a stream representing an old style partial object.</summary>
        /// <param name="outStr">Output stream to write to.</param>
        /// <param name="tag">The packet tag for the object.</param>
        public BcpgOutputStream(Stream outStr, PacketTag tag)
        {
            if (outStr == null)
                throw new ArgumentNullException("outStr");

            _outStr = outStr;
            this.WriteHeader(tag, true, true, 0);
        }
Ejemplo n.º 3
0
        /// <summary>Create a new style partial input stream buffered into chunks.</summary>
        /// <param name="outStr">Output stream to write to.</param>
        /// <param name="tag">Packet tag.</param>
        /// <param name="length">Size of chunks making up the packet.</param>
        public BcpgOutputStream(Stream outStr, PacketTag tag, long length)
        {
            if (outStr == null)
            {
                throw new ArgumentNullException("outStr");
            }

            _outStr = outStr;
            WriteHeader(tag, false, false, length);
        }
        /// <summary>Create a stream representing a general packet.</summary>
        /// <param name="outStr">Output stream to write to.</param>
        /// <param name="tag">Packet tag.</param>
        /// <param name="length">Size of chunks making up the packet.</param>
        /// <param name="oldFormat">If true, the header is written out in old format.</param>
        public BcpgOutputStream(Stream outStr, PacketTag tag, long length, bool oldFormat)
        {
            if (outStr == null)
                throw new ArgumentNullException("outStr");

            _outStr = outStr;

            if (length > 0xFFFFFFFFL)
            {
                this.WriteHeader(tag, false, true, 0);
                _partialBufferLength = 1 << BufferSizePower;
                _partialBuffer = new byte[_partialBufferLength];
                _partialPower = BufferSizePower;
                _partialOffset = 0;
            }
            else
            {
                this.WriteHeader(tag, oldFormat, false, length);
            }
        }
Ejemplo n.º 5
0
        /// <summary>Create a new style partial input stream buffered into chunks.</summary>
        /// <param name="outStr">Output stream to write to.</param>
        /// <param name="tag">Packet tag.</param>
        /// <param name="buffer">Buffer to use for collecting chunks.</param>
        public BcpgOutputStream(Stream outStr, PacketTag tag, byte[] buffer)
        {
            if (outStr == null)
            {
                throw new ArgumentNullException("outStr");
            }

            _outStr = outStr;
            WriteHeader(tag, false, true, 0);

            _partialBuffer = buffer;

            var length = (uint) _partialBuffer.Length;
            for (_partialPower = 0; length != 1; _partialPower++)
            {
                length >>= 1;
            }

            if (_partialPower > 30)
            {
                throw new IOException("Buffer cannot be greater than 2^30 in length.");
            }
            _partialBufferLength = 1 << _partialPower;
            _partialOffset = 0;
        }
Ejemplo n.º 6
0
 internal void WritePacket(PacketTag tag, byte[] body, bool oldFormat)
 {
     WriteHeader(tag, oldFormat, false, body.Length);
     Write(body);
 }
Ejemplo n.º 7
0
        private void WriteHeader(PacketTag tag, bool oldPackets, bool partial, long bodyLen)
        {
            int hdr = 0x80;

            if (_partialBuffer != null)
            {
                PartialFlush(true);
                _partialBuffer = null;
            }

            if (oldPackets)
            {
                hdr |= ((int) tag) << 2;

                if (partial)
                {
                    WriteByte((byte) (hdr | 0x03));
                }
                else
                {
                    if (bodyLen <= 0xff)
                    {
                        WriteByte((byte) hdr);
                        WriteByte((byte) bodyLen);
                    }
                    else if (bodyLen <= 0xffff)
                    {
                        WriteByte((byte) (hdr | 0x01));
                        WriteByte((byte) (bodyLen >> 8));
                        WriteByte((byte) (bodyLen));
                    }
                    else
                    {
                        WriteByte((byte) (hdr | 0x02));
                        WriteByte((byte) (bodyLen >> 24));
                        WriteByte((byte) (bodyLen >> 16));
                        WriteByte((byte) (bodyLen >> 8));
                        WriteByte((byte) bodyLen);
                    }
                }
            }
            else
            {
                hdr |= 0x40 | (int) tag;
                WriteByte((byte) hdr);

                if (partial)
                {
                    _partialOffset = 0;
                }
                else
                {
                    WriteNewPacketLength(bodyLen);
                }
            }
        }
 public void WritePacket(PacketTag tag, byte[] body, bool oldFormat)
 {
     this.WriteHeader(tag, oldFormat, false, body.Length);
     this.Write(body);
 }