Browse Source

Add stream extensions for Span<T>

pull/604/head
Jason Nelson 8 years ago
parent
commit
5abcf2b179
  1. 29
      src/ImageSharp/Common/Extensions/StreamExtensions.cs
  2. 10
      src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
  3. 5
      src/ImageSharp/Formats/Gif/LzwDecoder.cs

29
src/ImageSharp/Common/Extensions/StreamExtensions.cs

@ -1,7 +1,7 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System.Buffers;
using System;
using System.IO;
namespace SixLabors.ImageSharp
@ -11,6 +11,33 @@ namespace SixLabors.ImageSharp
/// </summary>
internal static class StreamExtensions
{
#if NETCOREAPP2_1
/// <summary>
/// Writes data from a stream into the provided buffer.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="buffer">The buffer.</param>
/// <param name="offset">The offset within the buffer to begin writing.</param>
/// <param name="count">The number of bytes to write to the stream.</param>
public static void Write(this Stream stream, Span<byte> buffer, int offset, int count)
{
stream.Write(buffer.Slice(offset, count));
}
/// <summary>
/// Reads data from a stream into the provided buffer.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="buffer">The buffer..</param>
/// <param name="offset">The offset within the buffer where the bytes are read into.</param>
/// <param name="count">The number of bytes, if available, to read.</param>
/// <returns>The actual number of bytes read.</returns>
public static int Read(this Stream stream, Span<byte> buffer, int offset, int count)
{
return stream.Read(buffer.Slice(offset, count));
}
#endif
/// <summary>
/// Skips the number of bytes in the given stream.
/// </summary>

10
src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs

@ -73,18 +73,12 @@ namespace SixLabors.ImageSharp.Formats.Bmp
#endif
fileHeader.WriteTo(buffer);
#if NETCOREAPP2_1
stream.Write(buffer.Slice(0, BmpFileHeader.Size));
#else
stream.Write(buffer, 0, BmpFileHeader.Size);
#endif
infoHeader.WriteTo(buffer);
#if NETCOREAPP2_1
stream.Write(buffer.Slice(0, 40));
#else
stream.Write(buffer, 0, 40);
#endif
this.WriteImage(stream, image.Frames.RootFrame);
stream.Flush();

5
src/ImageSharp/Formats/Gif/LzwDecoder.cs

@ -239,11 +239,8 @@ namespace SixLabors.ImageSharp.Formats.Gif
return 0;
}
#if NETCOREAPP2_1
int count = this.stream.Read(buffer.Slice(0, bufferSize));
#else
int count = this.stream.Read(buffer, 0, bufferSize);
#endif
return count != bufferSize ? 0 : bufferSize;
}

Loading…
Cancel
Save