TransformStream
This commit is contained in:
parent
fa9c5ac689
commit
f2cdfc86b7
1
.gitignore
vendored
1
.gitignore
vendored
@ -18,6 +18,7 @@ nuget/
|
|||||||
.idea/
|
.idea/
|
||||||
.editorconfig
|
.editorconfig
|
||||||
*.user
|
*.user
|
||||||
|
*.DotSettings
|
||||||
|
|
||||||
#backups
|
#backups
|
||||||
.old*/
|
.old*/
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<!--package info-->
|
<!--package info-->
|
||||||
<PackageId>DTLib.Logging.Microsoft</PackageId>
|
<PackageId>DTLib.Logging.Microsoft</PackageId>
|
||||||
<Version>1.1.0</Version>
|
<Version>1.1.1</Version>
|
||||||
<Authors>Timerix</Authors>
|
<Authors>Timerix</Authors>
|
||||||
<Description>DTLib logger wrapper with dependency injection</Description>
|
<Description>DTLib logger wrapper with dependency injection</Description>
|
||||||
<RepositoryType>GIT</RepositoryType>
|
<RepositoryType>GIT</RepositoryType>
|
||||||
@ -28,6 +28,6 @@
|
|||||||
<ProjectReference Include="..\DTLib\DTLib.csproj" />
|
<ProjectReference Include="..\DTLib\DTLib.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup Condition=" '$(Configuration)' != 'Debug' ">
|
<ItemGroup Condition=" '$(Configuration)' != 'Debug' ">
|
||||||
<PackageReference Include="DTLib" Version="1.4.*" />
|
<PackageReference Include="DTLib" Version="1.6.*" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<!--package info-->
|
<!--package info-->
|
||||||
<PackageId>DTLib</PackageId>
|
<PackageId>DTLib</PackageId>
|
||||||
<Version>1.6.0</Version>
|
<Version>1.6.1</Version>
|
||||||
<Authors>Timerix</Authors>
|
<Authors>Timerix</Authors>
|
||||||
<Description>Library for all my C# projects</Description>
|
<Description>Library for all my C# projects</Description>
|
||||||
<RepositoryType>GIT</RepositoryType>
|
<RepositoryType>GIT</RepositoryType>
|
||||||
|
|||||||
@ -1,3 +0,0 @@
|
|||||||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
|
||||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=console/@EntryIndexedValue">False</s:Boolean>
|
|
||||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=logging_005Cloggers/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
|
||||||
69
DTLib/TransformStream.cs
Normal file
69
DTLib/TransformStream.cs
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
using System.IO;
|
||||||
|
using File = System.IO.File;
|
||||||
|
|
||||||
|
namespace DTLib;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Stream wrapper for some operations over data chunks.
|
||||||
|
/// You can add multiple transform operations.
|
||||||
|
/// <example><code>
|
||||||
|
/// using var pipe = new TransformStream(File.OpenRead("encrypted"))
|
||||||
|
/// .AddTransform(ReportProgress)
|
||||||
|
/// .AddTransform(Decrypt);
|
||||||
|
/// using var o = File.OpenWrite("decrypted");
|
||||||
|
/// pipe.CopyTo(o);
|
||||||
|
/// </code></example>
|
||||||
|
/// </summary>
|
||||||
|
public class TransformStream : Stream
|
||||||
|
{
|
||||||
|
private readonly Stream _inputStream;
|
||||||
|
|
||||||
|
public delegate void TransformFuncDelegate(byte[] buffer, int offset, int count);
|
||||||
|
private List<TransformFuncDelegate> _transformFunctions = new();
|
||||||
|
|
||||||
|
public TransformStream(Stream inputStream)
|
||||||
|
{
|
||||||
|
_inputStream = inputStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TransformStream AddTransform(TransformFuncDelegate f)
|
||||||
|
{
|
||||||
|
_transformFunctions.Add(f);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TransformStream AddTransforms(IEnumerable<TransformFuncDelegate> f)
|
||||||
|
{
|
||||||
|
_transformFunctions.AddRange(f);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool CanRead => _inputStream.CanRead;
|
||||||
|
public override bool CanSeek => _inputStream.CanSeek;
|
||||||
|
public override bool CanWrite => false;
|
||||||
|
public override long Length => _inputStream.Length;
|
||||||
|
public override long Position { get => _inputStream.Position; set => _inputStream.Position = value; }
|
||||||
|
|
||||||
|
public override int Read(byte[] buffer, int offset, int count)
|
||||||
|
{
|
||||||
|
int read = _inputStream.Read(buffer, offset, count);
|
||||||
|
if(read > 0)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < _transformFunctions.Count; i++)
|
||||||
|
{
|
||||||
|
_transformFunctions[i].Invoke(buffer, offset, read);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return read;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Flush() {}
|
||||||
|
|
||||||
|
public override long Seek(long offset, SeekOrigin origin) => _inputStream.Seek(offset, origin);
|
||||||
|
|
||||||
|
public override void SetLength(long value) => throw new NotImplementedException();
|
||||||
|
|
||||||
|
public override void Write(byte[] buffer, int offset, int count) => throw new NotImplementedException();
|
||||||
|
|
||||||
|
public override void Close() => _inputStream.Close();
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user