类 io.netty.handler.codec.compression.CompressionException 源码实例Demo

下面列出了怎么用 io.netty.handler.codec.compression.CompressionException 的API类实例代码及写法,或者点击链接到github查看源代码。


SpdyHeaderBlockJZlibEncoder(
        SpdyVersion version, int compressionLevel, int windowBits, int memLevel) {
    super(version);
    if (compressionLevel < 0 || compressionLevel > 9) {
        throw new IllegalArgumentException(
                "compressionLevel: " + compressionLevel + " (expected: 0-9)");
    }
    if (windowBits < 9 || windowBits > 15) {
        throw new IllegalArgumentException(
                "windowBits: " + windowBits + " (expected: 9-15)");
    }
    if (memLevel < 1 || memLevel > 9) {
        throw new IllegalArgumentException(
                "memLevel: " + memLevel + " (expected: 1-9)");
    }

    int resultCode = z.deflateInit(
            compressionLevel, windowBits, memLevel, JZlib.W_ZLIB);
    if (resultCode != JZlib.Z_OK) {
        throw new CompressionException(
                "failed to initialize an SPDY header block deflater: " + resultCode);
    } else {
        resultCode = z.deflateSetDictionary(SPDY_DICT, SPDY_DICT.length);
        if (resultCode != JZlib.Z_OK) {
            throw new CompressionException(
                    "failed to set the SPDY dictionary: " + resultCode);
        }
    }
}
 
源代码2 项目: x-pipe   文件: ZstdEncoder.java

private void flushBufferedData(ByteBuf out) {
    final int flushableBytes = buffer.readableBytes();
    if (flushableBytes == 0) {
        return;
    }
    checksum.reset();
    checksum.update(buffer.internalNioBuffer(buffer.readerIndex(), flushableBytes));
    final int check = (int) checksum.getValue();

    final int bufSize = (int) Zstd.compressBound(flushableBytes) + HEADER_LENGTH;
    out.ensureWritable(bufSize);
    final int idx = out.writerIndex();
    int compressedLength;
    try {
        ByteBuffer outNioBuffer = out.internalNioBuffer(idx + HEADER_LENGTH, out.writableBytes() - HEADER_LENGTH);
        compressedLength = Zstd.compress(
                outNioBuffer,
                buffer.internalNioBuffer(buffer.readerIndex(), flushableBytes),
                DEFAULT_COMPRESS_LEVEL);
    } catch (Exception e) {
        throw new CompressionException(e);
    }
    final int blockType;
    if (compressedLength >= flushableBytes) {
        blockType = BLOCK_TYPE_NON_COMPRESSED;
        compressedLength = flushableBytes;
        out.setBytes(idx + HEADER_LENGTH, buffer, 0, flushableBytes);
    } else {
        blockType = BLOCK_TYPE_COMPRESSED;
    }

    out.setInt(idx, MAGIC_NUMBER);
    out.setByte(idx + TOKEN_OFFSET, (byte) (blockType | compressionLevel));
    out.setIntLE(idx + COMPRESSED_LENGTH_OFFSET, compressedLength);
    out.setIntLE(idx + DECOMPRESSED_LENGTH_OFFSET, flushableBytes);
    out.setIntLE(idx + CHECKSUM_OFFSET, check);
    out.writerIndex(idx + HEADER_LENGTH + compressedLength);
    buffer.clear();
}
 

SpdyHeaderBlockJZlibEncoder(
        SpdyVersion version, int compressionLevel, int windowBits, int memLevel) {
    super(version);
    if (compressionLevel < 0 || compressionLevel > 9) {
        throw new IllegalArgumentException(
                "compressionLevel: " + compressionLevel + " (expected: 0-9)");
    }
    if (windowBits < 9 || windowBits > 15) {
        throw new IllegalArgumentException(
                "windowBits: " + windowBits + " (expected: 9-15)");
    }
    if (memLevel < 1 || memLevel > 9) {
        throw new IllegalArgumentException(
                "memLevel: " + memLevel + " (expected: 1-9)");
    }

    int resultCode = z.deflateInit(
            compressionLevel, windowBits, memLevel, JZlib.W_ZLIB);
    if (resultCode != JZlib.Z_OK) {
        throw new CompressionException(
                "failed to initialize an SPDY header block deflater: " + resultCode);
    } else {
        resultCode = z.deflateSetDictionary(SPDY_DICT, SPDY_DICT.length);
        if (resultCode != JZlib.Z_OK) {
            throw new CompressionException(
                    "failed to set the SPDY dictionary: " + resultCode);
        }
    }
}
 

private ByteBuf encode(ByteBufAllocator alloc) {
    boolean release = true;
    ByteBuf out = null;
    try {
        int oldNextInIndex = z.next_in_index;
        int oldNextOutIndex = z.next_out_index;

        int maxOutputLength = (int) Math.ceil(z.next_in.length * 1.001) + 12;
        out = alloc.heapBuffer(maxOutputLength);
        z.next_out = out.array();
        z.next_out_index = out.arrayOffset() + out.writerIndex();
        z.avail_out = maxOutputLength;

        int resultCode;
        try {
            resultCode = z.deflate(JZlib.Z_SYNC_FLUSH);
        } finally {
            out.skipBytes(z.next_in_index - oldNextInIndex);
        }
        if (resultCode != JZlib.Z_OK) {
            throw new CompressionException("compression failure: " + resultCode);
        }

        int outputLength = z.next_out_index - oldNextOutIndex;
        if (outputLength > 0) {
            out.writerIndex(out.writerIndex() + outputLength);
        }
        release = false;
        return out;
    } finally {
        // Deference the external references explicitly to tell the VM that
        // the allocated byte arrays are temporary so that the call stack
        // can be utilized.
        // I'm not sure if the modern VMs do this optimization though.
        z.next_in = null;
        z.next_out = null;
        if (release && out != null) {
            out.release();
        }
    }
}
 

private ByteBuf encode(ByteBufAllocator alloc) {
    boolean release = true;
    ByteBuf out = null;
    try {
        int oldNextInIndex = z.next_in_index;
        int oldNextOutIndex = z.next_out_index;

        int maxOutputLength = (int) Math.ceil(z.next_in.length * 1.001) + 12;
        out = alloc.heapBuffer(maxOutputLength);
        z.next_out = out.array();
        z.next_out_index = out.arrayOffset() + out.writerIndex();
        z.avail_out = maxOutputLength;

        int resultCode;
        try {
            resultCode = z.deflate(JZlib.Z_SYNC_FLUSH);
        } finally {
            out.skipBytes(z.next_in_index - oldNextInIndex);
        }
        if (resultCode != JZlib.Z_OK) {
            throw new CompressionException("compression failure: " + resultCode);
        }

        int outputLength = z.next_out_index - oldNextOutIndex;
        if (outputLength > 0) {
            out.writerIndex(out.writerIndex() + outputLength);
        }
        release = false;
        return out;
    } finally {
        // Deference the external references explicitly to tell the VM that
        // the allocated byte arrays are temporary so that the call stack
        // can be utilized.
        // I'm not sure if the modern VMs do this optimization though.
        z.next_in = null;
        z.next_out = null;
        if (release && out != null) {
            out.release();
        }
    }
}
 
 类所在包
 同包方法