Anixe.IO
2.4.10
See the version list below for details.
dotnet add package Anixe.IO --version 2.4.10
NuGet\Install-Package Anixe.IO -Version 2.4.10
<PackageReference Include="Anixe.IO" Version="2.4.10" />
paket add Anixe.IO --version 2.4.10
#r "nuget: Anixe.IO, 2.4.10"
// Install Anixe.IO as a Cake Addin #addin nuget:?package=Anixe.IO&version=2.4.10 // Install Anixe.IO as a Cake Tool #tool nuget:?package=Anixe.IO&version=2.4.10
Anixe.IO
Package includes
- Base64ReverseStream
- BatchesEnumerator
- CustomMemoryPool
- DeflateReverseStream
- DelegatingTextWriter
- DelimitedEachSpanEnumerator
- DelimitedSpanEnumerator
- EnchancedTextWriter
- EscapedXmlStreamReader
- FastStreamReader
- JsonTextWriter
- ListSegment
- Log
- LoggingHandler
- MemoryOwnedStream
- PooledStream
- RefineReverseStream
- Stream2Stream
- Stream2TextWriter
- StreamCopy
- StreamReader
- StreamWriter
- TextWriter2StreamDelegate
- UnescapeXmlStreamReader
- UrlEncodededWriter
- UrlWriter
- ValueStringBuilder
- ValueUrlWriter
Base64ReverseStream
The reverse stream is a kind of stream which allows to copy from source to destination with passing source stream in constructor and destination steam while copy. Regular forward streams reguires destination stream in constructor, which is not always possible to archieve.
the Base64ReverseStream
allows you to encode source stream into base64 while copying data into destination stream
var source = new MemoryStream(Encoding.UTF8.GetBytes("test sample"));
var target = new MemoryStream();
using (var subject = new Base64ReverseStream(source, 2048, leaveOpen: true))
{
subject.CopyTo(target);
}
BatchesEnumerator
The struct allows to split IList<T>
into batches without allocations using ListSegment class. Check BatchesEnumeratorTest for details
CustomMemoryPool
The class implements an MemoryPool<T>
abstraction.
The class holds only ArrayPool instance inside and does not require disposing.
Creating multiple instances of this class will spawn multiple ArrayPool instances so use it as a singleton in the application.
The class has been added here becuase of limitation inside MemoryPool<T>
default implementation which causes memory leaks for > 1 Mb arrays. For explanation refer to: https://adamsitnik.com/Array-Pool/
var oneMb = 1024 * 1024;
var pool = new CustomMemoryPool<byte>(50 * oneMb);
using (var memoryOwner = pool.Rent(30 * oneMb))
{
DoSomething(memoryOwner);
}
var oneMb = 1024 * 1024;
var pool = new CustomMemoryPool<byte>(50 * oneMb);
using (var stream = new MemoryOwnedStream(pool)) // pass custom pool to the stream for renting
{
SerializeObject(stream);
return (stream.GetBuffer(), stream.Length); // pass the ownership of IMemoryOwner<byte> to next executor and length to have information about bytes written into the memory buffer.
}
DeflateReverseStream
The reverse stream is a kind of stream which allows to copy from source to destination with passing source stream in constructor and destination steam while copy. Regular forward streams reguires destination stream in constructor, which is not always possible to archieve.
the DeflateReverseStream
allows you to compress source stream into gzip format while copying data into destination stream
var source = new MemoryStream(Encoding.UTF8.GetBytes("test sample"));
var target = new MemoryStream();
using (var subject = new DeflateReverseStream(source, 2048, leaveOpen: true))
{
subject.CopyTo(target);
}
DelegatingTextWriter
The class delegates TextWriter signals to provided inner TextWriters, it is useful to feed multiple TextWriters at the same time.
Examples
var tw1 = new StreamWriter(someStream);
var tw2 = Console.Out;
using (var subject = new DelegatingTextWriter(tw1, tw2))
{
subject.WriteLine("some text");
}
DelimitedEachSpanEnumerator
The struct allows to enumerate through tokens delimited by single char from ReadOnlySpan<char>
It has the same output as string.Split with StringSplitOptions.None
Examples
var iterator = new DelimitedEachSpanEnumerator(text.AsSpan(), delimiter);
while (iterator.MoveNext())
{
ReadOnlySpan<char> segment = iterator.Current;
// do something
}
DelimitedSpanEnumerator
The struct allows to enumerate through tokens delimited by single char from ReadOnlySpan<char>
It has the same output as string.Split with StringSplitOptions.RemoveEmptyEntries
Use case:
var iterator = new DelimitedSpanEnumerator(text.AsSpan(), delimiter);
while (iterator.MoveNext())
{
ReadOnlySpan<char> segment = iterator.Current;
// do something
}
EnchancedTextWriter
The class implements TextWriter + some Stream class methods:
- TotalBytesWritten - returns total bytes written to target stream. It is usefull to track if target stream receives data even for forward-only streams (eg. NetworkStream)
- WriteBase64(byte[] data, int offset, int count) - writes data as base64 encoded string into underlying stream
- CanRead - calls the same method from underlying stream
- CanSeek - calls the same method from underlying stream
- CanWrite - calls the same method from underlying stream
- Length - calls the same method from underlying stream
- Position - calls the same method from underlying stream
- Seek(long offset, SeekOrigin origin) - calls the same method from underlying stream
- SetLength(long value) - calls the same method from underlying stream
EscapedXmlStreamReader
Reads XML-entites encoded stream
Example usage
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(escapedXml)))
{
var reader = XmlReader.Create(new EscapedXmlStreamReader(ms));
return ReadResponse(reader);
}
FastStreamReader
The class reads content from file line by line exposing it as ArraySegment<char>
. It has minimal memory footprint
Use case:
using(var reader = new FastStreamReader(File.OpenRead(path)
{
while(reader.Read())
{
ArraySegment<char> line = reader.CurrentRawLine;
// do something
}
}
JsonTextWriter
The class derives from Newtonsoft.Json.JsonTextWriter providing additions WriteValue routines (check JsonTextWriterTest for details)
ListSegment
The class (similar to ArraySegment<T>
) provides a allocation free view of the IList<T>
.
Log
Added in order to ease integration with Resfinity Logging Platform
LoggingHandler
Added allocationless http payloads logger which passes pooled array filled with data to the logging system.
Use LoggingHandler
class as http client delegating handler.
MemoryOwnedStream
This is a memory stream wrapping ReadOnlyMemory<byte>
(read-only mode) or wrapping IMemoryOwner<byte>
taken from MemoryPool<byte>
(read or write mode). It allows you to encapsulate Memory<byte>
inside managed stream. Be aware that default consructor requires to dispose IMemoryOwner<byte>
manually.
using (var stream = new MemoryOwnedStream())
{
SerializeObject(stream);
return (stream.GetBuffer(), stream.Length); // pass the ownership of IMemoryOwner<byte> to next executor and length to have information about bytes written into the memory buffer.
}
ReadOnlyMemory<byte> mem = ReadSomething();
using (var stream = new MemoryOwnedStream(mem)) // only for reading
{
var obj = DeserializeObject(stream);
return obj;
}
PooledStream
This memory stream with internal byte buffer pooled from ArrayPool<byte>
. Use it as regular stream. Useful hints:
- The stream resizes its internal array by rent an array with biger size if the bytes written exceeds the actual buffer size.
- The stream must be disposed to avoid memory leaks.
- The stream exposes it's internal buffer similar to MemoryStream with GetBuffer() method.
- SetLength method is important to limit the content of the stream it it's real length. Internal buffer is always bigger than written content.
var str = "test string";
var stream = new PooledStream(str.Length);
var bytesConverted = Encoding.UTF8.GetBytes(str, 0, str.Length, stream.GetBuffer(), 0);
stream.SetLength(bytesConverted);
RefineReverseStream
the RefineReverseStream
allows you to remove certain bytes from source stream while copying data into destination stream. Is is usefull to remove new line character, but it can be any byte.
var source = new MemoryStream(Encoding.UTF8.GetBytes("test sample\ntest sample2"));
var target = new MemoryStream();
using (var subject = new RefineReverseStream(source, byteToRefine: (byte) '\n', leaveOpen: true))
{
subject.CopyTo(target);
}
Stream2Stream
The class delegates source Stream reads and writes to destination Stream writes
Stream2TextWriter
The class writes input bytes into both: Stream and TextWriter.
Example usage
var subject = new Stream2TextWriter(outputStream, outputTextWriter, writeBuffer: new char[1024]);
subject.Write(inputByteArray, offset: 0, count: inputByteArray.Length);
StreamCopy
Example usage
using(var tw = new StreamWriter(outputStream, Encoding.UTF8))
{
var buffer = new char[buffSize];
StreamCopy.Copy(inputStream, tw, 0, 50, buffer);
}
StreamReader
The class uses array pool for internal buffers allocations (.NET Core only)
StreamWriter
The class uses array pool for internal buffers allocations (.NET Core only)
TextWriter2StreamDelegate
The class delegates source Stream reads and writes to destination Stream writes
UnescapeXmlStreamReader
UrlEncodededWriter
UrlWriter
Example usage
var urlWriter = new UrlWriter();
urlWriter.WriteBaseUrl("http://site.com");
urlWriter.WriteUrlSegment("page");
urlWriter.WriteUrlSegment("subpage");
urlWriter.WriteStartArguments();
urlWriter.WriteArgument("id", "1234");
urlWriter.WriteArgument("device", "mobile");
var url = UrlWriter.toString();
// produces: http://site.com/page/subpage?id=1234&device=mobile
ValueStringBuilder
Anixe implementation of internal .NET library type for building strings without heap allocations. It uses a ReadOnlySpan<char>
provided in constructor as an internal buffer, or an array from ArrayPool.Shared
if the buffer was not provied or was too small.
Use case:
var sb = new ValueStringBuilder(stackalloc char[50]);
var names = new[] { "first", "second" };
for (int i = 0; i < names.Length; i++)
{
if (i > 0)
{
sb.Append(',');
}
sb.Append(i.ToString());
sb.Append(": ");
sb.Append(name);
}
return sb.ToString();
ValueUrlWriter
Allocationless implementation of UrlWriter. It is implemented as ref struct and operates on Span<char>
buffer. Rents an array from ArrayPool.Shared
if provided buffer was too small to write.
Example usage
var urlWriter = new ValueUrlWriter(stackallock char[256]);
urlWriter.WriteBaseUrl("http://site.com");
urlWriter.WriteUrlSegment("page");
urlWriter.WriteUrlSegment("subpage");
urlWriter.WriteStartArguments();
urlWriter.WriteArgument("id", "1234");
urlWriter.WriteArgument("device", "mobile");
var url = UrlWriter.toString();
// produces: http://site.com/page/subpage?id=1234&device=mobile
Remarks
ListSegment<T>
is a wrapper around an IList<T>
that delimits a range of elements in that list. Multiple ListSegment<T>
instances can refer to the same original list and can overlap. The original list must have zero-based indexing and cannot change size in the meantime.
Each message requires logging level to be assigned. For majority of Anixe apps it's easy mapping
- transactions with status OK → MessageLevel.INFORMATIONAL
- transactions with status ERR → MessageLevel.ERROR
If you don't need any customization register:
MessageAsSyslogWriter
asIMessageWriter
for syslog messagesMessageAsGelfWriter
asIMessageWriter
for GELF messages]MessageAsGelfWriter
writer has dependency toIArrayPool<char>
- it must be passed directly to the constructor or registered via dependency injection framework
If you don't need any custom message object use Anixe.IO.Log.Message
- if you need to store data in Graylog and model does not define key for it you can use
- keyDoublePairs, keyLongPairs, keyIntPairs for numbers
- keyStringPairs for strings
If customization is required
define custom writer that inherits from MessageAsSyslogWriter and override:
WriteCustom(TextWriter writer, IMessage message)
method for SeriLogAppendCustom(StringBuilder builder, IMessage message)
method for NLog
define custom writer that inherits from MessageAsGelfWriter and override:
WriteCustom(Anixe.IO.JsonTextWriter, IMessage message)
method for both SeriLog/NLog
define custom IMessage model that either inherits from Message or implements IMessage directly
public class CustomMessage : Message
{
public string foo;
public string bar;
}
public class CustomWriter : MessageAsSyslogTextWriterWriter
{
protected override void WriteCustom(TextWriter writer, IMessage message)
{
var m = message as CustomMessage;
WriteProperty(writer, "foo", m.foo);
WriteProperty(writer, "bar", m.bar);
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 is compatible. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 2.1
- Microsoft.Extensions.Logging (>= 2.1.0)
- Microsoft.IO.RecyclableMemoryStream (>= 2.2.0)
- Newtonsoft.Json (>= 13.0.1)
-
.NETStandard 2.0
- Microsoft.Extensions.Logging.Abstractions (>= 2.4.10)
- Microsoft.Extensions.Options (>= 2.4.10)
- Microsoft.IO.RecyclableMemoryStream (>= 2.2.0)
- Newtonsoft.Json (>= 13.0.1)
- System.Buffers (>= 4.5.1)
- System.Memory (>= 4.5.5)
- System.Threading.Tasks.Extensions (>= 4.5.4)
-
.NETStandard 2.1
- Microsoft.Extensions.Logging.Abstractions (>= 2.4.10)
- Microsoft.Extensions.Options (>= 2.4.10)
- Microsoft.IO.RecyclableMemoryStream (>= 2.2.0)
- Newtonsoft.Json (>= 13.0.1)
- System.Threading.Tasks.Extensions (>= 4.5.4)
-
net5.0
- Microsoft.Extensions.Logging (>= 5.0.0)
- Microsoft.IO.RecyclableMemoryStream (>= 2.2.0)
- Newtonsoft.Json (>= 13.0.1)
-
net6.0
- Microsoft.Extensions.Logging (>= 6.0.0)
- Microsoft.IO.RecyclableMemoryStream (>= 2.2.0)
- Newtonsoft.Json (>= 13.0.1)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on Anixe.IO:
Package | Downloads |
---|---|
Anixe.IO.Messaging
Package Description |
|
Anixe.IO.NLog.Extensions
Package Description |
|
Anixe.IO.Appmetrics
Package Description |
|
Anixe.IO.AuditlogClient
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
4.0.0 | 11,176 | 1/8/2024 |
3.1.0 | 1,438 | 12/4/2023 |
3.0.1 | 6,443 | 2/27/2023 |
3.0.0 | 1,161 | 1/10/2023 |
2.5.0 | 9,507 | 10/14/2022 |
2.4.10 | 1,315 | 10/3/2022 |
2.4.9 | 388 | 9/29/2022 |
2.4.8 | 1,956 | 7/7/2022 |
2.4.7 | 491 | 6/30/2022 |
2.4.6 | 2,310 | 6/21/2022 |
2.4.5 | 650 | 6/20/2022 |
2.4.4 | 1,253 | 6/8/2022 |
2.4.3 | 7,272 | 4/6/2022 |
2.4.2 | 973 | 3/28/2022 |
2.4.1 | 445 | 3/22/2022 |
2.4.0 | 3,298 | 3/1/2022 |
2.3.3 | 5,054 | 12/15/2021 |
2.3.2 | 2,664 | 10/29/2021 |
2.3.1 | 382 | 10/7/2021 |
2.3.0 | 611 | 9/29/2021 |
2.2.0 | 1,098 | 9/6/2021 |
2.1.0 | 7,483 | 8/31/2021 |
2.0.26 | 7,717 | 7/8/2021 |
2.0.25 | 6,050 | 4/28/2021 |
2.0.24 | 9,581 | 2/22/2021 |
2.0.23 | 4,368 | 1/26/2021 |
2.0.22 | 371 | 1/26/2021 |
2.0.21 | 1,687 | 12/15/2020 |
2.0.20 | 4,205 | 9/9/2020 |
2.0.19 | 11,967 | 6/23/2020 |
2.0.17 | 785 | 5/29/2020 |
2.0.16 | 1,635 | 5/25/2020 |
2.0.15 | 3,177 | 5/12/2020 |
2.0.14 | 1,074 | 5/12/2020 |
2.0.13 | 5,394 | 4/24/2020 |
2.0.12 | 581 | 4/22/2020 |
2.0.11 | 1,509 | 4/20/2020 |
2.0.10 | 3,686 | 3/19/2020 |
2.0.9 | 770 | 3/18/2020 |
2.0.8 | 548 | 3/17/2020 |
2.0.7 | 4,574 | 1/14/2020 |
2.0.6 | 743 | 12/31/2019 |
2.0.5 | 901 | 12/11/2019 |
2.0.4 | 2,871 | 11/29/2019 |
2.0.2 | 1,198 | 9/5/2019 |
2.0.1 | 4,289 | 7/23/2019 |
2.0.0 | 1,207 | 7/22/2019 |
1.9.1 | 8,533 | 7/14/2019 |
1.9.0 | 685 | 7/9/2019 |
1.8.1 | 2,368 | 5/9/2019 |
1.8.0 | 1,590 | 4/4/2019 |
1.6.8 | 1,161 | 2/13/2019 |
1.6.7 | 780 | 2/6/2019 |
1.6.6 | 703 | 2/4/2019 |
1.6.5 | 1,707 | 11/5/2018 |
1.6.4 | 774 | 11/1/2018 |
1.6.3 | 772 | 10/28/2018 |
1.6.2 | 775 | 10/20/2018 |
1.6.1 | 766 | 10/20/2018 |
1.5.0 | 836 | 10/14/2018 |