Exemple #1
0
 public static object DeserializeFromBytes(this ISerializer self, ReadOnlyMemory <byte> bytes)
 {
     using (var stream = bytes.AsStream())
     {
         return(self.Deserialize(stream));
     }
 }
Exemple #2
0
        public void Test_MemoryStream_WriteToEndAndRefreshPosition()
        {
            byte[]
            array = new byte[10],
            temp = new byte[1];
            ReadOnlyMemory <byte> memory = array;

            using var stream = memory.AsStream();

            for (int i = 0; i < array.Length; i++)
            {
                int read = stream.Read(temp, 0, 1);

                Assert.AreEqual(read, 1);
                Assert.AreEqual(stream.Position, i + 1);
            }

            Assert.AreEqual(stream.Position, array.Length);

            // These should not throw, seeking to the end is valid
            stream.Position = stream.Position;
            Assert.AreEqual(stream.Position, array.Length);

            stream.Seek(array.Length, SeekOrigin.Begin);
            Assert.AreEqual(stream.Position, array.Length);

            stream.Seek(0, SeekOrigin.Current);
            Assert.AreEqual(stream.Position, array.Length);

            stream.Seek(0, SeekOrigin.End);
            Assert.AreEqual(stream.Position, array.Length);
        }
Exemple #3
0
        public async ValueTask <ArraySegment <byte> > DecompressAsync(ReadOnlyMemory <byte> compressedData)
        {
            Guard.AgainstEmpty(compressedData, nameof(compressedData));

            using var uncompressedStream = RecyclableManager.GetStream(nameof(RecyclableGzipProvider), CompressionHelpers.GetGzipUncompressedLength(compressedData));
            using (var gzipStream = new GZipStream(compressedData.AsStream(), CompressionMode.Decompress, false))
            {
                await gzipStream
                .CopyToAsync(uncompressedStream)
                .ConfigureAwait(false);
            }

            if (uncompressedStream.TryGetBuffer(out var buffer))
            {
                return(buffer);
            }
            else
            {
                // dispose stream after allocation.
                using (uncompressedStream)
                {
                    return(uncompressedStream.ToArray());
                }
            }
        }
Exemple #4
0
        public void Test_ReadOnlyMemoryExtensions_EmptyMemoryStream()
        {
            ReadOnlyMemory <byte> memory = default;

            Stream stream = memory.AsStream();

            Assert.IsNotNull(stream);
            Assert.AreEqual(stream.Length, memory.Length);
            Assert.IsFalse(stream.CanWrite);
        }
        /// <summary>
        /// Returns a new <c>MemoryStream</c> that has decompressed data inside.
        /// </summary>
        /// <param name="compressedData"></param>
        /// <returns>A <c>new MemoryStream</c>.</returns>
        public MemoryStream DecompressToStream(ReadOnlyMemory <byte> compressedData)
        {
            var uncompressedStream = new MemoryStream();

            using (var brotliStream = new BrotliStream(compressedData.AsStream(), CompressionMode.Decompress, false))
            {
                brotliStream.CopyTo(uncompressedStream);
            }

            return(uncompressedStream);
        }
Exemple #6
0
        /// <summary>
        /// Returns a new <c>MemoryStream</c> that has decompressed data inside.
        /// </summary>
        /// <param name="compressedData"></param>
        /// <returns>A <c>new MemoryStream</c>.</returns>
        public MemoryStream DecompressToStream(ReadOnlyMemory <byte> compressedData)
        {
            var uncompressedStream = new MemoryStream();

            using (var lz4Stream = LZ4Stream.Decode(compressedData.AsStream(), _decoderSettings, false))
            {
                lz4Stream.CopyTo(uncompressedStream);
            }

            return(uncompressedStream);
        }
Exemple #7
0
        /// <summary>
        /// Returns a new <c>MemoryStream</c> that has decompressed data inside.
        /// </summary>
        /// <param name="compressedData"></param>
        /// <returns>A <c>new MemoryStream</c>.</returns>
        public MemoryStream DecompressToStream(ReadOnlyMemory <byte> compressedData)
        {
            var uncompressedStream = new MemoryStream(CompressionHelpers.GetGzipUncompressedLength(compressedData));

            using (var gzipStream = new GZipStream(compressedData.AsStream(), CompressionMode.Decompress, false))
            {
                gzipStream.CopyTo(uncompressedStream);
            }

            return(uncompressedStream);
        }
Exemple #8
0
        /// <summary>
        /// Returns a new <c>MemoryStream</c> that has decompressed data inside.
        /// </summary>
        /// <param name="compressedStream"></param>
        /// <param name="leaveStreamOpen"></param>
        /// <returns>A <c>new MemoryStream</c>.</returns>
        public MemoryStream DecompressToStream(ReadOnlyMemory <byte> compressedData)
        {
            Guard.AgainstEmpty(compressedData, nameof(compressedData));

            var uncompressedStream = RecyclableManager.GetStream(nameof(RecyclableGzipProvider), CompressionHelpers.GetGzipUncompressedLength(compressedData));

            using (var gzipStream = new GZipStream(compressedData.AsStream(), CompressionMode.Decompress, false))
            {
                gzipStream.CopyTo(uncompressedStream);
            }

            return(uncompressedStream);
        }
        /// <summary>
        /// Returns a new <c>MemoryStream</c> that has decompressed data inside.
        /// </summary>
        /// <param name="compressedStream"></param>
        /// <param name="leaveStreamOpen"></param>
        /// <returns>A <c>new MemoryStream</c>.</returns>
        public MemoryStream DecompressToStream(ReadOnlyMemory <byte> compressedData)
        {
            Guard.AgainstEmpty(compressedData, nameof(compressedData));

            var uncompressedStream = RecyclableManager.GetStream(nameof(RecyclableBrotliProvider));

            using (var brotliStream = new BrotliStream(compressedData.AsStream(), CompressionMode.Decompress, false))
            {
                brotliStream.CopyTo(uncompressedStream);
            }

            return(uncompressedStream);
        }
Exemple #10
0
        /// <summary>
        /// Transforms data back to the original object.
        /// <para>Data was serialized, compressed, then encrypted. So here it is decrypted, decompressed, and deserialized.</para>
        /// </summary>
        /// <typeparam name="TOut"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public Task <TOut> DeserializeAsync <TOut>(ReadOnlyMemory <byte> data)
        {
            if (_encryptionProvider != null && _compressionProvider != null)
            {
                return(DecryptDecompressDeserializeAsync <TOut>(data));
            }
            else if (_encryptionProvider != null)
            {
                return(DecryptDeserializeAsync <TOut>(data));
            }
            else if (_compressionProvider != null)
            {
                return(DecompressDeserializeAsync <TOut>(data));
            }

            return(_serializationProvider.DeserializeAsync <TOut>(data.AsStream()));
        }
        public ArraySegment <byte> Decompress(ReadOnlyMemory <byte> compressedData)
        {
            Guard.AgainstEmpty(compressedData, nameof(compressedData));

            using var uncompressedStream = new MemoryStream();
            using (var brotliStream = new BrotliStream(compressedData.AsStream(), CompressionMode.Decompress, false))
            {
                brotliStream.CopyTo(uncompressedStream);
            }

            if (uncompressedStream.TryGetBuffer(out var buffer))
            {
                return(buffer);
            }
            else
            {
                return(uncompressedStream.ToArray());
            }
        }
Exemple #12
0
        public ArraySegment <byte> Decompress(ReadOnlyMemory <byte> compressedData)
        {
            Guard.AgainstEmpty(compressedData, nameof(compressedData));

            using var uncompressedStream = new MemoryStream();
            using (var lz4Stream = LZ4Stream.Decode(compressedData.AsStream(), _decoderSettings, false))
            {
                lz4Stream.CopyTo(uncompressedStream);
            }

            if (uncompressedStream.TryGetBuffer(out var buffer))
            {
                return(buffer);
            }
            else
            {
                return(uncompressedStream.ToArray());
            }
        }
        public async ValueTask <ArraySegment <byte> > DecompressAsync(ReadOnlyMemory <byte> compressedData)
        {
            Guard.AgainstEmpty(compressedData, nameof(compressedData));

            using var uncompressedStream = new MemoryStream();
            using (var brotliStream = new BrotliStream(compressedData.AsStream(), CompressionMode.Decompress, false))
            {
                await brotliStream
                .CopyToAsync(uncompressedStream)
                .ConfigureAwait(false);
            }

            if (uncompressedStream.TryGetBuffer(out var buffer))
            {
                return(buffer);
            }
            else
            {
                return(uncompressedStream.ToArray());
            }
        }
        public ArraySegment <byte> Decompress(ReadOnlyMemory <byte> compressedData)
        {
            Guard.AgainstEmpty(compressedData, nameof(compressedData));

            var uncompressedStream = RecyclableManager.GetStream(nameof(RecyclableBrotliProvider));

            using (var brotliStream = new BrotliStream(compressedData.AsStream(), CompressionMode.Decompress, false))
            {
                brotliStream.CopyTo(uncompressedStream);
            }

            if (uncompressedStream.TryGetBuffer(out var buffer))
            {
                return(buffer);
            }
            else
            {
                // dispose stream after allocation.
                using (uncompressedStream)
                {
                    return(uncompressedStream.ToArray());
                }
            }
        }
Exemple #15
0
 public override object?Read(ReadOnlyMemory <byte> data, Type type)
 => _jsonSerializer.Deserialize(new StreamReader(data.AsStream()), type);