public unsafe object ReadBody(VoxBinaryReader reader)
        {
            var length  = VarIntSerializer.ReadVariableInt(reader.ReadByte);
            var pString = reader.TakeBytes(length);

            return(Encoding.UTF8.GetString(pString, length));
        }
Exemplo n.º 2
0
            public void SetValue(int value)
            {
                var restore = target.Position;

                target.Position = varIntBlockOffset;
                VarIntSerializer.WriteVariableInt(target.WriteByte, value);

                var varIntByteCount = (int)(target.Position - varIntBlockOffset);
                var fredTheBanana   = banans[banansIndex];

                fredTheBanana.Length = varIntByteCount;
                fredTheBanana.Skip   = kMaxLengthByteCount - varIntByteCount;
                banans[banansIndex]  = fredTheBanana;
                addToByteCount(varIntByteCount);

                target.Position = restore;
            }
Exemplo n.º 3
0
        public void HandleEnterReadFrame(Stream stream)
        {
            this.stream = stream;
            var frameLength = VarIntSerializer.ReadVariableInt(readByteFromEndOfStreamUnsafe);

            if (stream.Position + frameLength > stream.Length)
            {
                throw new InvalidStateException("The frame would surpass the end of the stream.");
            }

            var ms = stream as MemoryStream;

            if (ms != null)
            {
                var buffer = ms.GetBuffer();
                bufferHandle    = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                basePointer     = (byte *)bufferHandle.AddrOfPinnedObject().ToPointer();
                dataPointers[0] = basePointer + ms.Position;
                ms.Position    += frameLength;
            }
            else
            {
                basePointer = (byte *)Marshal.AllocHGlobal(frameLength).ToPointer();
                var ums = new UnmanagedMemoryStream(basePointer, frameLength);

                const int kCopyBufferSize = 4096;
                byte[]    copyBuffer      = new byte[kCopyBufferSize];
                int       bytesRemaining  = frameLength;
                while (bytesRemaining > 0)
                {
                    var bytesRead = stream.Read(copyBuffer, 0, Math.Min(bytesRemaining, kCopyBufferSize));
                    ums.Write(copyBuffer, 0, bytesRead);
                    bytesRemaining -= bytesRead;
                }

                dataPointers[0] = basePointer;
            }
            bytesRemainings[0] = frameLength;
            currentIndex       = 0;
        }
Exemplo n.º 4
0
        private byte[] ComputeBinaryRepresentation(Type arg)
        {
            var result = new List <byte>();
            var s      = new Stack <Type>();

            s.Push(arg);
            int serializedTypeCount = 0;

            while (s.Count != 0)
            {
                var t = s.Pop();
                if (t.IsArray && t != typeof(string))
                {
                    s.Push(typeof(Array));
                    s.Push(t.GetElementType());
                }
                else if (t.IsGenericType && !t.IsGenericTypeDefinition)
                {
                    s.Push(t.GetGenericTypeDefinition());
                    t.GetGenericArguments().ForEach(s.Push);
                }
                else
                {
                    var typeId = typeRegistry.GetTypeIdOrThrow(t);
                    VarIntSerializer.WriteVariableInt(result.Add, typeId);
                    serializedTypeCount++;
                }
            }
            var lengthBuffer = new List <byte>();

            VarIntSerializer.WriteVariableInt(lengthBuffer.Add, serializedTypeCount);
            // shits given = 0
            for (var i = 0; i < lengthBuffer.Count; i++)
            {
                result.Insert(i, lengthBuffer[i]);
            }
            return(result.ToArray());
        }
Exemplo n.º 5
0
 public int ReadVariableInt()
 {
     return(VarIntSerializer.ReadVariableInt(readByte));
 }