Exemplo n.º 1
0
        /// <summary>
        /// Writes out part of an array of bytes.
        /// </summary>
        public void WriteRawBytes(byte[] value, int offset, int length)
        {
            if (limit - position >= length)
            {
                ByteArray.Copy(value, offset, buffer, position, length);
                // We have room in the current buffer.
                position += length;
            }
            else
            {
                // Write extends past current buffer.  Fill the rest of this buffer and
                // flush.
                int bytesWritten = limit - position;
                ByteArray.Copy(value, offset, buffer, position, bytesWritten);
                offset  += bytesWritten;
                length  -= bytesWritten;
                position = limit;
                RefreshBuffer();

                // Now deal with the rest.
                // Since we have an output stream, this is our buffer
                // and buffer offset == 0
                if (length <= limit)
                {
                    // Fits in new buffer.
                    ByteArray.Copy(value, offset, buffer, 0, length);
                    position = length;
                }
                else
                {
                    // Write is very big.  Let's do it all at once.
                    output.Write(value, offset, length);
                }
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Copies the entire byte array to the destination array provided at the offset specified.
 /// </summary>
 public void CopyTo(byte[] array, int position)
 {
     ByteArray.Copy(bytes, 0, array, position, bytes.Length);
 }
Exemplo n.º 3
0
 /// <summary>
 /// Constructs a ByteString from a portion of a byte array.
 /// </summary>
 public static ByteString CopyFrom(byte[] bytes, int offset, int count)
 {
     byte[] portion = new byte[count];
     ByteArray.Copy(bytes, offset, portion, 0, count);
     return(new ByteString(portion));
 }
Exemplo n.º 4
0
        public byte[] ReadRawBytes(int size)
        {
            if (size < 0)
            {
                throw InvalidProtocolBufferException.NegativeSize();
            }
            if (this.totalBytesRetired + this.bufferPos + size > this.currentLimit)
            {
                this.SkipRawBytes(this.currentLimit - this.totalBytesRetired - this.bufferPos);
                throw InvalidProtocolBufferException.TruncatedMessage();
            }
            if (size <= this.bufferSize - this.bufferPos)
            {
                byte[] array = new byte[size];
                ByteArray.Copy(this.buffer, this.bufferPos, array, 0, size);
                this.bufferPos += size;
                return(array);
            }
            if (size < 4096)
            {
                byte[] array2 = new byte[size];
                int    num    = this.bufferSize - this.bufferPos;
                ByteArray.Copy(this.buffer, this.bufferPos, array2, 0, num);
                this.bufferPos = this.bufferSize;
                this.RefillBuffer(true);
                while (size - num > this.bufferSize)
                {
                    Buffer.BlockCopy(this.buffer, 0, array2, num, this.bufferSize);
                    num           += this.bufferSize;
                    this.bufferPos = this.bufferSize;
                    this.RefillBuffer(true);
                }
                ByteArray.Copy(this.buffer, 0, array2, num, size - num);
                this.bufferPos = size - num;
                return(array2);
            }
            int num2 = this.bufferPos;
            int num3 = this.bufferSize;

            this.totalBytesRetired += this.bufferSize;
            this.bufferPos          = 0;
            this.bufferSize         = 0;
            int           i    = size - (num3 - num2);
            List <byte[]> list = new List <byte[]>();

            while (i > 0)
            {
                byte[] array3 = new byte[Math.Min(i, 4096)];
                int    num4;
                for (int j = 0; j < array3.Length; j += num4)
                {
                    num4 = ((this.input == null) ? -1 : this.input.Read(array3, j, array3.Length - j));
                    if (num4 <= 0)
                    {
                        throw InvalidProtocolBufferException.TruncatedMessage();
                    }
                    this.totalBytesRetired += num4;
                }
                i -= array3.Length;
                list.Add(array3);
            }
            byte[] array4 = new byte[size];
            int    num5   = num3 - num2;

            ByteArray.Copy(this.buffer, num2, array4, 0, num5);
            using (List <byte[]> .Enumerator enumerator = list.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    byte[] current = enumerator.Current;
                    Buffer.BlockCopy(current, 0, array4, num5, current.Length);
                    num5 += current.Length;
                }
            }
            return(array4);
        }
Exemplo n.º 5
0
 public static ByteString CopyFrom(byte[] bytes, int offset, int count)
 {
     byte[] dst = new byte[count];
     ByteArray.Copy(bytes, offset, dst, 0, count);
     return(new ByteString(dst));
 }