|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
| 4 | +using System; |
| 5 | +using System.Buffers; |
| 6 | +using System.IO; |
| 7 | +using System.Reflection; |
4 | 8 | using System.Text.RegularExpressions;
|
5 | 9 | using Microsoft.Shared.Diagnostics;
|
6 | 10 |
|
@@ -30,4 +34,104 @@ internal static string SanitizeMemberName(string memberName)
|
30 | 34 | private static Regex InvalidNameCharsRegex() => _invalidNameCharsRegex;
|
31 | 35 | private static readonly Regex _invalidNameCharsRegex = new("[^0-9A-Za-z_]", RegexOptions.Compiled);
|
32 | 36 | #endif
|
| 37 | + |
| 38 | + /// <summary>Invokes the MethodInfo with the specified target object and arguments.</summary> |
| 39 | + private static object? ReflectionInvoke(MethodInfo method, object? target, object?[]? arguments) |
| 40 | + { |
| 41 | +#if NET |
| 42 | + return method.Invoke(target, BindingFlags.DoNotWrapExceptions, binder: null, arguments, culture: null); |
| 43 | +#else |
| 44 | + try |
| 45 | + { |
| 46 | + return method.Invoke(target, BindingFlags.Default, binder: null, arguments, culture: null); |
| 47 | + } |
| 48 | + catch (TargetInvocationException e) when (e.InnerException is not null) |
| 49 | + { |
| 50 | + // If we're targeting .NET Framework, such that BindingFlags.DoNotWrapExceptions |
| 51 | + // is ignored, the original exception will be wrapped in a TargetInvocationException. |
| 52 | + // Unwrap it and throw that original exception, maintaining its stack information. |
| 53 | + System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(e.InnerException).Throw(); |
| 54 | + throw; |
| 55 | + } |
| 56 | +#endif |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Implements a simple write-only memory stream that uses pooled buffers. |
| 61 | + /// </summary> |
| 62 | + private sealed class PooledMemoryStream : Stream |
| 63 | + { |
| 64 | + private const int DefaultBufferSize = 4096; |
| 65 | + private byte[] _buffer; |
| 66 | + private int _position; |
| 67 | + |
| 68 | + public PooledMemoryStream(int initialCapacity = DefaultBufferSize) |
| 69 | + { |
| 70 | + _buffer = ArrayPool<byte>.Shared.Rent(initialCapacity); |
| 71 | + _position = 0; |
| 72 | + } |
| 73 | + |
| 74 | + public ReadOnlySpan<byte> GetBuffer() => _buffer.AsSpan(0, _position); |
| 75 | + public override bool CanWrite => true; |
| 76 | + public override bool CanRead => false; |
| 77 | + public override bool CanSeek => false; |
| 78 | + public override long Length => _position; |
| 79 | + public override long Position |
| 80 | + { |
| 81 | + get => _position; |
| 82 | + set => throw new NotSupportedException(); |
| 83 | + } |
| 84 | + |
| 85 | + public override void Write(byte[] buffer, int offset, int count) |
| 86 | + { |
| 87 | + EnsureNotDisposed(); |
| 88 | + EnsureCapacity(_position + count); |
| 89 | + |
| 90 | + Buffer.BlockCopy(buffer, offset, _buffer, _position, count); |
| 91 | + _position += count; |
| 92 | + } |
| 93 | + |
| 94 | + public override void Flush() |
| 95 | + { |
| 96 | + } |
| 97 | + |
| 98 | + public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException(); |
| 99 | + public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); |
| 100 | + public override void SetLength(long value) => throw new NotSupportedException(); |
| 101 | + |
| 102 | + protected override void Dispose(bool disposing) |
| 103 | + { |
| 104 | + if (_buffer is not null) |
| 105 | + { |
| 106 | + ArrayPool<byte>.Shared.Return(_buffer); |
| 107 | + _buffer = null!; |
| 108 | + } |
| 109 | + |
| 110 | + base.Dispose(disposing); |
| 111 | + } |
| 112 | + |
| 113 | + private void EnsureCapacity(int requiredCapacity) |
| 114 | + { |
| 115 | + if (requiredCapacity <= _buffer.Length) |
| 116 | + { |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + int newCapacity = Math.Max(requiredCapacity, _buffer.Length * 2); |
| 121 | + byte[] newBuffer = ArrayPool<byte>.Shared.Rent(newCapacity); |
| 122 | + Buffer.BlockCopy(_buffer, 0, newBuffer, 0, _position); |
| 123 | + |
| 124 | + ArrayPool<byte>.Shared.Return(_buffer); |
| 125 | + _buffer = newBuffer; |
| 126 | + } |
| 127 | + |
| 128 | + private void EnsureNotDisposed() |
| 129 | + { |
| 130 | + if (_buffer is null) |
| 131 | + { |
| 132 | + Throw(); |
| 133 | + static void Throw() => throw new ObjectDisposedException(nameof(PooledMemoryStream)); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
33 | 137 | }
|
0 commit comments