|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.io.compress; |
| 20 | + |
| 21 | +import com.aayushatharva.brotli4j.Brotli4jLoader; |
| 22 | +import com.aayushatharva.brotli4j.encoder.Encoder; |
| 23 | +import org.apache.hadoop.classification.InterfaceAudience; |
| 24 | +import org.apache.hadoop.classification.InterfaceStability; |
| 25 | +import org.apache.hadoop.conf.Configured; |
| 26 | +import org.apache.hadoop.io.compress.brotli.BrotliCompressor; |
| 27 | +import org.apache.hadoop.io.compress.brotli.BrotliDecompressor; |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.InputStream; |
| 30 | +import java.io.OutputStream; |
| 31 | + |
| 32 | +/** |
| 33 | + * This class provides output and input streams for Brotli compression |
| 34 | + * and decompression. It uses the native brotli library provided as a |
| 35 | + * Maven dependency. |
| 36 | + * |
| 37 | + * Brotli compression does not support splittability! |
| 38 | + */ |
| 39 | +@InterfaceAudience.Public |
| 40 | +@InterfaceStability.Evolving |
| 41 | +public class BrotliCodec extends Configured implements CompressionCodec { |
| 42 | + |
| 43 | + public static final String MODE_PROP = "compression.brotli.is-text"; |
| 44 | + public static final String QUALITY_LEVEL_PROP = "compression.brotli.quality"; |
| 45 | + public static final String LZ_WINDOW_SIZE_PROP = "compression.brotli.lzwin"; |
| 46 | + public static final Encoder.Mode DEFAULT_MODE = Encoder.Mode.GENERIC; |
| 47 | + public static final int DEFAULT_QUALITY = -1; |
| 48 | + public static final int DEFAULT_LZ_WINDOW_SIZE = -1; |
| 49 | + |
| 50 | + static { |
| 51 | + loadNatives(); |
| 52 | + } |
| 53 | + |
| 54 | + public static void loadNatives() { |
| 55 | + Brotli4jLoader.ensureAvailability(); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public CompressionOutputStream createOutputStream(OutputStream out) |
| 60 | + throws IOException { |
| 61 | + return createOutputStream(out, createCompressor()); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public CompressionOutputStream createOutputStream(OutputStream out, |
| 66 | + Compressor compressor) |
| 67 | + throws IOException { |
| 68 | + return new BrotliCompressorStream(out, compressor); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Class<? extends Compressor> getCompressorType() { |
| 73 | + return BrotliCompressor.class; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Compressor createCompressor() { |
| 78 | + return new BrotliCompressor(getConf()); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public CompressionInputStream createInputStream(InputStream in) |
| 83 | + throws IOException { |
| 84 | + return createInputStream(in, createDecompressor()); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public CompressionInputStream createInputStream(InputStream in, |
| 89 | + Decompressor decompressor) |
| 90 | + throws IOException { |
| 91 | + return new BrotliDecompressorStream(in, decompressor); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public Class<? extends Decompressor> getDecompressorType() { |
| 96 | + return BrotliDecompressor.class; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public Decompressor createDecompressor() { |
| 101 | + return new BrotliDecompressor(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public String getDefaultExtension() { |
| 106 | + return ".br"; |
| 107 | + } |
| 108 | + |
| 109 | + private static final class BrotliCompressorStream extends CompressorStream { |
| 110 | + private BrotliCompressorStream(OutputStream out, |
| 111 | + Compressor compressor, |
| 112 | + int bufferSize) { |
| 113 | + super(out, compressor, bufferSize); |
| 114 | + } |
| 115 | + |
| 116 | + private BrotliCompressorStream(OutputStream out, |
| 117 | + Compressor compressor) { |
| 118 | + super(out, compressor); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void close() throws IOException { |
| 123 | + super.close(); |
| 124 | + compressor.end(); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private static final class BrotliDecompressorStream |
| 129 | + extends DecompressorStream { |
| 130 | + |
| 131 | + private BrotliDecompressorStream(InputStream in, |
| 132 | + Decompressor decompressor, |
| 133 | + int bufferSize) |
| 134 | + throws IOException { |
| 135 | + super(in, decompressor, bufferSize); |
| 136 | + } |
| 137 | + |
| 138 | + private BrotliDecompressorStream(InputStream in, Decompressor decompressor) |
| 139 | + throws IOException { |
| 140 | + super(in, decompressor); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public void close() throws IOException { |
| 145 | + super.close(); |
| 146 | + decompressor.end(); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments