diff --git a/src/ImageSharp/IO/BufferedReadStream.cs b/src/ImageSharp/IO/BufferedReadStream.cs index 0592e58cd..dfc05e62f 100644 --- a/src/ImageSharp/IO/BufferedReadStream.cs +++ b/src/ImageSharp/IO/BufferedReadStream.cs @@ -223,20 +223,16 @@ namespace SixLabors.ImageSharp.IO this.stream.Seek(this.readerPosition, SeekOrigin.Begin); } - int n = this.stream.Read(this.readBuffer, 0, BufferLength); - // Read doesn't always guarantee the full returned length so read a byte // at a time until we get either our count or hit the end of the stream. - int i = 0; - while (n < BufferLength && i != -1) + int n = 0; + int i; + do { - i = this.stream.ReadByte(); - - if (i != -1) - { - this.readBuffer[n++] = (byte)i; - } + i = this.stream.Read(this.readBuffer, n, BufferLength - n); + n += i; } + while (n < BufferLength && i > 0); this.readBufferIndex = 0; } @@ -271,20 +267,16 @@ namespace SixLabors.ImageSharp.IO this.stream.Seek(this.readerPosition, SeekOrigin.Begin); } - int n = this.stream.Read(buffer, offset, count); - // Read doesn't always guarantee the full returned length so read a byte // at a time until we get either our count or hit the end of the stream. - int i = 0; - while (n < count && i != -1) + int n = 0; + int i; + do { - i = this.stream.ReadByte(); - - if (i != -1) - { - buffer[n++] = (byte)i; - } + i = this.stream.Read(buffer, n, count - n); + n += i; } + while (n < count && i > 0); this.Position += n; diff --git a/src/ImageSharp/Image.Decode.cs b/src/ImageSharp/Image.Decode.cs index c78c706bd..683590fd1 100644 --- a/src/ImageSharp/Image.Decode.cs +++ b/src/ImageSharp/Image.Decode.cs @@ -60,20 +60,16 @@ namespace SixLabors.ImageSharp { long startPosition = stream.Position; - int n = stream.Read(buffer.Array, 0, headerSize); - // Read doesn't always guarantee the full returned length so read a byte // at a time until we get either our count or hit the end of the stream. - int i = 0; - while (n < headerSize && i != -1) + int n = 0; + int i; + do { - i = stream.ReadByte(); - - if (i != -1) - { - buffer.Array[n++] = (byte)i; - } + i = stream.Read(buffer.Array, n, headerSize - n); + n += i; } + while (n < headerSize && i > 0); stream.Position = startPosition;