public static byte[] ReadAllBytes(this Stream stream) { long originalPosition = 0; if (stream.CanSeek) { originalPosition = stream.Position; stream.Position = 0; } try { byte[] readBuffer = new byte[4096]; int totalBytesRead = 0; int bytesRead; while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0) { totalBytesRead += bytesRead; if (totalBytesRead == readBuffer.Length) { int nextByte = stream.ReadByte(); if (nextByte != -1) { byte[] temp = new byte[readBuffer.Length * 2]; Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length); Buffer.SetByte(temp, totalBytesRead, (byte)nextByte); readBuffer = temp; totalBytesRead++; } } } byte[] buffer = readBuffer; if (readBuffer.Length != totalBytesRead) { buffer = new byte[totalBytesRead]; Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead); } return(buffer); } finally { if (stream.CanSeek) { stream.Position = originalPosition; } } }
static int Main() { Boolean pass = true; #pragma warning disable 0436 Int32 a = new Int32(); a.Init("asdas"); TestLibrary.Logging.WriteLine(a); Int32[] foo = new Int32 [] { a }; #pragma warning restore 0436 byte b = 0; // GetByte try { b = Buffer.GetByte(foo, 0); pass = false; TestLibrary.Logging.WriteLine("GetByte: No exception thrown! Got 0x{0:x}", b); } catch (ArgumentException ex) { TestLibrary.Logging.WriteLine("GetByte: Got expected exception: {0}: {1}", ex.GetType(), ex.Message); } catch (Exception ex) { pass = false; TestLibrary.Logging.WriteLine("GetByte: Unexpected exception thrown: " + ex); } // SetByte try { Buffer.SetByte(foo, 0, (Byte)2); pass = false; TestLibrary.Logging.WriteLine("SetByte: No exception thrown! Got 0x{0:x}", b); } catch (ArgumentException ex) { TestLibrary.Logging.WriteLine("SetByte: Got expected exception: {0}: {1}", ex.GetType(), ex.Message); } catch (Exception ex) { pass = false; TestLibrary.Logging.WriteLine("SetByte: Unexpected exception thrown: " + ex); } // BlockCopy try { Object[] arrObjects = new Object[3]; Buffer.BlockCopy(foo, 0, arrObjects, 0, 4); pass = false; TestLibrary.Logging.WriteLine("BlockCopy: No exception thrown! Got 0x{0:x}", b); } catch (ArgumentException ex) { TestLibrary.Logging.WriteLine("BlockCopy: Got expected exception: {0}: {1}", ex.GetType(), ex.Message); } catch (Exception ex) { pass = false; TestLibrary.Logging.WriteLine("BlockCopy: Unexpected exception thrown: " + ex); } if (pass) { TestLibrary.Logging.WriteLine("Test passed."); return(100); } else { TestLibrary.Logging.WriteLine("Test failed."); return(1); } }
public static async Task <byte[]> ReadBytesAsync( this Stream stream, CancellationToken ct, ulong startPosition = 0, Action <ulong, ulong?>?progressCallback = null) { if (!stream.CanRead) { throw new InvalidOperationException("This stream can't read."); } ulong?length; try { length = (ulong)stream.Length - startPosition; if (length <= 0) // empty stream or position not before end of stream { return(new byte[] { }); } } catch (NotSupportedException) { length = null; } var bufferSize = (int?)length ?? DEFAULT_BUFFER_LENGTH; var classicStream = stream; if (classicStream is MemoryStream { Position : 0 } memStream) { // MemoryStream.ToArray() is already optimized, so use it when possible return(memStream.ToArray()); } var readBuffer = new byte[bufferSize]; var totalBytesRead = 0; int bytesRead; progressCallback?.Invoke(0, length); while ((bytesRead = await stream.ReadAsync(readBuffer, totalBytesRead, bufferSize - totalBytesRead, ct)) > 0) { totalBytesRead += bytesRead; progressCallback?.Invoke((ulong)totalBytesRead, length); if (totalBytesRead == bufferSize) { var nextBytes = new byte[1]; var read = await stream.ReadAsync(nextBytes, 0, 1, ct); if (read != 1) { continue; } var temp = new byte[bufferSize * 2]; SystemBuffer.BlockCopy(readBuffer, 0, temp, 0, bufferSize); SystemBuffer.SetByte(temp, totalBytesRead, (byte)nextBytes[0]); readBuffer = temp; totalBytesRead++; } } progressCallback?.Invoke((ulong)totalBytesRead, (ulong)totalBytesRead); var buffer = readBuffer; if (totalBytesRead != bufferSize) { buffer = new byte[totalBytesRead]; SystemBuffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead); } return(buffer); }