Ejemplo n.º 1
0
        private static Stream ToStream <T>(int version, ItemBase <T> item)
            where T : ItemBase <T>
        {
            Stream stream = null;

            try
            {
                stream = new RangeStream(item.Export(_bufferManager));

                var dic = new Dictionary <byte, Stream>();

                try
                {
                    stream.Seek(0, SeekOrigin.Begin);

                    BufferStream deflateBufferStream = null;

                    try
                    {
                        deflateBufferStream = new BufferStream(_bufferManager);

                        using (var deflateStream = new DeflateStream(deflateBufferStream, CompressionMode.Compress, true))
                            using (var safeBuffer = _bufferManager.CreateSafeBuffer(1024 * 4))
                            {
                                int length;

                                while ((length = stream.Read(safeBuffer.Value, 0, safeBuffer.Value.Length)) > 0)
                                {
                                    deflateStream.Write(safeBuffer.Value, 0, length);
                                }
                            }

                        deflateBufferStream.Seek(0, SeekOrigin.Begin);

                        dic.Add((byte)ConvertCompressionAlgorithm.Deflate, deflateBufferStream);
                    }
                    catch (Exception)
                    {
                        if (deflateBufferStream != null)
                        {
                            deflateBufferStream.Dispose();
                        }
                    }
                }
                catch (Exception)
                {
                }

                dic.Add((byte)ConvertCompressionAlgorithm.None, stream);

                var list = dic.ToList();

                list.Sort((x, y) =>
                {
                    int c = x.Value.Length.CompareTo(y.Value.Length);
                    if (c != 0)
                    {
                        return(c);
                    }

                    return(x.Key.CompareTo(y.Key));
                });

                for (int i = 1; i < list.Count; i++)
                {
                    list[i].Value.Dispose();
                }

                var headerStream = new BufferStream(_bufferManager);
                VintUtils.SetUInt64(headerStream, (uint)version);
                VintUtils.SetUInt64(headerStream, list[0].Key);

                var dataStream = new UniteStream(headerStream, list[0].Value);

                var crcStream = new MemoryStream(Crc32_Castagnoli.ComputeHash(new WrapperStream(dataStream, true)));
                return(new UniteStream(dataStream, crcStream));
            }
            catch (Exception ex)
            {
                if (stream != null)
                {
                    stream.Dispose();
                }

                throw new ArgumentException(ex.Message, ex);
            }
        }
Ejemplo n.º 2
0
        private static Stream ToStream <T>(ItemBase <T> item)
            where T : ItemBase <T>
        {
            Stream stream = null;

            try
            {
                stream = new RangeStream(item.Export(_bufferManager));

                List <KeyValuePair <byte, Stream> > list = new List <KeyValuePair <byte, Stream> >();

                try
                {
                    stream.Seek(0, SeekOrigin.Begin);

                    BufferStream deflateBufferStream = null;

                    try
                    {
                        deflateBufferStream = new BufferStream(_bufferManager);

                        using (DeflateStream deflateStream = new DeflateStream(deflateBufferStream, CompressionMode.Compress, true))
                        {
                            byte[] compressBuffer = null;

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

                                int i = -1;

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

                        deflateBufferStream.Seek(0, SeekOrigin.Begin);

                        list.Add(new KeyValuePair <byte, Stream>((byte)ConvertCompressionAlgorithm.Deflate, deflateBufferStream));
                    }
                    catch (Exception)
                    {
                        if (deflateBufferStream != null)
                        {
                            deflateBufferStream.Dispose();
                        }
                    }
                }
                catch (Exception)
                {
                }

                list.Add(new KeyValuePair <byte, Stream>((byte)ConvertCompressionAlgorithm.None, stream));

                list.Sort((x, y) =>
                {
                    int c = x.Value.Length.CompareTo(y.Value.Length);
                    if (c != 0)
                    {
                        return(c);
                    }

                    return(x.Key.CompareTo(y.Key));
                });

#if DEBUG
                if (list[0].Value.Length != stream.Length)
                {
                    Debug.WriteLine("AmoebaConverter ToStream : {0}→{1} {2}",
                                    NetworkConverter.ToSizeString(stream.Length),
                                    NetworkConverter.ToSizeString(list[0].Value.Length),
                                    NetworkConverter.ToSizeString(list[0].Value.Length - stream.Length));
                }
#endif

                for (int i = 1; i < list.Count; i++)
                {
                    list[i].Value.Dispose();
                }

                BufferStream headerStream = new BufferStream(_bufferManager);
                headerStream.WriteByte((byte)list[0].Key);

                var dataStream = new UniteStream(headerStream, list[0].Value);

                MemoryStream crcStream = new MemoryStream(Crc32_Castagnoli.ComputeHash(dataStream));
                return(new UniteStream(dataStream, crcStream));
            }
            catch (Exception ex)
            {
                if (stream != null)
                {
                    stream.Dispose();
                }

                throw new ArgumentException(ex.Message, ex);
            }
        }