Beispiel #1
0
        public void seek()
        {
            MemoryStream memStream = new MemoryStream(testData, false);

            memStream.Seek(testDataOffset, SeekOrigin.Current);
            RangeStream rangeStream = new RangeStream(memStream, testDataLength);

            Assert.AreEqual(testData[testDataOffset + 0], rangeStream.ReadByte());
            rangeStream.Position += 2;
            Assert.AreEqual(testData[testDataOffset + 3], rangeStream.ReadByte());
            Assert.AreEqual(0, rangeStream.Seek(0, SeekOrigin.Begin));
            Assert.AreEqual(testData[testDataOffset + 0], rangeStream.ReadByte());
            Assert.AreEqual(2, rangeStream.Seek(1, SeekOrigin.Current));
            Assert.AreEqual(testData[testDataOffset + 2], rangeStream.ReadByte());
            Assert.AreEqual(1, rangeStream.Seek(-3, SeekOrigin.End));
            Assert.AreEqual(testData[testDataOffset + 1], rangeStream.ReadByte());
        }
Beispiel #2
0
        private static T FromStream <T>(Stream stream)
            where T : ItemBase <T>
        {
            try
            {
                using (var targetStream = new RangeStream(stream, true))
                {
                    using (Stream verifyStream = new RangeStream(targetStream, 0, targetStream.Length - 4, true))
                    {
                        byte[] verifyCrc  = Crc32_Castagnoli.ComputeHash(verifyStream);
                        byte[] orignalCrc = new byte[4];

                        using (RangeStream crcStream = new RangeStream(targetStream, targetStream.Length - 4, 4, true))
                        {
                            crcStream.Read(orignalCrc, 0, orignalCrc.Length);
                        }

                        if (!Unsafe.Equals(verifyCrc, orignalCrc))
                        {
                            throw new ArgumentException("Crc Error");
                        }
                    }

                    targetStream.Seek(0, SeekOrigin.Begin);
                    byte type = (byte)targetStream.ReadByte();

                    using (Stream dataStream = new RangeStream(targetStream, targetStream.Position, targetStream.Length - targetStream.Position - 4, true))
                    {
                        if (type == (byte)ConvertCompressionAlgorithm.None)
                        {
                            return(ItemBase <T> .Import(dataStream, _bufferManager));
                        }
                        else if (type == (byte)ConvertCompressionAlgorithm.Deflate)
                        {
                            using (BufferStream deflateBufferStream = new BufferStream(_bufferManager))
                            {
                                byte[] decompressBuffer = null;

                                try
                                {
                                    decompressBuffer = _bufferManager.TakeBuffer(1024 * 4);

                                    using (DeflateStream deflateStream = new DeflateStream(dataStream, CompressionMode.Decompress, true))
                                    {
                                        int i = -1;

                                        while ((i = deflateStream.Read(decompressBuffer, 0, decompressBuffer.Length)) > 0)
                                        {
                                            deflateBufferStream.Write(decompressBuffer, 0, i);
                                        }
                                    }
                                }
                                finally
                                {
                                    if (decompressBuffer != null)
                                    {
                                        _bufferManager.ReturnBuffer(decompressBuffer);
                                    }
                                }

#if DEBUG
                                Debug.WriteLine("AmoebaConverter FromStream : {0}→{1} {2}",
                                                NetworkConverter.ToSizeString(dataStream.Length),
                                                NetworkConverter.ToSizeString(deflateBufferStream.Length),
                                                NetworkConverter.ToSizeString(dataStream.Length - deflateBufferStream.Length));
#endif

                                deflateBufferStream.Seek(0, SeekOrigin.Begin);

                                return(ItemBase <T> .Import(deflateBufferStream, _bufferManager));
                            }
                        }
                        else
                        {
                            throw new ArgumentException("ArgumentException");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw new ArgumentException(e.Message, e);
            }
        }
Beispiel #3
0
        private static Stream Decompress(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            try
            {
                var targetStream = new RangeStream(stream, true);

                byte type = (byte)targetStream.ReadByte();

                if (type == (byte)ConvertCompressionAlgorithm.None)
                {
                    return(new RangeStream(targetStream));
                }
                else if (type == (byte)ConvertCompressionAlgorithm.Deflate)
                {
                    using (Stream dataStream = new WrapperStream(targetStream, true))
                    {
                        BufferStream deflateBufferStream = null;

                        try
                        {
                            deflateBufferStream = new BufferStream(_bufferManager);

                            using (DeflateStream deflateStream = new DeflateStream(dataStream, CompressionMode.Decompress, true))
                            {
                                byte[] decompressBuffer = null;

                                try
                                {
                                    decompressBuffer = _bufferManager.TakeBuffer(1024 * 4);

                                    int i = -1;

                                    while ((i = deflateStream.Read(decompressBuffer, 0, decompressBuffer.Length)) > 0)
                                    {
                                        deflateBufferStream.Write(decompressBuffer, 0, i);
                                    }
                                }
                                finally
                                {
                                    if (decompressBuffer != null)
                                    {
                                        _bufferManager.ReturnBuffer(decompressBuffer);
                                    }
                                }
                            }

                            deflateBufferStream.Seek(0, SeekOrigin.Begin);

#if DEBUG
                            Debug.WriteLine("ContentConverter Decompress {3} : {0}→{1} {2}",
                                            NetworkConverter.ToSizeString(dataStream.Length),
                                            NetworkConverter.ToSizeString(deflateBufferStream.Length),
                                            NetworkConverter.ToSizeString(dataStream.Length - deflateBufferStream.Length),
                                            ConvertCompressionAlgorithm.Deflate);
#endif

                            return(deflateBufferStream);
                        }
                        catch (Exception)
                        {
                            if (deflateBufferStream != null)
                            {
                                deflateBufferStream.Dispose();
                            }
                        }
                    }
                }

                throw new ArgumentException("ArgumentException");
            }
            catch (Exception e)
            {
                throw new ArgumentException(e.Message, e);
            }
        }