From c188e5aa7990598a46afbfe114da8ea57ee885d5 Mon Sep 17 00:00:00 2001 From: dirk Date: Tue, 8 Nov 2016 20:40:12 +0100 Subject: [PATCH] Added Skip extension to support streams that cannot seek. --- .../Common/Extensions/StreamExtensions.cs | 25 +++++++++++++++++++ src/ImageSharp/Formats/Gif/GifDecoderCore.cs | 6 ++--- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 2 +- 3 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 src/ImageSharp/Common/Extensions/StreamExtensions.cs diff --git a/src/ImageSharp/Common/Extensions/StreamExtensions.cs b/src/ImageSharp/Common/Extensions/StreamExtensions.cs new file mode 100644 index 000000000..912a03823 --- /dev/null +++ b/src/ImageSharp/Common/Extensions/StreamExtensions.cs @@ -0,0 +1,25 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp +{ + using System.IO; + + internal static class StreamExtensions + { + public static void Skip(this Stream stream, int count) + { + if (stream.CanSeek) + { + stream.Position += count; + } + else + { + byte[] foo = new byte[count]; + stream.Read(foo, 0, count); + } + } + } +} diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index 29b34aa9a..44bb5553b 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -59,7 +59,7 @@ namespace ImageSharp.Formats this.currentStream = stream; // Skip the identifier - this.currentStream.Seek(6, SeekOrigin.Current); + this.currentStream.Skip(6); this.ReadLogicalScreenDescriptor(); if (this.logicalScreenDescriptor.GlobalColorTableFlag) @@ -192,13 +192,13 @@ namespace ImageSharp.Formats /// The number of bytes to skip. private void Skip(int length) { - this.currentStream.Seek(length, SeekOrigin.Current); + this.currentStream.Skip(length); int flag; while ((flag = this.currentStream.ReadByte()) != 0) { - this.currentStream.Seek(flag, SeekOrigin.Current); + this.currentStream.Skip(flag); } } diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index f0f209a0c..cad32d9f2 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -116,7 +116,7 @@ namespace ImageSharp.Formats { Image currentImage = image; this.currentStream = stream; - this.currentStream.Seek(8, SeekOrigin.Current); + this.currentStream.Skip(8); bool isEndChunkReached = false;