BlockCopy() public static method

public static BlockCopy ( byte from, int fromIndex, byte to, int toIndex, int count ) : void
from byte
fromIndex int
to byte
toIndex int
count int
return void
Beispiel #1
0
 // Token: 0x06003274 RID: 12916 RVA: 0x00126FC8 File Offset: 0x001253C8
 void IExtension.EndAppend(Stream stream, bool commit)
 {
     try
     {
         int num;
         if (commit && (num = (int)stream.Length) > 0)
         {
             MemoryStream memoryStream = (MemoryStream)stream;
             if (this.buffer == null)
             {
                 this.buffer = memoryStream.ToArray();
             }
             else
             {
                 int    num2 = this.buffer.Length;
                 byte[] to   = new byte[num2 + num];
                 Helpers.BlockCopy(this.buffer, 0, to, 0, num2);
                 Helpers.BlockCopy(Helpers.GetBuffer(memoryStream), 0, to, num2, num);
                 this.buffer = to;
             }
         }
     }
     finally
     {
         if (stream != null)
         {
             ((IDisposable)stream).Dispose();
         }
     }
 }
Beispiel #2
0
        internal static void ResizeAndFlushLeft(ref byte[] buffer, int toFitAtLeastBytes, int copyFromIndex, int copyBytes)
        {
            Helpers.DebugAssert(buffer != null);
            Helpers.DebugAssert(toFitAtLeastBytes > buffer.Length);
            Helpers.DebugAssert(copyFromIndex >= 0);
            Helpers.DebugAssert(copyBytes >= 0);

            // try doubling, else match
            int newLength = buffer.Length * 2;

            if (newLength < toFitAtLeastBytes)
            {
                newLength = toFitAtLeastBytes;
            }

            byte[] newBuffer = new byte[newLength];
            if (copyBytes > 0)
            {
                Helpers.BlockCopy(buffer, copyFromIndex, newBuffer, 0, copyBytes);
            }
            if (buffer.Length == BufferPool.BufferLength)
            {
                BufferPool.ReleaseBufferToPool(ref buffer);
            }
            buffer = newBuffer;
        }
Beispiel #3
0
        /// <summary>
        /// Writes a string to the stream; supported wire-types: String
        /// </summary>
        public static void WriteString(string value, ProtoWriter writer)
        {
            if (writer.wireType != WireType.String)
            {
                throw CreateException(writer);
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");                // written header; now what?
            }
            int len = value.Length;

            if (len == 0)
            {
                WriteUInt32Variant(0, writer);
                writer.wireType = WireType.None;
                return; // just a header
            }
#if MF
            byte[] bytes  = encoding.GetBytes(value);
            int    actual = bytes.Length;
            writer.WriteUInt32Variant((uint)actual);
            writer.Ensure(actual);
            Helpers.BlockCopy(bytes, 0, writer.ioBuffer, writer.ioIndex, actual);
#else
            int predicted = encoding.GetByteCount(value);
            WriteUInt32Variant((uint)predicted, writer);
            DemandSpace(predicted, writer);
            int actual = encoding.GetBytes(value, 0, value.Length, writer.ioBuffer, writer.ioIndex);
            Helpers.DebugAssert(predicted == actual);
#endif
            IncrementedAndReset(actual, writer);
        }
 void IExtension.EndAppend(Stream stream, bool commit)
 {
     using (stream)
     {
         int num;
         if (commit && (num = (int)stream.Length) > 0)
         {
             using (MemoryStream memoryStream = (MemoryStream)stream)
             {
                 if (buffer == null)
                 {
                     buffer = memoryStream.ToArray();
                 }
                 else
                 {
                     int    num2 = buffer.Length;
                     byte[] to   = new byte[num2 + num];
                     Helpers.BlockCopy(buffer, 0, to, 0, num2);
                     Helpers.BlockCopy(memoryStream.GetBuffer(), 0, to, num2, num);
                     buffer = to;
                 }
             }
         }
     }
 }
Beispiel #5
0
        internal static void ResizeAndFlushLeft(ref byte[] buffer, int toFitAtLeastBytes, int copyFromIndex, int copyBytes)
        {
            Helpers.DebugAssert(buffer != null);
            Helpers.DebugAssert(toFitAtLeastBytes > buffer.Length);
            Helpers.DebugAssert(copyFromIndex >= 0);
            Helpers.DebugAssert(copyBytes >= 0);

            // try doubling, else match
            int newLength = buffer.Length * 2;

            if (newLength < toFitAtLeastBytes)
            {
                newLength = toFitAtLeastBytes;
            }

            try
            {
                if (copyBytes == 0)
                {
                    BufferPool.ReleaseBufferToPool(ref buffer); // No need to copy, we can release immediately
                }

                byte[] newBuffer = GetCachedBuffer(toFitAtLeastBytes);
                if (newBuffer == null)
                {
                    newBuffer = new byte[newLength];
                }

                if (copyBytes > 0)
                {
                    Helpers.BlockCopy(buffer, copyFromIndex, newBuffer, 0, copyBytes);
                    BufferPool.ReleaseBufferToPool(ref buffer);
                }

                buffer = newBuffer;
            }
            catch (OutOfMemoryException)
            {
                // Low memory situation: flush existing buffers, save current to disk, allocate new one and read data back
                string tempPath = Path.GetTempFileName();
                try
                {
                    Flush();
                    File.WriteAllBytes(tempPath, buffer);   // Write the current buffer to disk to be able to release the current buffer
                    buffer = null;
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    buffer = new byte[(newLength + toFitAtLeastBytes) / 2];  // A bit more conservative resizing
                    using (FileStream stream = File.OpenRead(tempPath))
                    {
                        stream.Read(buffer, 0, copyBytes);  // Read data back from disk
                    }
                }
                finally
                {
                    try { File.Delete(tempPath); }  // Remove temporary file
                    catch { }
                }
            }
        }
Beispiel #6
0
        internal static void ResizeAndFlushLeft(ref byte[] buffer, int toFitAtLeastBytes, int copyFromIndex, int copyBytes)
        {
            Helpers.DebugAssert(buffer != null);
            Helpers.DebugAssert(toFitAtLeastBytes > buffer.Length);
            Helpers.DebugAssert(copyFromIndex >= 0);
            Helpers.DebugAssert(copyBytes >= 0);

            int newLength = buffer.Length * 2;

            if (newLength < 0)
            {
                newLength = MaxByteArraySize;
            }

            if (newLength < toFitAtLeastBytes)
            {
                newLength = toFitAtLeastBytes;
            }

            if (copyBytes == 0)
            {
                ReleaseBufferToPool(ref buffer);
            }

            var newBuffer = GetCachedBuffer(toFitAtLeastBytes) ?? new byte[newLength];

            if (copyBytes > 0)
            {
                Helpers.BlockCopy(buffer, copyFromIndex, newBuffer, 0, copyBytes);
                ReleaseBufferToPool(ref buffer);
            }

            buffer = newBuffer;
        }
Beispiel #7
0
 void ProtoBuf.IExtension.EndAppend(Stream stream, bool commit)
 {
     using (stream)
     {
         if (commit)
         {
             int length = (int)stream.Length;
             int num    = length;
             if (length > 0)
             {
                 MemoryStream memoryStream = (MemoryStream)stream;
                 if (this.buffer != null)
                 {
                     int    length1  = (int)this.buffer.Length;
                     byte[] numArray = new byte[length1 + num];
                     Helpers.BlockCopy(this.buffer, 0, numArray, 0, length1);
                     Helpers.BlockCopy(memoryStream.GetBuffer(), 0, numArray, length1, num);
                     this.buffer = numArray;
                 }
                 else
                 {
                     this.buffer = memoryStream.ToArray();
                 }
             }
         }
     }
 }
        void IExtension.EndAppend(Stream stream, bool commit)
        {
            using (stream)
            {
                int len;
                if (commit && (len = (int)stream.Length) > 0)
                {
                    MemoryStream ms = (MemoryStream)stream;

                    if (buffer == null)
                    {   // allocate new buffer
                        buffer = ms.ToArray();
                    }
                    else
                    {   // resize and copy the data
                        // note: Array.Resize not available on CF
                        int    offset = buffer.Length;
                        byte[] tmp    = new byte[offset + len];
                        Helpers.BlockCopy(buffer, 0, tmp, 0, offset);
                        Helpers.BlockCopy(ms.GetBuffer(), 0, tmp, offset, len);
                        buffer = tmp;
                    }
                }
            }
        }
Beispiel #9
0
        public static byte[] AppendBytes(byte[] value, ProtoReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            WireType wireType = reader.wireType;

            if (wireType == WireType.String)
            {
                int num = (int)reader.ReadUInt32Variant(trimNegative: false);
                reader.wireType = WireType.None;
                if (num == 0)
                {
                    if (value != null)
                    {
                        return(value);
                    }
                    return(EmptyBlob);
                }
                int num2;
                if (value == null || value.Length == 0)
                {
                    num2  = 0;
                    value = new byte[num];
                }
                else
                {
                    num2 = value.Length;
                    byte[] array = new byte[value.Length + num];
                    Helpers.BlockCopy(value, 0, array, 0, value.Length);
                    value = array;
                }
                reader.position += num;
                while (num > reader.available)
                {
                    if (reader.available > 0)
                    {
                        Helpers.BlockCopy(reader.ioBuffer, reader.ioIndex, value, num2, reader.available);
                        num           -= reader.available;
                        num2          += reader.available;
                        reader.ioIndex = (reader.available = 0);
                    }
                    int num3 = (num > reader.ioBuffer.Length) ? reader.ioBuffer.Length : num;
                    if (num3 > 0)
                    {
                        reader.Ensure(num3, strict: true);
                    }
                }
                if (num > 0)
                {
                    Helpers.BlockCopy(reader.ioBuffer, reader.ioIndex, value, num2, num);
                    reader.ioIndex   += num;
                    reader.available -= num;
                }
                return(value);
            }
            throw reader.CreateWireTypeException();
        }
Beispiel #10
0
        /// <summary>
        /// Writes a byte-array to the stream; supported wire-types: String
        /// </summary>
        public static void WriteBytes(byte[] data, int offset, int length, ProtoWriter writer)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            switch (writer.wireType)
            {
            case WireType.Fixed32:
                if (length != 4)
                {
                    throw new ArgumentException("length");
                }
                goto CopyFixedLength;      // ugly but effective

            case WireType.Fixed64:
                if (length != 8)
                {
                    throw new ArgumentException("length");
                }
                goto CopyFixedLength;      // ugly but effective

            case WireType.String:
                WriteUInt32Variant((uint)length, writer);
                writer.wireType = WireType.None;
                if (length == 0)
                {
                    return;
                }
                if (writer.flushLock != 0 || length <= writer.ioBuffer.Length) // write to the buffer
                {
                    goto CopyFixedLength;                                      // ugly but effective
                }
                // writing data that is bigger than the buffer (and the buffer
                // isn't currently locked due to a sub-object needing the size backfilled)
                Flush(writer);     // commit any existing data from the buffer
                // now just write directly to the underlying stream
                                        #if OPTIMIZATION_NETWORK
                writer._destBuf.In(data, (UInt32)offset, (UInt32)length);
                                        #else
                writer.dest.Write(data, offset, length);
                                        #endif

                writer.position += length;     // since we've flushed offset etc is 0, and remains
                // zero since we're writing directly to the stream
                return;
            }
            throw CreateException(writer);
CopyFixedLength:         // no point duplicating this lots of times, and don't really want another stackframe
            DemandSpace(length, writer);
            Helpers.BlockCopy(data, offset, writer.ioBuffer, writer.ioIndex, length);
            IncrementedAndReset(length, writer);
        }
Beispiel #11
0
        public static byte[] AppendBytes(byte[] value, ProtoReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            WireType wireType = reader.wireType;

            if (wireType != WireType.String)
            {
                throw reader.CreateWireTypeException();
            }
            int i = (int)reader.ReadUInt32Variant(false);

            reader.wireType = WireType.None;
            if (i == 0)
            {
                return((value != null) ? value : ProtoReader.EmptyBlob);
            }
            int num;

            if (value == null || value.Length == 0)
            {
                num   = 0;
                value = new byte[i];
            }
            else
            {
                num = value.Length;
                byte[] array = new byte[value.Length + i];
                Helpers.BlockCopy(value, 0, array, 0, value.Length);
                value = array;
            }
            reader.position += i;
            while (i > reader.available)
            {
                if (reader.available > 0)
                {
                    Helpers.BlockCopy(reader.ioBuffer, reader.ioIndex, value, num, reader.available);
                    i             -= reader.available;
                    num           += reader.available;
                    reader.ioIndex = (reader.available = 0);
                }
                int num2 = (i <= reader.ioBuffer.Length) ? i : reader.ioBuffer.Length;
                if (num2 > 0)
                {
                    reader.Ensure(num2, true);
                }
            }
            if (i > 0)
            {
                Helpers.BlockCopy(reader.ioBuffer, reader.ioIndex, value, num, i);
                reader.ioIndex   += i;
                reader.available -= i;
            }
            return(value);
        }
Beispiel #12
0
        /// <summary>
        /// Reads a byte-sequence from the stream, appending them to an existing byte-sequence (which can be null); supported wire-types: String
        /// </summary>
        public static byte[] AppendBytes(byte[] value, ProtoReader reader)
        {
            switch (reader.wireType)
            {
            case WireType.String:
                int len = (int)reader.ReadUInt32Variant(false);
                reader.wireType = WireType.None;
                if (len == 0)
                {
                    return(value);
                }
                int offset;
                if (value == null || value.Length == 0)
                {
                    offset = 0;
                    value  = new byte[len];
                }
                else
                {
                    offset = value.Length;
                    byte[] tmp = new byte[value.Length + len];
                    Helpers.BlockCopy(value, 0, tmp, 0, value.Length);
                    value = tmp;
                }
                // value is now sized with the final length, and (if necessary)
                // contains the old data up to "offset"
                reader.position += len;     // assume success
                while (len > reader.available)
                {
                    if (reader.available > 0)
                    {
                        // copy what we *do* have
                        Helpers.BlockCopy(reader.ioBuffer, reader.ioIndex, value, offset, reader.available);
                        len           -= reader.available;
                        offset        += reader.available;
                        reader.ioIndex = reader.available = 0;     // we've drained the buffer
                    }
                    //  now refill the buffer (without overflowing it)
                    int count = len > reader.ioBuffer.Length ? reader.ioBuffer.Length : len;
                    if (count > 0)
                    {
                        reader.Ensure(count, true);
                    }
                }
                // at this point, we know that len <= available
                if (len > 0)
                {       // still need data, but we have enough buffered
                    Helpers.BlockCopy(reader.ioBuffer, reader.ioIndex, value, offset, len);
                    reader.ioIndex   += len;
                    reader.available -= len;
                }
                return(value);

            default:
                throw reader.CreateException();
            }
        }
Beispiel #13
0
        public static void WriteBytes(byte[] data, int offset, int length, ProtoWriter writer)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            switch (writer.wireType)
            {
            case WireType.Fixed32:
                if (length == 4)
                {
                    break;
                }
                throw new ArgumentException("length");

            case WireType.Fixed64:
                if (length == 8)
                {
                    break;
                }
                throw new ArgumentException("length");

            case WireType.String:
                WriteUInt32Variant((uint)length, writer);
                writer.wireType = WireType.None;
                if (length != 0)
                {
                    if (writer.flushLock != 0)
                    {
                        break;
                    }
                    if (length <= writer.ioBuffer.Length)
                    {
                        break;
                    }
                    Flush(writer);
                    writer.dest.Write(data, offset, length);
                    writer.position += length;
                }
                return;

            default:
                throw CreateException(writer);
            }
            DemandSpace(length, writer);
            Helpers.BlockCopy(data, offset, writer.ioBuffer, writer.ioIndex, length);
            IncrementedAndReset(length, writer);
        }
Beispiel #14
0
        void IExtension.EndAppend(Stream stream, bool commit)
        {
            using (stream)
            {
                int len;
                if (commit && (len = (int)stream.Length) > 0)
                {
                    MemoryStream ms = (MemoryStream)stream;

                    if (buffer == null)
                    {   // allocate new buffer
                        buffer = ms.ToArray();
                    }
                    else
                    {   // resize and copy the data
                        // note: Array.Resize not available on CF
                        int    offset = buffer.Length;
                        byte[] tmp    = new byte[offset + len];
                        Helpers.BlockCopy(buffer, 0, tmp, 0, offset);

#if PORTABLE || WINRT // no GetBuffer() - fine, we'll use Read instead
                        int  bytesRead;
                        long oldPos = ms.Position;
                        ms.Position = 0;
                        while (len > 0 && (bytesRead = ms.Read(tmp, offset, len)) > 0)
                        {
                            len    -= bytesRead;
                            offset += bytesRead;
                        }
                        if (len != 0)
                        {
                            throw new EndOfStreamException();
                        }
                        ms.Position = oldPos;
#elif DNXCORE50
                        ArraySegment <byte> segment;
                        if (ms.TryGetBuffer(out segment))
                        {
                            Helpers.BlockCopy(segment.Array, segment.Offset, tmp, offset, len);
                        }
                        else
                        {
                            throw new InvalidOperationException("Could not obtain underlying MemoryStream buffer");
                        }
#else
                        Helpers.BlockCopy(ms.GetBuffer(), 0, tmp, offset, len);
#endif
                        buffer = tmp;
                    }
                }
            }
        }
        public static void ResizeAndFlushLeft(ref byte[] buffer, int toFitAtLeastBytes, int copyFromIndex, int copyBytes)
        {
            Helpers.DebugAssert(buffer != null);
            Helpers.DebugAssert(toFitAtLeastBytes > buffer.Length);
            Helpers.DebugAssert(copyFromIndex >= 0);
            Helpers.DebugAssert(copyBytes >= 0);

            byte[] newBuffer = GetBuffer(toFitAtLeastBytes);
            if (copyBytes > 0)
            {
                Helpers.BlockCopy(buffer, copyFromIndex, newBuffer, 0, copyBytes);
            }
            ReleaseBufferToPool(ref buffer);
            buffer = newBuffer;
        }
Beispiel #16
0
        // Token: 0x060034C3 RID: 13507 RVA: 0x001320A0 File Offset: 0x001304A0
        public static void WriteBytes(byte[] data, int offset, int length, ProtoWriter writer)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            WireType wireType = writer.wireType;

            if (wireType != WireType.Fixed32)
            {
                if (wireType != WireType.Fixed64)
                {
                    if (wireType != WireType.String)
                    {
                        throw ProtoWriter.CreateException(writer);
                    }
                    ProtoWriter.WriteUInt32Variant((uint)length, writer);
                    writer.wireType = WireType.None;
                    if (length == 0)
                    {
                        return;
                    }
                    if (writer.flushLock == 0 && length > writer.ioBuffer.Length)
                    {
                        ProtoWriter.Flush(writer);
                        writer.dest.Write(data, offset, length);
                        writer.position += length;
                        return;
                    }
                }
                else if (length != 8)
                {
                    throw new ArgumentException("length");
                }
            }
            else if (length != 4)
            {
                throw new ArgumentException("length");
            }
            ProtoWriter.DemandSpace(length, writer);
            Helpers.BlockCopy(data, offset, writer.ioBuffer, writer.ioIndex, length);
            ProtoWriter.IncrementedAndReset(length, writer);
        }
        internal static void ResizeAndFlushLeft(ref byte[] buffer, int toFitAtLeastBytes, int copyFromIndex, int copyBytes)
        {
            int length = (int)buffer.Length * 2;

            if (length < toFitAtLeastBytes)
            {
                length = toFitAtLeastBytes;
            }
            byte[] numArray = new byte[length];
            if (copyBytes > 0)
            {
                Helpers.BlockCopy(buffer, copyFromIndex, numArray, 0, copyBytes);
            }
            if ((int)buffer.Length == 1024)
            {
                BufferPool.ReleaseBufferToPool(ref buffer);
            }
            buffer = numArray;
        }
Beispiel #18
0
        internal static void ResizeAndFlushLeft(ref byte[] buffer, int toFitAtLeastBytes, int copyFromIndex, int copyBytes)
        {
            int num = buffer.Length * 2;

            if (num < toFitAtLeastBytes)
            {
                num = toFitAtLeastBytes;
            }
            byte[] array = new byte[num];
            if (copyBytes > 0)
            {
                Helpers.BlockCopy(buffer, copyFromIndex, array, 0, copyBytes);
            }
            if (buffer.Length == 1024)
            {
                BufferPool.ReleaseBufferToPool(ref buffer);
            }
            buffer = array;
        }
Beispiel #19
0
        internal void Ensure(int count, bool strict)
        {
            if (count > ioBuffer.Length)
            {
                BufferPool.ResizeAndFlushLeft(ref ioBuffer, count, ioIndex, available);
                ioIndex = 0;
            }
            else if (ioIndex + count >= ioBuffer.Length)
            {
                Helpers.BlockCopy(ioBuffer, ioIndex, ioBuffer, 0, available);
                ioIndex = 0;
            }
            count -= available;
            int num  = ioIndex + available;
            int num2 = ioBuffer.Length - num;

            if (isFixedLength && dataRemaining < num2)
            {
                num2 = dataRemaining;
            }
            int num3;

            while (count > 0 && num2 > 0 && (num3 = source.Read(ioBuffer, num, num2)) > 0)
            {
                available += num3;
                count     -= num3;
                num2      -= num3;
                num       += num3;
                if (isFixedLength)
                {
                    dataRemaining -= num3;
                }
            }
            if (!strict)
            {
                return;
            }
            if (count <= 0)
            {
                return;
            }
            throw EoF(this);
        }
Beispiel #20
0
        internal void Ensure(int count, bool strict)
        {
            Helpers.DebugAssert(available <= count, "Asking for data without checking first");
            if (count > ioBuffer.Length)
            {
                BufferPool.ResizeAndFlushLeft(ref ioBuffer, count, ioIndex, available);
                ioIndex = 0;
            }
            else if (ioIndex + count >= ioBuffer.Length)
            {
                // need to shift the buffer data to the left to make space
                Helpers.BlockCopy(ioBuffer, ioIndex, ioBuffer, 0, available);
                ioIndex = 0;
            }
            count -= available;
            int writePos = ioIndex + available, bytesRead;
            int canRead = ioBuffer.Length - writePos;

            if (isFixedLength)
            {   // throttle it if needed
                if (dataRemaining < canRead)
                {
                    canRead = dataRemaining;
                }
            }
            while (count > 0 && (bytesRead = source.Read(ioBuffer, writePos, canRead)) > 0)
            {
                available += bytesRead;
                count     -= bytesRead;
                canRead   -= bytesRead;
                writePos  += bytesRead;
                if (isFixedLength)
                {
                    dataRemaining -= bytesRead;
                }
            }
            if (strict && count > 0)
            {
                throw EoF(this);
            }
        }
Beispiel #21
0
        internal static void ResizeAndFlushLeft(ref byte[] buffer, int toFitAtLeastBytes, int copyFromIndex, int copyBytes)
        {
            Helpers.DebugAssert(buffer != null);
            Helpers.DebugAssert(toFitAtLeastBytes > buffer.Length);
            Helpers.DebugAssert(copyFromIndex >= 0);
            Helpers.DebugAssert(copyBytes >= 0);

            // try doubling, else match
            int newLength = buffer.Length * 2;

            if (newLength < 0)
            {
                newLength = MaxByteArraySize;
            }

            if (newLength < toFitAtLeastBytes)
            {
                newLength = toFitAtLeastBytes;
            }

            //byte[] newBuffer = new byte[newLength];
            //if (copyBytes > 0)
            //{
            //  Helpers.BlockCopy(buffer, copyFromIndex, newBuffer, 0, copyBytes);
            //}
            //if (buffer.Length == BufferPool.BufferLength)
            //{
            //  BufferPool.ReleaseBufferToPool(ref buffer);
            //}
            //buffer = newBuffer;
            byte[] newBuffer = BufferManager.TakeBuffer(newLength);
            if (copyBytes > 0)
            {
                Helpers.BlockCopy(buffer, copyFromIndex, newBuffer, 0, copyBytes);
            }
            BufferManager.ReturnBuffer(buffer);
            buffer = newBuffer;
        }
Beispiel #22
0
        internal void Ensure(int count, bool strict)
        {
            if (count > this.ioBuffer.Length)
            {
                BufferPool.ResizeAndFlushLeft(ref this.ioBuffer, count, this.ioIndex, this.available);
                this.ioIndex = 0;
            }
            else if (this.ioIndex + count >= this.ioBuffer.Length)
            {
                Helpers.BlockCopy(this.ioBuffer, this.ioIndex, this.ioBuffer, 0, this.available);
                this.ioIndex = 0;
            }
            count -= this.available;
            int num  = this.ioIndex + this.available;
            int num2 = this.ioBuffer.Length - num;

            if (this.isFixedLength && this.dataRemaining < num2)
            {
                num2 = this.dataRemaining;
            }
            int num3;

            while (count > 0 && num2 > 0 && (num3 = this.source.Read(this.ioBuffer, num, num2)) > 0)
            {
                this.available += num3;
                count          -= num3;
                num2           -= num3;
                num            += num3;
                if (this.isFixedLength)
                {
                    this.dataRemaining -= num3;
                }
            }
            if (strict && count > 0)
            {
                throw ProtoReader.EoF(this);
            }
        }
Beispiel #23
0
        /// <summary>
        /// Reads a string from the stream (using UTF8); supported wire-types: String
        /// </summary>
        public string ReadString()
        {
            if (wireType == WireType.String)
            {
                int bytes = (int)ReadUInt32Variant(false);
                if (bytes == 0)
                {
                    return("");
                }
                if (available < bytes)
                {
                    Ensure(bytes, true);
                }
#if MF
                byte[] tmp;
                if (ioIndex == 0 && bytes == ioBuffer.Length)
                {
                    // unlikely, but...
                    tmp = ioBuffer;
                }
                else
                {
                    tmp = new byte[bytes];
                    Helpers.BlockCopy(ioBuffer, ioIndex, tmp, 0, bytes);
                }
                string s = new string(encoding.GetChars(tmp));
#else
                string s = encoding.GetString(ioBuffer, ioIndex, bytes);
#endif
                s          = Intern(s);
                available -= bytes;
                position  += bytes;
                ioIndex   += bytes;
                return(s);
            }
            throw CreateException();
        }
        private static void EndSubItem(SubItemToken token, ProtoWriter writer, PrefixStyle style)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writer.wireType != WireType.None)
            {
                throw CreateException(writer);
            }
            int value = token.value;

            if (writer.depth <= 0)
            {
                throw CreateException(writer);
            }
            if (writer.depth-- > RecursionCheckDepth)
            {
                writer.PopRecursionStack();
            }
            writer.packedFieldNumber = 0; // ending the sub-item always wipes packed encoding
            if (value < 0)
            {                             // group - very simple append
                WriteHeaderCore(-value, WireType.EndGroup, writer);
                writer.wireType = WireType.None;
                return;
            }

            // so we're backfilling the length into an existing sequence
            int len;

            switch (style)
            {
            case PrefixStyle.Fixed32:
                len = (int)((writer.ioIndex - value) - 4);
                ProtoWriter.WriteInt32ToBuffer(len, writer.ioBuffer, value);
                break;

            case PrefixStyle.Fixed32BigEndian:
                len = (int)((writer.ioIndex - value) - 4);
                byte[] buffer = writer.ioBuffer;
                ProtoWriter.WriteInt32ToBuffer(len, buffer, value);
                // and swap the byte order
                byte b = buffer[value];
                buffer[value]     = buffer[value + 3];
                buffer[value + 3] = b;
                b = buffer[value + 1];
                buffer[value + 1] = buffer[value + 2];
                buffer[value + 2] = b;
                break;

            case PrefixStyle.Base128:
                // string - complicated because we only reserved one byte;
                // if the prefix turns out to need more than this then
                // we need to shuffle the existing data
                len = (int)((writer.ioIndex - value) - 1);
                int  offset = 0;
                uint tmp    = (uint)len;
                while ((tmp >>= 7) != 0)
                {
                    offset++;
                }
                if (offset == 0)
                {
                    writer.ioBuffer[value] = (byte)(len & 0x7F);
                }
                else
                {
                    DemandSpace(offset, writer);
                    byte[] blob = writer.ioBuffer;
                    Helpers.BlockCopy(blob, value + 1, blob, value + 1 + offset, len);
                    tmp = (uint)len;
                    do
                    {
                        blob[value++] = (byte)((tmp & 0x7F) | 0x80);
                    } while ((tmp >>= 7) != 0);
                    blob[value - 1]  = (byte)(blob[value - 1] & ~0x80);
                    writer.position += offset;
                    writer.ioIndex  += offset;
                }
                break;

            default:
                throw new ArgumentOutOfRangeException("style");
            }
            // and this object is no longer a blockage - also flush if sensible
            const int ADVISORY_FLUSH_SIZE = 1024;

            if (--writer.flushLock == 0 && writer.ioIndex >= ADVISORY_FLUSH_SIZE)
            {
                ProtoWriter.Flush(writer);
            }
        }
Beispiel #25
0
        private static void EndSubItem(SubItemToken token, ProtoWriter writer, PrefixStyle style)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writer.wireType != WireType.None)
            {
                throw CreateException(writer);
            }
            int num = token.value;

            if (writer.depth <= 0)
            {
                throw CreateException(writer);
            }
            if (writer.depth-- > 25)
            {
                writer.PopRecursionStack();
            }
            writer.packedFieldNumber = 0;
            if (num < 0)
            {
                WriteHeaderCore(-num, WireType.EndGroup, writer);
                writer.wireType = WireType.None;
            }
            else
            {
                switch (style)
                {
                case PrefixStyle.Fixed32:
                {
                    int num2 = writer.ioIndex - num - 4;
                    WriteInt32ToBuffer(num2, writer.ioBuffer, num);
                    break;
                }

                case PrefixStyle.Fixed32BigEndian:
                {
                    int    num2   = writer.ioIndex - num - 4;
                    byte[] array2 = writer.ioBuffer;
                    WriteInt32ToBuffer(num2, array2, num);
                    byte b = array2[num];
                    array2[num]     = array2[num + 3];
                    array2[num + 3] = b;
                    b = array2[num + 1];
                    array2[num + 1] = array2[num + 2];
                    array2[num + 2] = b;
                    break;
                }

                case PrefixStyle.Base128:
                {
                    int  num2 = writer.ioIndex - num - 1;
                    int  num3 = 0;
                    uint num4 = (uint)num2;
                    while ((num4 >>= 7) != 0)
                    {
                        num3++;
                    }
                    if (num3 == 0)
                    {
                        writer.ioBuffer[num] = (byte)(num2 & 0x7F);
                    }
                    else
                    {
                        DemandSpace(num3, writer);
                        byte[] array = writer.ioBuffer;
                        Helpers.BlockCopy(array, num + 1, array, num + 1 + num3, num2);
                        num4 = (uint)num2;
                        do
                        {
                            array[num++] = (byte)((num4 & 0x7F) | 0x80);
                        }while ((num4 >>= 7) != 0);
                        array[num - 1]   = (byte)(array[num - 1] & -129);
                        writer.position += num3;
                        writer.ioIndex  += num3;
                    }
                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException("style");
                }
                if (--writer.flushLock == 0 && writer.ioIndex >= 1024)
                {
                    Flush(writer);
                }
            }
        }
Beispiel #26
0
        private static void EndSubItem(SubItemToken token, ProtoWriter writer, PrefixStyle style)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writer.wireType != WireType.None)
            {
                throw ProtoWriter.CreateException(writer);
            }
            int value = token.value;

            if (writer.depth <= 0)
            {
                throw ProtoWriter.CreateException(writer);
            }
            if (writer.depth-- > 25)
            {
                writer.PopRecursionStack();
            }
            writer.packedFieldNumber = 0;
            if (value < 0)
            {
                ProtoWriter.WriteHeaderCore(-value, WireType.EndGroup, writer);
                writer.wireType = WireType.None;
                return;
            }
            switch (style)
            {
            case PrefixStyle.Base128:
            {
                int  num  = writer.ioIndex - value - 1;
                int  num2 = 0;
                uint num3 = (uint)num;
                while ((num3 >>= 7) != 0u)
                {
                    num2++;
                }
                if (num2 == 0)
                {
                    writer.ioBuffer[value] = (byte)(num & 127);
                }
                else
                {
                    ProtoWriter.DemandSpace(num2, writer);
                    byte[] array = writer.ioBuffer;
                    Helpers.BlockCopy(array, value + 1, array, value + 1 + num2, num);
                    num3 = (uint)num;
                    do
                    {
                        array[value++] = (byte)((num3 & 127u) | 128u);
                    }while ((num3 >>= 7) != 0u);
                    array[value - 1] = (byte)((int)array[value - 1] & -129);
                    writer.position += num2;
                    writer.ioIndex  += num2;
                }
                break;
            }

            case PrefixStyle.Fixed32:
            {
                int num = writer.ioIndex - value - 4;
                ProtoWriter.WriteInt32ToBuffer(num, writer.ioBuffer, value);
                break;
            }

            case PrefixStyle.Fixed32BigEndian:
            {
                int    num    = writer.ioIndex - value - 4;
                byte[] array2 = writer.ioBuffer;
                ProtoWriter.WriteInt32ToBuffer(num, array2, value);
                byte b = array2[value];
                array2[value]     = array2[value + 3];
                array2[value + 3] = b;
                b = array2[value + 1];
                array2[value + 1] = array2[value + 2];
                array2[value + 2] = b;
                break;
            }

            default:
                throw new ArgumentOutOfRangeException("style");
            }
            if (--writer.flushLock == 0 && writer.ioIndex >= 1024)
            {
                ProtoWriter.Flush(writer);
            }
        }
        private static void EndSubItem(SubItemToken token, ProtoWriter writer, PrefixStyle style)
        {
            int    num;
            UInt32 num1;

            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writer.wireType != ProtoBuf.WireType.None)
            {
                throw ProtoWriter.CreateException(writer);
            }
            int num2 = token.@value;

            if (writer.depth <= 0)
            {
                throw ProtoWriter.CreateException(writer);
            }
            ProtoWriter protoWriter = writer;
            int         num3        = protoWriter.depth;
            int         num4        = num3;

            protoWriter.depth = num3 - 1;
            if (num4 > 25)
            {
                writer.PopRecursionStack();
            }
            writer.packedFieldNumber = 0;
            if (num2 < 0)
            {
                ProtoWriter.WriteHeaderCore(-num2, ProtoBuf.WireType.EndGroup, writer);
                writer.wireType = ProtoBuf.WireType.None;
                return;
            }
            switch (style)
            {
            case PrefixStyle.Base128:
            {
                num = writer.ioIndex - num2 - 1;
                int  num5 = 0;
                uint num6 = (uint)num;
                while (true)
                {
                    UInt32 num7 = num6 >> 7;
                    num6 = num7;
                    if (num7 == 0)
                    {
                        break;
                    }
                    num5++;
                }
                if (num5 != 0)
                {
                    ProtoWriter.DemandSpace(num5, writer);
                    byte[] numArray = writer.ioBuffer;
                    Helpers.BlockCopy(numArray, num2 + 1, numArray, num2 + 1 + num5, num);
                    num6 = (uint)num;
                    do
                    {
                        int num8 = num2;
                        num2           = num8 + 1;
                        numArray[num8] = (byte)(num6 & 127 | 128);
                        num1           = num6 >> 7;
                        num6           = num1;
                    }while (num1 != 0);
                    numArray[num2 - 1] = (byte)(numArray[num2 - 1] & -129);
                    writer.position   += num5;
                    writer.ioIndex    += num5;
                    break;
                }
                else
                {
                    writer.ioBuffer[num2] = (byte)(num & 127);
                    break;
                }
            }

            case PrefixStyle.Fixed32:
            {
                num = writer.ioIndex - num2 - 4;
                ProtoWriter.WriteInt32ToBuffer(num, writer.ioBuffer, num2);
                break;
            }

            case PrefixStyle.Fixed32BigEndian:
            {
                num = writer.ioIndex - num2 - 4;
                byte[] numArray1 = writer.ioBuffer;
                ProtoWriter.WriteInt32ToBuffer(num, numArray1, num2);
                byte num9 = numArray1[num2];
                numArray1[num2]     = numArray1[num2 + 3];
                numArray1[num2 + 3] = num9;
                num9 = numArray1[num2 + 1];
                numArray1[num2 + 1] = numArray1[num2 + 2];
                numArray1[num2 + 2] = num9;
                break;
            }

            default:
            {
                throw new ArgumentOutOfRangeException("style");
            }
            }
            ProtoWriter protoWriter1 = writer;
            int         num10        = protoWriter1.flushLock - 1;
            int         num11        = num10;

            protoWriter1.flushLock = num10;
            if (num11 == 0 && writer.ioIndex >= 1024)
            {
                ProtoWriter.Flush(writer);
            }
        }
        public static void WriteBytes(byte[] data, int offset, int length, ProtoWriter writer)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            switch (writer.wireType)
            {
            case ProtoBuf.WireType.Fixed64:
            {
                if (length == 8)
                {
                    break;
                }
                throw new ArgumentException("length");
            }

            case ProtoBuf.WireType.String:
            {
                ProtoWriter.WriteUInt32Variant((uint)length, writer);
                writer.wireType = ProtoBuf.WireType.None;
                if (length == 0)
                {
                    return;
                }
                if (writer.flushLock != 0 || length <= (int)writer.ioBuffer.Length)
                {
                    break;
                }
                ProtoWriter.Flush(writer);
                writer.dest.Write(data, offset, length);
                writer.position += length;
                return;
            }

            case ProtoBuf.WireType.StartGroup:
            case ProtoBuf.WireType.EndGroup:
            {
                throw ProtoWriter.CreateException(writer);
            }

            case ProtoBuf.WireType.Fixed32:
            {
                if (length == 4)
                {
                    break;
                }
                throw new ArgumentException("length");
            }

            default:
            {
                throw ProtoWriter.CreateException(writer);
            }
            }
            ProtoWriter.DemandSpace(length, writer);
            Helpers.BlockCopy(data, offset, writer.ioBuffer, writer.ioIndex, length);
            ProtoWriter.IncrementedAndReset(length, writer);
        }