|
1 | 1 | #pragma warning disable CS8600 |
2 | 2 | #pragma warning disable CS0169 |
| 3 | +#pragma warning disable CS8602 |
| 4 | +#pragma warning disable CS8618 |
3 | 5 |
|
4 | 6 | using MemoryPack; |
5 | 7 | using MemoryPack.Compression; |
|
26 | 28 | using System.Xml.Linq; |
27 | 29 |
|
28 | 30 |
|
29 | | -var str1 = File.ReadAllText(@"Hypertext Transfer Protocol - Wikipedia.html"); |
30 | | -var str2 = File.ReadAllText(@"Hypertext Transfer Protocol - Wikipedia_JPN.html"); |
31 | | -var str3 = File.ReadAllText(@"Hypertext Transfer Protocol - Wikipedia _JPN_TextOnly.txt"); |
32 | | - |
33 | | - |
34 | | -foreach (var item in new[] { str1, str2, str3 }) |
35 | | -{ |
36 | | - if (item == str1) Console.WriteLine("English"); |
37 | | - if (item == str2) Console.WriteLine("Japanese"); |
38 | | - if (item == str3) Console.WriteLine("Jpn Text Only"); |
39 | | - |
40 | | - var utf16 = MemoryMarshal.AsBytes(item.AsSpan()).ToArray(); |
41 | | - var utf8 = Encoding.UTF8.GetBytes(item); |
42 | | - |
43 | | - Console.WriteLine($"UTF16: {HumanReadable(utf16.Length)}"); |
44 | | - Console.WriteLine($"UTF8: {HumanReadable(utf8.Length)}"); |
45 | | - Console.WriteLine($"Compress UTF16: {HumanReadable(Compress(utf16).Length)}"); |
46 | | - Console.WriteLine($"Compress UTF8: {HumanReadable(Compress(utf8).Length)}"); |
47 | | -} |
48 | | - |
49 | | - |
50 | | - |
| 31 | +var value = new ListBytesSample(); |
51 | 32 |
|
| 33 | +var bin = MemoryPackSerializer.Serialize(value); |
| 34 | +MemoryPackSerializer.Deserialize<ListBytesSample>(bin, ref value); |
52 | 35 |
|
| 36 | +// for efficient operation, you can get Span<T> by CollectionsMarshal |
53 | 37 |
|
54 | | -// --- |
| 38 | +var span = CollectionsMarshal.AsSpan(value.Payload); |
55 | 39 |
|
56 | | -static byte[] Compress(ReadOnlySpan<byte> source) |
| 40 | +[MemoryPackable] |
| 41 | +public partial class ListBytesSample |
57 | 42 | { |
58 | | - using var encoder = new BrotliEncoder(1, 22); |
59 | | - var maxLength = BrotliEncoder.GetMaxCompressedLength(source.Length); |
60 | | - var dest = new byte[maxLength]; |
61 | | - var status = encoder.Compress(source, dest, out var consumed, out var written, true); |
62 | | - if (status != OperationStatus.Done) |
63 | | - { |
64 | | - throw new Exception("DAME"); |
65 | | - } |
66 | | - return dest.AsSpan(written).ToArray(); |
| 43 | + public int Id { get; set; } |
| 44 | + public List<byte> Payload { get; set; } |
67 | 45 | } |
68 | 46 |
|
69 | | -string HumanReadable(int length) |
| 47 | +[MemoryPackable] |
| 48 | +public partial class IntClass2 |
70 | 49 | { |
71 | | - if (length < 1024) return $"{length} bytes"; |
72 | | - if (length < 1024 * 1024) return $"{length / 1024} KB"; |
73 | | - if (length < 1024 * 1024 * 1024) return $"{length / 1024 / 1024} MB"; |
74 | | - return $"{length / 1024 / 1024 / 1024} GB"; |
| 50 | + public int Value { get; set; } |
75 | 51 | } |
76 | 52 |
|
| 53 | + |
| 54 | + |
77 | 55 | [MemoryPackable] |
78 | | -public partial class IntClass2 |
| 56 | +public partial struct BrotliValue<T> |
79 | 57 | { |
80 | | - public int Value { get; set; } |
| 58 | + public long Value { get; set; } |
81 | 59 | } |
82 | 60 |
|
83 | 61 |
|
84 | 62 |
|
85 | 63 |
|
| 64 | + |
86 | 65 | //var arrayBufferWriter = new ArrayBufferWriter<byte>(); |
87 | 66 |
|
88 | 67 |
|
|
0 commit comments