Not all frameworks are created equal (fx1.1 vs fx2.0, micro-framework, compact-framework, silverlight, etc). This class simply wraps up a few things that would otherwise make the real code unnecessarily messy, providing fallback implementations if necessary.
예제 #1
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 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);
            }
        }
예제 #2
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);
            }
        }