Skip to content

Commit 47f0593

Browse files
committed
HADOOP-13126 Add BrotliCodec based on Brotli4j library
1 parent 0af4bb3 commit 47f0593

File tree

10 files changed

+962
-0
lines changed

10 files changed

+962
-0
lines changed

hadoop-common-project/hadoop-common/pom.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,41 @@
231231
<artifactId>commons-text</artifactId>
232232
<scope>compile</scope>
233233
</dependency>
234+
<dependency>
235+
<groupId>com.aayushatharva.brotli4j</groupId>
236+
<artifactId>brotli4j</artifactId>
237+
<version>${brotli4j.version}</version>
238+
<scope>provided</scope>
239+
<optional>true</optional>
240+
</dependency>
241+
<dependency>
242+
<groupId>com.aayushatharva.brotli4j</groupId>
243+
<artifactId>native-linux-aarch64</artifactId>
244+
<scope>provided</scope>
245+
<version>${brotli4j.version}</version>
246+
<optional>true</optional>
247+
</dependency>
248+
<dependency>
249+
<groupId>com.aayushatharva.brotli4j</groupId>
250+
<artifactId>native-linux-x86_64</artifactId>
251+
<scope>provided</scope>
252+
<version>${brotli4j.version}</version>
253+
<optional>true</optional>
254+
</dependency>
255+
<dependency>
256+
<groupId>com.aayushatharva.brotli4j</groupId>
257+
<artifactId>native-windows-x86_64</artifactId>
258+
<version>${brotli4j.version}</version>
259+
<scope>provided</scope>
260+
<optional>true</optional>
261+
</dependency>
262+
<dependency>
263+
<groupId>com.aayushatharva.brotli4j</groupId>
264+
<artifactId>native-osx-x86_64</artifactId>
265+
<version>${brotli4j.version}</version>
266+
<scope>provided</scope>
267+
<optional>true</optional>
268+
</dependency>
234269
<dependency>
235270
<groupId>org.slf4j</groupId>
236271
<artifactId>slf4j-api</artifactId>
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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

Comments
 (0)