Exemplo n.º 1
0
        public static UnsafeRawList <byte> ReadToEnd(this Stream stream, out int length)
        {
            if (stream is null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            var buf = new UnsafeRawList <byte>(0);

            try {
                length = 0;
                while (true)
                {
                    var span    = buf.Extend(1024);
                    var readLen = stream.Read(span);
                    length += readLen;
                    if (readLen == 0)
                    {
                        break;
                    }
                }
                return(buf);
            }
            catch {
                buf.Dispose();
                throw;
            }
        }
Exemplo n.º 2
0
        public void Extend()
        {
            using (var list = new UnsafeRawList <int>(0)) {
                Assert.True(list.Count == 0);
                Assert.True(list.Capacity == 0);
                var extended = list.Extend(0, true);
                Assert.True(list.Count == 0);
                Assert.True(list.Capacity == 0);
                Assert.True(extended.IsEmpty);
            }

            using (var list = new UnsafeRawList <int>(0)) {
                Assert.True(list.Count == 0);
                Assert.True(list.Capacity == 0);
                var extended = list.Extend(3, true);
                Assert.True(list.Count == 3);

                // When extended, the minimum capacity is 4.
                Assert.True(list.Capacity == 4);
                Assert.True(extended.Length == 3);
            }

            using (var list = new UnsafeRawList <int>(40)) {
                Assert.True(list.Count == 0);
                Assert.True(list.Capacity == 40);
                var extended1 = list.Extend(10, true);
                Assert.True(list.Count == 10);

                // The capacity does not changed because it is large enough,.
                Assert.True(list.Capacity == 40);
                Assert.True(extended1.Length == 10);

                // -----------------------------
                var extended2 = list.Extend(50, true);
                Assert.True(list.Count == 60);

                // 'Extend' does not allocate memory of the exact size, but allocates a large enough size.
                // It is twice the size of the current one.
                Assert.True(list.Capacity == 80);
                Assert.True(extended2.Length == 50);
            }
        }
Exemplo n.º 3
0
 private static UnsafeRawList <byte> ReadToBuffer(Stream stream, out int length)
 {
     if (stream.CanSeek)
     {
         var streamLen = stream.Length;
         if (streamLen <= int.MaxValue)
         {
             var buffer = new UnsafeRawList <byte>((int)streamLen);
             try {
                 length = stream.Read(buffer.Extend((int)streamLen, false));
                 return(buffer);
             }
             catch {
                 buffer.Dispose();
                 throw;
             }
         }
         else
         {
             throw new NotSupportedException();
         }
     }
     else
     {
         var buffer   = new UnsafeRawList <byte>();
         var totalLen = 0;
         while (true)
         {
             const int BlockSize = 1024;
             var       readLen   = stream.Read(buffer.Extend(BlockSize, false));
             totalLen += readLen;
             if (readLen < BlockSize)
             {
                 break;
             }
         }
         length = totalLen;
         return(buffer);
     }
 }