Skip to content

Commit

Permalink
Use utility function to read byte array.
Browse files Browse the repository at this point in the history
  • Loading branch information
oozcitak committed Mar 19, 2018
1 parent 0f7781d commit 64d6c8f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 23 deletions.
22 changes: 3 additions & 19 deletions ExifLibrary/JPEGFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected internal JPEGFile(Stream stream, Encoding encoding)
long length = (long)BitConverterEx.BigEndian.ToUInt16(lengthbytes, 0);

// Read section header.
header = ReadBuffer(stream, length - 2);
header = Utility.GetStreamBytes(stream, length - 2);
}

// Start of Scan (SOS) sections and RST sections are immediately
Expand Down Expand Up @@ -126,7 +126,7 @@ protected internal JPEGFile(Stream stream, Encoding encoding)
stream.Seek(position, SeekOrigin.Begin);

// Read entropy coded data
entropydata = ReadBuffer(stream, edlength);
entropydata = Utility.GetStreamBytes(stream, edlength);

break;
}
Expand All @@ -141,7 +141,7 @@ protected internal JPEGFile(Stream stream, Encoding encoding)
if (marker == JPEGMarker.EOI)
{
long eoflength = stream.Length - stream.Position;
TrailingData = ReadBuffer(stream, eoflength);
TrailingData = Utility.GetStreamBytes(stream, eoflength);
}
}

Expand Down Expand Up @@ -934,22 +934,6 @@ private void WriteIFD(MemoryStream stream, Dictionary<ExifTag, ExifProperty> ifd
}
}
}

private byte[] ReadBuffer(Stream stream, long length)
{
byte[] buffer = new byte[length];
long bytestoread = length;
while (bytestoread > 0)
{
// Read in chunks of 4K bytes
int count = (int)Math.Min(bytestoread, 4 * 1024);
int bytesread = stream.Read(buffer, (int)(length - bytestoread), count);
if (bytesread == 0)
throw new NotValidJPEGFileException();
bytestoread -= bytesread;
}
return buffer;
}
#endregion
}
}
8 changes: 4 additions & 4 deletions ExifLibrary/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ public static byte[] GetStreamBytes(Stream stream)
/// Reads the stream into the given byte array.
/// </summary>
/// <param name="stream">The <see cref="System.IO.Stream"/> to read.</param>
/// <param name="rem">The number of bytes to read.</param>
/// <param name="length">The number of bytes to read.</param>
/// <returns>Contents of the <paramref name="stream"/> as a byte array.</returns>
public static byte[] GetStreamBytes(Stream stream, long rem)
public static byte[] GetStreamBytes(Stream stream, long length)
{
using (MemoryStream mem = new MemoryStream())
{
byte[] b = new byte[32768];
int r;
while (rem > 0 && (r = stream.Read(b, 0, (int)Math.Min(rem, b.Length))) > 0)
while (length > 0 && (r = stream.Read(b, 0, (int)Math.Min(length, b.Length))) > 0)
{
mem.Write(b, 0, r);
rem = rem - r;
length = length - r;
}

return mem.ToArray();
Expand Down

0 comments on commit 64d6c8f

Please sign in to comment.