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

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

/*
 * Copyright 2013 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 org.testifyproject.testifyproject.netty.buffer.Unpooled;

import java.util.zip.Deflater;

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

class SpdyHeaderBlockZlibEncoder extends SpdyHeaderBlockRawEncoder {

    private final Deflater org.testifyproject.testifyprojectpressor;

    private boolean finished;

    SpdyHeaderBlockZlibEncoder(SpdyVersion spdyVersion, int org.testifyproject.testifyprojectpressionLevel) {
        super(spdyVersion);
        if (org.testifyproject.testifyprojectpressionLevel < 0 || org.testifyproject.testifyprojectpressionLevel > 9) {
            throw new IllegalArgumentException(
                    "org.testifyproject.testifyprojectpressionLevel: " + org.testifyproject.testifyprojectpressionLevel + " (expected: 0-9)");
        }
        org.testifyproject.testifyprojectpressor = new Deflater(org.testifyproject.testifyprojectpressionLevel);
        org.testifyproject.testifyprojectpressor.setDictionary(SPDY_DICT);
    }

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

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

        return len;
    }

    private ByteBuf encode(ByteBufAllocator alloc, int len) {
        ByteBuf org.testifyproject.testifyprojectpressed = alloc.heapBuffer(len);
        boolean release = true;
        try {
            while (org.testifyproject.testifyprojectpressInto(org.testifyproject.testifyprojectpressed)) {
                // Although unlikely, it's possible that the org.testifyproject.testifyprojectpressed size is larger than the org.testifyproject.testifyprojectcompressed size
                org.testifyproject.testifyprojectpressed.ensureWritable(org.testifyproject.testifyprojectpressed.capacity() << 1);
            }
            release = false;
            return org.testifyproject.testifyprojectpressed;
        } finally {
            if (release) {
                org.testifyproject.testifyprojectpressed.release();
            }
        }
    }

    private boolean org.testifyproject.testifyprojectpressInto(ByteBuf org.testifyproject.testifyprojectpressed) {
        byte[] out = org.testifyproject.testifyprojectpressed.array();
        int off = org.testifyproject.testifyprojectpressed.arrayOffset() + org.testifyproject.testifyprojectpressed.writerIndex();
        int toWrite = org.testifyproject.testifyprojectpressed.writableBytes();
        int numBytes = org.testifyproject.testifyprojectpressor.org.testifyproject.testifyprojectflate(out, off, toWrite, Deflater.SYNC_FLUSH);
        org.testifyproject.testifyprojectpressed.writerIndex(org.testifyproject.testifyprojectpressed.writerIndex() + numBytes);
        return numBytes == toWrite;
    }

    @Override
    public ByteBuf encode(ByteBufAllocator alloc, SpdyHeadersFrame frame) throws Exception {
        if (frame == null) {
            throw new IllegalArgumentException("frame");
        }

        if (finished) {
            return Unpooled.EMPTY_BUFFER;
        }

        ByteBuf org.testifyproject.testifyprojectcompressed = super.encode(alloc, frame);
        try {
            if (!org.testifyproject.testifyprojectcompressed.isReadable()) {
                return Unpooled.EMPTY_BUFFER;
            }

            int len = setInput(org.testifyproject.testifyprojectcompressed);
            return encode(alloc, len);
        } finally {
            org.testifyproject.testifyprojectcompressed.release();
        }
    }

    @Override
    public void end() {
        if (finished) {
            return;
        }
        finished = true;
        org.testifyproject.testifyprojectpressor.end();
        super.end();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy