diff options
Diffstat (limited to 'crypto/src/util/io/Streams.cs')
-rw-r--r-- | crypto/src/util/io/Streams.cs | 66 |
1 files changed, 37 insertions, 29 deletions
diff --git a/crypto/src/util/io/Streams.cs b/crypto/src/util/io/Streams.cs index e1da47fcd..da8f01068 100644 --- a/crypto/src/util/io/Streams.cs +++ b/crypto/src/util/io/Streams.cs @@ -3,20 +3,13 @@ using System.IO; namespace Org.BouncyCastle.Utilities.IO { - public sealed class Streams + public static class Streams { private const int BufferSize = 4096; - private Streams() - { - } - public static void Drain(Stream inStr) { - byte[] bs = new byte[BufferSize]; - while (inStr.Read(bs, 0, bs.Length) > 0) - { - } + inStr.CopyTo(Stream.Null, BufferSize); } /// <summary>Write the full contents of inStr to the destination stream outStr.</summary> @@ -25,7 +18,7 @@ namespace Org.BouncyCastle.Utilities.IO /// <exception cref="IOException">In case of IO failure.</exception> public static void PipeAll(Stream inStr, Stream outStr) { - PipeAll(inStr, outStr, BufferSize); + inStr.CopyTo(outStr, BufferSize); } /// <summary>Write the full contents of inStr to the destination stream outStr.</summary> @@ -35,12 +28,7 @@ namespace Org.BouncyCastle.Utilities.IO /// <exception cref="IOException">In case of IO failure.</exception> public static void PipeAll(Stream inStr, Stream outStr, int bufferSize) { - byte[] bs = new byte[bufferSize]; - int numRead; - while ((numRead = inStr.Read(bs, 0, bs.Length)) > 0) - { - outStr.Write(bs, 0, numRead); - } + inStr.CopyTo(outStr, bufferSize); } /// <summary> @@ -60,17 +48,9 @@ namespace Org.BouncyCastle.Utilities.IO /// <exception cref="IOException"></exception> public static long PipeAllLimited(Stream inStr, long limit, Stream outStr) { - byte[] bs = new byte[BufferSize]; - long total = 0; - int numRead; - while ((numRead = inStr.Read(bs, 0, bs.Length)) > 0) - { - if ((limit - total) < numRead) - throw new StreamOverflowException("Data Overflow"); - total += numRead; - outStr.Write(bs, 0, numRead); - } - return total; + var limited = new LimitedInputStream(inStr, limit); + limited.CopyTo(outStr, BufferSize); + return limit - limited.CurrentLimit; } public static byte[] ReadAll(Stream inStr) @@ -80,7 +60,12 @@ namespace Org.BouncyCastle.Utilities.IO return buf.ToArray(); } - public static byte[] ReadAllLimited(Stream inStr, int limit) + public static byte[] ReadAll(MemoryStream inStr) + { + return inStr.ToArray(); + } + + public static byte[] ReadAllLimited(Stream inStr, int limit) { MemoryStream buf = new MemoryStream(); PipeAllLimited(inStr, limit, buf); @@ -105,7 +90,22 @@ namespace Org.BouncyCastle.Utilities.IO return totalRead; } - public static void ValidateBufferArguments(byte[] buffer, int offset, int count) +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + public static int ReadFully(Stream inStr, Span<byte> buffer) + { + int totalRead = 0; + while (totalRead < buffer.Length) + { + int numRead = inStr.Read(buffer[totalRead..]); + if (numRead < 1) + break; + totalRead += numRead; + } + return totalRead; + } +#endif + + public static void ValidateBufferArguments(byte[] buffer, int offset, int count) { if (buffer == null) throw new ArgumentNullException("buffer"); @@ -120,6 +120,14 @@ namespace Org.BouncyCastle.Utilities.IO /// <exception cref="IOException"></exception> public static int WriteBufTo(MemoryStream buf, byte[] output, int offset) { +#if NETCOREAPP2_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER + if (buf.TryGetBuffer(out var buffer)) + { + buffer.CopyTo(output, offset); + return buffer.Count; + } +#endif + int size = Convert.ToInt32(buf.Length); buf.WriteTo(new MemoryStream(output, offset, size)); return size; |