|
| 1 | +/* |
| 2 | + * Copyright 2012 The Netty Project |
| 3 | + * |
| 4 | + * The Netty Project licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.arangodb.http.compression; |
| 18 | + |
| 19 | +import io.netty.buffer.ByteBuf; |
| 20 | +import io.netty.buffer.ByteBufAllocator; |
| 21 | +import io.netty.handler.codec.compression.CompressionException; |
| 22 | +import io.netty.handler.codec.compression.ZlibWrapper; |
| 23 | +import io.netty.util.internal.*; |
| 24 | +import io.netty.util.internal.logging.InternalLogger; |
| 25 | +import io.netty.util.internal.logging.InternalLoggerFactory; |
| 26 | + |
| 27 | +import java.util.zip.CRC32; |
| 28 | +import java.util.zip.Deflater; |
| 29 | + |
| 30 | +/** |
| 31 | + * Compresses a {@link ByteBuf} using the deflate algorithm. |
| 32 | + */ |
| 33 | +class JdkZlibEncoder { |
| 34 | + |
| 35 | + private static final InternalLogger logger = InternalLoggerFactory.getInstance(JdkZlibEncoder.class); |
| 36 | + |
| 37 | + /** |
| 38 | + * Maximum initial size for temporary heap buffers used for the compressed output. Buffer may still grow beyond |
| 39 | + * this if necessary. |
| 40 | + */ |
| 41 | + private static final int MAX_INITIAL_OUTPUT_BUFFER_SIZE; |
| 42 | + /** |
| 43 | + * Max size for temporary heap buffers used to copy input data to heap. |
| 44 | + */ |
| 45 | + private static final int MAX_INPUT_BUFFER_SIZE; |
| 46 | + private static final ByteBuf EMPTY_BUF; |
| 47 | + |
| 48 | + private final ZlibWrapper wrapper; |
| 49 | + private final Deflater deflater; |
| 50 | + |
| 51 | + /* |
| 52 | + * GZIP support |
| 53 | + */ |
| 54 | + private final CRC32 crc = new CRC32(); |
| 55 | + private static final byte[] gzipHeader = {0x1f, (byte) 0x8b, Deflater.DEFLATED, 0, 0, 0, 0, 0, 0, 0}; |
| 56 | + |
| 57 | + static { |
| 58 | + MAX_INITIAL_OUTPUT_BUFFER_SIZE = SystemPropertyUtil.getInt( |
| 59 | + "io.netty.jdkzlib.encoder.maxInitialOutputBufferSize", |
| 60 | + 65536); |
| 61 | + MAX_INPUT_BUFFER_SIZE = SystemPropertyUtil.getInt( |
| 62 | + "io.netty.jdkzlib.encoder.maxInputBufferSize", |
| 63 | + 65536); |
| 64 | + |
| 65 | + if (logger.isDebugEnabled()) { |
| 66 | + logger.debug("-Dio.netty.jdkzlib.encoder.maxInitialOutputBufferSize={}", MAX_INITIAL_OUTPUT_BUFFER_SIZE); |
| 67 | + logger.debug("-Dio.netty.jdkzlib.encoder.maxInputBufferSize={}", MAX_INPUT_BUFFER_SIZE); |
| 68 | + } |
| 69 | + |
| 70 | + EMPTY_BUF = allocateByteBuf(0); |
| 71 | + } |
| 72 | + |
| 73 | + private static ByteBuf allocateByteBuf(int len) { |
| 74 | + return ByteBufAllocator.DEFAULT.heapBuffer(len); |
| 75 | + } |
| 76 | + |
| 77 | + private static ByteBuf allocateByteBuf() { |
| 78 | + return ByteBufAllocator.DEFAULT.heapBuffer(); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + /** |
| 83 | + * Creates a new zlib encoder with the specified {@code compressionLevel} |
| 84 | + * and the specified wrapper. |
| 85 | + * |
| 86 | + * @param compressionLevel {@code 1} yields the fastest compression and {@code 9} yields the |
| 87 | + * best compression. {@code 0} means no compression. The default |
| 88 | + * compression level is {@code 6}. |
| 89 | + * @throws CompressionException if failed to initialize zlib |
| 90 | + */ |
| 91 | + JdkZlibEncoder(ZlibWrapper wrapper, int compressionLevel) { |
| 92 | + ObjectUtil.checkInRange(compressionLevel, 0, 9, "compressionLevel"); |
| 93 | + ObjectUtil.checkNotNull(wrapper, "wrapper"); |
| 94 | + |
| 95 | + if (wrapper == ZlibWrapper.ZLIB_OR_NONE) { |
| 96 | + throw new IllegalArgumentException( |
| 97 | + "wrapper '" + ZlibWrapper.ZLIB_OR_NONE + "' is not " + |
| 98 | + "allowed for compression."); |
| 99 | + } |
| 100 | + |
| 101 | + this.wrapper = wrapper; |
| 102 | + deflater = new Deflater(compressionLevel, wrapper != ZlibWrapper.ZLIB); |
| 103 | + } |
| 104 | + |
| 105 | + ByteBuf encode(byte[] in) { |
| 106 | + if (in.length == 0) { |
| 107 | + return EMPTY_BUF; |
| 108 | + } |
| 109 | + |
| 110 | + ByteBuf out = allocateBuffer(in.length); |
| 111 | + encodeSome(in, out); |
| 112 | + finishEncode(out); |
| 113 | + return out; |
| 114 | + } |
| 115 | + |
| 116 | + private void encodeSome(byte[] in, ByteBuf out) { |
| 117 | + if (wrapper == ZlibWrapper.GZIP) { |
| 118 | + out.writeBytes(gzipHeader); |
| 119 | + } |
| 120 | + if (wrapper == ZlibWrapper.GZIP) { |
| 121 | + crc.update(in, 0, in.length); |
| 122 | + } |
| 123 | + |
| 124 | + deflater.setInput(in); |
| 125 | + for (; ; ) { |
| 126 | + deflate(out); |
| 127 | + if (!out.isWritable()) { |
| 128 | + out.ensureWritable(out.writerIndex()); |
| 129 | + } else if (deflater.needsInput()) { |
| 130 | + break; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private ByteBuf allocateBuffer(int length) { |
| 136 | + int sizeEstimate = (int) Math.ceil(length * 1.001) + 12; |
| 137 | + switch (wrapper) { |
| 138 | + case GZIP: |
| 139 | + sizeEstimate += gzipHeader.length; |
| 140 | + break; |
| 141 | + case ZLIB: |
| 142 | + sizeEstimate += 2; // first two magic bytes |
| 143 | + break; |
| 144 | + default: |
| 145 | + throw new IllegalArgumentException(); |
| 146 | + } |
| 147 | + // sizeEstimate might overflow if close to 2G |
| 148 | + if (sizeEstimate < 0 || sizeEstimate > MAX_INITIAL_OUTPUT_BUFFER_SIZE) { |
| 149 | + // can always expand later |
| 150 | + return allocateByteBuf(MAX_INITIAL_OUTPUT_BUFFER_SIZE); |
| 151 | + } |
| 152 | + return allocateByteBuf(sizeEstimate); |
| 153 | + } |
| 154 | + |
| 155 | + private void finishEncode(ByteBuf out) { |
| 156 | + ByteBuf footer = allocateByteBuf(); |
| 157 | + deflater.finish(); |
| 158 | + while (!deflater.finished()) { |
| 159 | + deflate(footer); |
| 160 | + } |
| 161 | + if (wrapper == ZlibWrapper.GZIP) { |
| 162 | + int crcValue = (int) crc.getValue(); |
| 163 | + int uncBytes = deflater.getTotalIn(); |
| 164 | + footer.writeByte(crcValue); |
| 165 | + footer.writeByte(crcValue >>> 8); |
| 166 | + footer.writeByte(crcValue >>> 16); |
| 167 | + footer.writeByte(crcValue >>> 24); |
| 168 | + footer.writeByte(uncBytes); |
| 169 | + footer.writeByte(uncBytes >>> 8); |
| 170 | + footer.writeByte(uncBytes >>> 16); |
| 171 | + footer.writeByte(uncBytes >>> 24); |
| 172 | + } |
| 173 | + out.writeBytes(footer); |
| 174 | + deflater.reset(); |
| 175 | + crc.reset(); |
| 176 | + } |
| 177 | + |
| 178 | + private void deflate(ByteBuf out) { |
| 179 | + int numBytes; |
| 180 | + do { |
| 181 | + int writerIndex = out.writerIndex(); |
| 182 | + numBytes = deflater.deflate( |
| 183 | + out.array(), out.arrayOffset() + writerIndex, out.writableBytes(), Deflater.SYNC_FLUSH); |
| 184 | + out.writerIndex(writerIndex + numBytes); |
| 185 | + } while (numBytes > 0); |
| 186 | + } |
| 187 | + |
| 188 | + void close() { |
| 189 | + deflater.reset(); |
| 190 | + deflater.end(); |
| 191 | + } |
| 192 | +} |
0 commit comments