Comments (4)
It absolutely works as described in the stream compression chapter in the README but you misunderstood how it works.
Let me go through your code.
var random = new Random();
var data = new byte[ushort.MaxValue];
random.NextBytes(data);
You don't understand the idea of random data. This is does not matter too much here as it will work with random bytes, but still, you need to understand that:
[...] The central idea is that a string of bits is random if and only if it is shorter than any computer program that can produce that string [...], which means that random strings are those that cannot be compressed [...]
var compressionOutput = new MemoryStream(ushort.MaxValue);
var compressionInput = new MemoryStream(ushort.MaxValue);
This pre-allocation might be useful in some high performance scenario, but this code, even after fixing obvious bugs is far from optimized, so it just makes things complex for no reason.
compressionInput.SetLength(0);
compressionInput.Write(data, 0, data.Length);
compressionInput.Position = 0;
Writing it to stream, to copy it later is a waste of resources. LZ4 is a stream already, why not write it directly?
using (var compressionStream = LZ4Stream.Encode(compressionInput, leaveOpen: true))
compressionStream.CopyTo(compressionOutput);
This is bad in both lines. You switched input and output and then tried to copy in wrong direction.
The correct(-ish) version would be:
using (var compressionStream = LZ4Stream.Encode(compressionOutput, leaveOpen: true))
compressionInput.CopyTo(compressionStream);
var compressed = compressionOutput.ToArray();
So you have compressed
bytes... which you never use again!
var decompressionOutput = new MemoryStream(ushort.MaxValue);
var decompressionInput = new MemoryStream(ushort.MaxValue);
Unnecessary pre-allocation again.
decompressionInput.SetLength(0);
decompressionInput.Write(data, 0, data.Length);
decompressionInput.Position = 0;
This is also wrong, this should be compressed
not data
.
using (var decompressionStream = LZ4Stream.Decode(compressionInput, leaveOpen: true))
decompressionStream.CopyTo(decompressionOutput);
Not compressionInput
but decompressionInput
.
var decompressed = decompressionOutput.ToArray();
Console.WriteLine(new
{
ReferenceEquals = ReferenceEquals(data, decompressed),
SequenceEqual = Enumerable.SequenceEqual(data, decompressed),
});
So bug-free code would be:
using System;
using System.IO;
using System.Linq;
using K4os.Compression.LZ4.Streams;
public static class Program
{
static void Main(string[] args)
{
var random = new Random();
var data = new byte[ushort.MaxValue];
random.NextBytes(data);
var compressionOutput = new MemoryStream();
var compressionInput = new MemoryStream();
compressionInput.SetLength(0);
compressionInput.Write(data, 0, data.Length);
compressionInput.Position = 0;
using (var compressionStream = LZ4Stream.Encode(compressionOutput, leaveOpen: true))
compressionInput.CopyTo(compressionStream);
var compressed = compressionOutput.ToArray();
var decompressionOutput = new MemoryStream();
var decompressionInput = new MemoryStream();
decompressionInput.SetLength(0);
decompressionInput.Write(compressed, 0, compressed.Length);
decompressionInput.Position = 0;
using (var decompressionStream = LZ4Stream.Decode(decompressionInput, leaveOpen: true))
decompressionStream.CopyTo(decompressionOutput);
var decompressed = decompressionOutput.ToArray();
Console.WriteLine(new
{
ReferenceEquals = ReferenceEquals(data, decompressed),
SequenceEqual = Enumerable.SequenceEqual(data, decompressed),
});
Console.WriteLine("\n(press any key to exit)");
Console.ReadKey();
}
}
after removing intermediate streams which are just consuming memory, faster version would be:
using System;
using System.IO;
using System.Linq;
using K4os.Compression.LZ4.Streams;
public static class Program
{
static void Main(string[] args)
{
var random = new Random();
var data = new byte[ushort.MaxValue];
random.NextBytes(data);
var compressionOutput = new MemoryStream();
using (var compressionStream = LZ4Stream.Encode(compressionOutput, leaveOpen: true))
compressionStream.Write(data, 0, data.Length);
compressionOutput.Position = 0;
var decompressed = new byte[data.Length];
using (var decompressionStream = LZ4Stream.Decode(compressionOutput, leaveOpen: true))
decompressionStream.Read(decompressed, 0, decompressed.Length);
Console.WriteLine(new
{
ReferenceEquals = ReferenceEquals(data, decompressed),
SequenceEqual = Enumerable.SequenceEqual(data, decompressed),
});
Console.WriteLine("\n(press any key to exit)");
Console.ReadKey();
}
}
from k4os.compression.lz4.
I wrote a Stream test program according to the instructions in the README file [...]
Does not feel like it. Please point to the part of README which made you think it works this way so I can rephase it if needed.
Not sure what you want to do but there is no bug here: LZ4Stream.Encode(...)
creates a stream to be written to (Encode/Compress), so:
using (var compressionStream = LZ4Stream.Encode(...))
compressionStream.CopyTo(compressionOutput);
tries to read from (copy from) stream which is write-only.
from k4os.compression.lz4.
My English is not very fluent
I apologize to you for my rudeness
This is the code I used to test
I expected it to work as described in the stream compression chapter in the README but I got an exception
By the way, my target framework is dotnet 6.0
'System.Private.CoreLib.dll': 'Stream does not support reading.'
at System.ThrowHelper.ThrowNotSupportedException_UnreadableStream()
at System.IO.Stream.CopyTo(Stream destination, Int32 bufferSize)
at System.IO.Stream.CopyTo(Stream destination)
using System;
using System.IO;
using System.Linq;
using System.Text;
using K4os.Compression.LZ4.Streams;
var data = Encoding.UTF8.GetBytes("Try K4OS LZ4 Stream :)");
var compressionInput = new MemoryStream(data);
var compressionOutput = new MemoryStream(ushort.MaxValue);
using (var compressionStream = LZ4Stream.Encode(compressionInput, leaveOpen: true))
compressionStream.CopyTo(compressionOutput);
var compressed = compressionOutput.ToArray();
var decompressionInput = new MemoryStream(compressed);
var decompressionOutput = new MemoryStream(ushort.MaxValue);
using (var decompressionStream = LZ4Stream.Decode(decompressionInput, leaveOpen: true, interactive: false))
decompressionStream.CopyTo(decompressionOutput);
var decompressed = decompressionOutput.ToArray();
Console.WriteLine(new
{
ReferenceEquals = ReferenceEquals(data, decompressed),
SequenceEqual = Enumerable.SequenceEqual(data, decompressed),
});
Console.WriteLine("\n(press any key to exit)");
Console.ReadKey();
from k4os.compression.lz4.
Code works successfully
Thank you very much for your help and advice
they are useful to me
from k4os.compression.lz4.
Related Issues (20)
- Unity Support HOT 3
- Implicit reference to System.Runtime.CompilerServices.Unsafe appears to cause wrong version to be selected HOT 3
- Chained encoder does not produce the same result as the lz4 cli when chaining is enabled HOT 4
- Migrating from L4zNet to new implementation
- InvalidDataException: LZ4 frame magic number expected HOT 10
- Implement frame format with K4os.Compression.LZ4 HOT 2
- Question about random access, i want to make something similar to the original dictionaryRandoimAccess.c example HOT 5
- why Length return 7 HOT 1
- Question regarding Lz4net archived repo HOT 6
- LZ4 Legacy format support HOT 2
- LZ4EncoderStream Dispose Issue HOT 3
- Compression Issue? HOT 3
- API: LZ4Codec.Decode/Encode(Stream input, Stream output) HOT 12
- LZ4 with Maui HOT 7
- LZ4DecoderStream ReadAsync calls original stream's sync Read internally HOT 2
- [Question] Block vs Stream vs Pickler to store compressed bitmaps in memory HOT 10
- Adding non compressed data at the end of the stream HOT 2
- The `int LZ4Frame#Encode` overloads don't close the frame HOT 2
- The repository contains the code with the incompatible licenses HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from k4os.compression.lz4.