public async Task Invoke(IDictionary <string, object> environment)
        {
            var context          = new OwinContext(environment);
            var httpOutputStream = context.Response.Body;
            var compressor       = GetCompressor(context.Request);

            if (compressor == null)
            {
                await next.Invoke(environment);

                return;
            }

            await compressionStrategy.Compress(next, context, compressor, httpOutputStream);

            context.Response.Body = httpOutputStream;
        }
示例#2
0
        private void RunCompression(object ewh)
        {
            EventWaitHandle eventWaitHandle = ewh as EventWaitHandle;

            if (eventWaitHandle == null)
            {
                throw new ArgumentException($"{nameof(eventWaitHandle)} has incorrect type or is null");
            }


            FileChunk taskChunk;

            while (InputQueue.TryDequeue(out taskChunk))
            {
                var result = CompressionStrategy.Compress(taskChunk);
                OutputQueue.Enqueue(result);
            }
            eventWaitHandle.Set();
        }
示例#3
0
 public byte[] Compress(byte[] input)
 {
     return(CompressionStrategy.Compress(input));
 }
示例#4
0
        void SerializeIntoBuffer(object metadata, IReadOnlyList <object> events, out int startOffset,
                                 out IDescriptorSerializerContext serializerContext, out BlockType blockType,
                                 out int lenWithoutEndPadding, out ByteBuffer block)
        {
            startOffset = (int)EndBufferLen + HeaderSize;
            var writer = new ByteBufferWriter();

            writer.WriteBlock(_zeroes.AsSpan(0, startOffset));
            serializerContext = Mapping;
            if (metadata != null)
            {
                serializerContext = serializerContext.StoreNewDescriptors(writer, metadata);
            }
            if (events != null)
            {
                foreach (var o in events)
                {
                    serializerContext = serializerContext.StoreNewDescriptors(writer, o);
                }
                if (events.Count == 0)
                {
                    events = null;
                }
            }
            serializerContext.FinishNewDescriptors(writer);
            blockType = BlockType.FirstBlock;
            if (serializerContext.SomeTypeStored)
            {
                blockType |= BlockType.HasTypeDeclaration;
            }
            if (metadata != null)
            {
                serializerContext.StoreObject(writer, metadata);
                blockType |= BlockType.HasMetadata;
            }
            if (events != null)
            {
                if (events.Count == 1)
                {
                    serializerContext.StoreObject(writer, events[0]);
                    blockType |= BlockType.HasOneEvent;
                }
                else
                {
                    writer.WriteVUInt32((uint)events.Count);
                    foreach (var o in events)
                    {
                        serializerContext.StoreObject(writer, o);
                    }
                    blockType |= BlockType.HasMoreEvents;
                }
            }
            lenWithoutEndPadding = (int)writer.GetCurrentPosition();
            writer.WriteBlock(_zeroes.AsSpan(0, (int)(SectorSize - 1)));
            block = writer.Data;
            if (CompressionStrategy.ShouldTryToCompress(lenWithoutEndPadding - startOffset))
            {
                var compressedBlock = ByteBuffer.NewSync(block.Buffer, startOffset, lenWithoutEndPadding - startOffset);
                if (CompressionStrategy.Compress(ref compressedBlock))
                {
                    blockType |= BlockType.Compressed;
                    Array.Copy(compressedBlock.Buffer, compressedBlock.Offset, block.Buffer, startOffset, compressedBlock.Length);
                    lenWithoutEndPadding = startOffset + compressedBlock.Length;
                    Array.Copy(_zeroes, 0, block.Buffer, lenWithoutEndPadding, (int)SectorSize - 1);
                }
            }
        }