All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.testifyproject.netty.handler.codec.spdy.SpdyHeaderBlockZlibDecoder Maven / Gradle / Ivy

/*
 * Copyright 2014 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in org.testifyproject.testifyprojectpliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org.testifyproject/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package org.testifyproject.testifyproject.netty.handler.codec.spdy;

import org.testifyproject.testifyproject.netty.buffer.ByteBuf;
import org.testifyproject.testifyproject.netty.buffer.ByteBufAllocator;

import java.util.zip.DataFormatException;
import java.util.zip.Inflater;

import static org.testifyproject.testifyproject.netty.handler.codec.spdy.SpdyCodecUtil.*;

final class SpdyHeaderBlockZlibDecoder extends SpdyHeaderBlockRawDecoder {

    private static final int DEFAULT_BUFFER_CAPACITY = 4096;
    private static final SpdyProtocolException INVALID_HEADER_BLOCK =
            new SpdyProtocolException("Invalid Header Block");

    private final Inflater org.testifyproject.testifyprojectcompressor = new Inflater();

    private ByteBuf org.testifyproject.testifyprojectcompressed;

    SpdyHeaderBlockZlibDecoder(SpdyVersion spdyVersion, int maxHeaderSize) {
        super(spdyVersion, maxHeaderSize);
    }

    @Override
    void org.testifyproject.testifyprojectcode(ByteBufAllocator alloc, ByteBuf headerBlock, SpdyHeadersFrame frame) throws Exception {
        int len = setInput(headerBlock);

        int numBytes;
        do {
            numBytes = org.testifyproject.testifyprojectcompress(alloc, frame);
        } while (numBytes > 0);

        // z_stream has an internal 64-bit hold buffer
        // it is always capable of consuming the entire input
        if (org.testifyproject.testifyprojectcompressor.getRemaining() != 0) {
            // we reached the end of the org.testifyproject.testifyprojectflate stream
            throw INVALID_HEADER_BLOCK;
        }

        headerBlock.skipBytes(len);
    }

    private int setInput(ByteBuf org.testifyproject.testifyprojectpressed) {
        int len = org.testifyproject.testifyprojectpressed.readableBytes();

        if (org.testifyproject.testifyprojectpressed.hasArray()) {
            org.testifyproject.testifyprojectcompressor.setInput(org.testifyproject.testifyprojectpressed.array(), org.testifyproject.testifyprojectpressed.arrayOffset() + org.testifyproject.testifyprojectpressed.readerIndex(), len);
        } else {
            byte[] in = new byte[len];
            org.testifyproject.testifyprojectpressed.getBytes(org.testifyproject.testifyprojectpressed.readerIndex(), in);
            org.testifyproject.testifyprojectcompressor.setInput(in, 0, in.length);
        }

        return len;
    }

    private int org.testifyproject.testifyprojectcompress(ByteBufAllocator alloc, SpdyHeadersFrame frame) throws Exception {
        ensureBuffer(alloc);
        byte[] out = org.testifyproject.testifyprojectcompressed.array();
        int off = org.testifyproject.testifyprojectcompressed.arrayOffset() + org.testifyproject.testifyprojectcompressed.writerIndex();
        try {
            int numBytes = org.testifyproject.testifyprojectcompressor.inflate(out, off, org.testifyproject.testifyprojectcompressed.writableBytes());
            if (numBytes == 0 && org.testifyproject.testifyprojectcompressor.needsDictionary()) {
                try {
                    org.testifyproject.testifyprojectcompressor.setDictionary(SPDY_DICT);
                } catch (IllegalArgumentException ignored) {
                    throw INVALID_HEADER_BLOCK;
                }
                numBytes = org.testifyproject.testifyprojectcompressor.inflate(out, off, org.testifyproject.testifyprojectcompressed.writableBytes());
            }
            if (frame != null) {
                org.testifyproject.testifyprojectcompressed.writerIndex(org.testifyproject.testifyprojectcompressed.writerIndex() + numBytes);
                org.testifyproject.testifyprojectcodeHeaderBlock(org.testifyproject.testifyprojectcompressed, frame);
                org.testifyproject.testifyprojectcompressed.discardReadBytes();
            }

            return numBytes;
        } catch (DataFormatException e) {
            throw new SpdyProtocolException("Received invalid header block", e);
        }
    }

    private void ensureBuffer(ByteBufAllocator alloc) {
        if (org.testifyproject.testifyprojectcompressed == null) {
            org.testifyproject.testifyprojectcompressed = alloc.heapBuffer(DEFAULT_BUFFER_CAPACITY);
        }
        org.testifyproject.testifyprojectcompressed.ensureWritable(1);
    }

    @Override
    void endHeaderBlock(SpdyHeadersFrame frame) throws Exception {
        super.endHeaderBlock(frame);
        releaseBuffer();
    }

    @Override
    public void end() {
        super.end();
        releaseBuffer();
        org.testifyproject.testifyprojectcompressor.end();
    }

    private void releaseBuffer() {
        if (org.testifyproject.testifyprojectcompressed != null) {
            org.testifyproject.testifyprojectcompressed.release();
            org.testifyproject.testifyprojectcompressed = null;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy