GetBuffer() public method

public GetBuffer ( int length ) : byte[]
length int
return byte[]
Exemplo n.º 1
0
        internal static unsafe byte[] GetBytes(string str, SerializerSession session, out int byteCount)
        {
            //if first byte is 0 = null
            //if first byte is 254 or less, then length is value - 1
            //if first byte is 255 then the next 4 bytes are an int32 for length
            if (str == null)
            {
                byteCount = 1;
                return(new[] { (byte)0 });
            }
            byteCount = Utf8.GetByteCount(str);
            if (byteCount < 254) //short string
            {
                var bytes = session.GetBuffer(byteCount + 1);
                Utf8.GetBytes(str, 0, str.Length, bytes, 1);
                bytes[0]   = (byte)(byteCount + 1);
                byteCount += 1;
                return(bytes);
            }
            else //long string
            {
                var bytes = session.GetBuffer(byteCount + 1 + 4);
                Utf8.GetBytes(str, 0, str.Length, bytes, 1 + 4);
                bytes[0] = 255;


                fixed(byte *b = bytes)
                * (int *)(b + 1) = byteCount;

                byteCount += 1 + 4;

                return(bytes);
            }
        }
Exemplo n.º 2
0
 public static byte[] ReadLengthEncodedByteArray(this Stream self, SerializerSession session)
 {
     var length = self.ReadInt32(session);
     var buffer = session.GetBuffer(length);
     self.Read(buffer, 0, length);
     return buffer;
 }
Exemplo n.º 3
0
 public static int ReadInt32(this Stream self, SerializerSession session)
 {
     var buffer = session.GetBuffer(4);
     self.Read(buffer, 0, 4);
     var res = BitConverter.ToInt32(buffer, 0);
     return res;
 }
Exemplo n.º 4
0
        public static byte[] ReadLengthEncodedByteArray(this Stream self, SerializerSession session)
        {
            var length = self.ReadInt32(session);
            var buffer = session.GetBuffer(length);

            self.Read(buffer, 0, length);
            return(buffer);
        }
Exemplo n.º 5
0
        public static int ReadInt32(this Stream self, SerializerSession session)
        {
            var buffer = session.GetBuffer(4);

            self.Read(buffer, 0, 4);
            var res = BitConverter.ToInt32(buffer, 0);

            return(res);
        }