Comments (5)
补充一下:
当我使用Lz4Picker类进行操作的时候是完全OK的,只是速度略慢,我需要更快的速度。
from k4os.compression.lz4.
Unfortunately I don't read Chinese, but pickler is the simples approach:
var s = "{\"ID\":\"59fd4cb7-dd79-4f9d-953a-ac91dc0b00f9\",\"Key\":\"init\",\"Step\":2147483647,\"Datas\":null}";
var e = LZ4Pickler.Pickle(Encoding.UTF8.GetBytes(s));
var t = Encoding.UTF8.GetString(LZ4Pickler.Unpickle(e));
Debug.Assert(s == t);
If you really need to use low level functions, you need to do all the work around it.
For example, I can see that you are not using value returned by LZ4Codec.Encode(...)
. Well, value tells you how many bytes were used in compression, which is the value you need to use when doing LZ4Coded.Decode(...)
BTW, using GetBytes
, GetString
, new byte[]
puts strain on GC. Try using IBufferWriter<byte>
as much as possible (Pickler works with it).
from k4os.compression.lz4.
Thank you .
My English is very poor. so, I can't describe my problem clearly.
When using the LZ4Codec class to compress and decompress JSON strings, if the Length of the solution byte is set to the Length of the source byte[], it will not be decompressed. If the Length is set to Source.Length * 255, it will be decompressed correctly.
e.g.
//this is source string
var s = "{\"ID\":\"59fd4cb7-dd79-4f9d-953a-ac91dc0b00f9\",\"Key\":\"init\",\"Step\":2147483647,\"Datas\":null}"; var srcList = new List<byte>();
srcList.AddRange(System.Text.Encoding.UTF8.GetBytes(s));
//Get src byte[]
var src = srcList.ToArray();
//Init encode byte[]
var target = new byte[LZ4Codec.MaximumOutputSize(src.Length) ];
LZ4Codec.Encode(src, 0, src.Length, target, 0, target.Length, LZ4Level.L03_HC);
//Data copy
var rarray = new byte[target.Length];
Array.Copy(target, rarray, rarray.Length);
//init decode byte[]
var r = new byte[src.Length];
LZ4Codec.Decode(rarray, r);
// result string
var rs = System.Text.Encoding.UTF8.GetString(r);
//this is false rs != s,
// But, if s="abcdefg",this is true, rs == s
Assert.IsTrue(rs == s);
from k4os.compression.lz4.
Pickler very good, it's so great.
from k4os.compression.lz4.
Your rarray
has maximum bytes not actual bytes.
Take actualLength = LZ4Codec.Encode(...);
and use it when creating var rarray = new byte[actualLength];
BUT...
your code is far from optimal. You are making many unnecesary allocation (for example: srcList.AddRange(System.Text.Encoding.UTF8.GetBytes(s)); var src = srcList.ToArray();
). Stick to my previous example:
var s = "{\"ID\":\"59fd4cb7-dd79-4f9d-953a-ac91dc0b00f9\",\"Key\":\"init\",\"Step\":2147483647,\"Datas\":null}";
var e = LZ4Pickler.Pickle(Encoding.UTF8.GetBytes(s));
var t = Encoding.UTF8.GetString(LZ4Pickler.Unpickle(e));
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
- Stream API CopyTo got NotSupportedException HOT 4
- 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.