aboutsummaryrefslogtreecommitdiff
path: root/java
diff options
context:
space:
mode:
authorEugene Kliuchnikov <eustas@google.com>2018-09-13 08:09:32 -0400
committerGitHub <noreply@github.com>2018-09-13 08:09:32 -0400
commit2216a0dd63e0142b78abe97bba8be52f6bc972c7 (patch)
tree22ea063e1811526674cb9b01ff60b9a47bd4ef93 /java
parentd4cd6cdf1cd52b75f871b6d75b1c8941454371db (diff)
downloadbrotli-2216a0dd63e0142b78abe97bba8be52f6bc972c7.zip
brotli-2216a0dd63e0142b78abe97bba8be52f6bc972c7.tar.gz
brotli-2216a0dd63e0142b78abe97bba8be52f6bc972c7.tar.bz2
Update (#706)
Update * add ASAN/MSAN unaligned read specializations * add "brotli" prefix to u_uint64 type * increment version to 1.0.06 * fix CoverityScan "unused assignment" warning * fix JDK 8<->9 incompatibility * add encoder optimization for empty input * regenerate JS decoder * unbreak Travis builds
Diffstat (limited to 'java')
-rw-r--r--java/org/brotli/dec/DictionaryData.java12
-rw-r--r--java/org/brotli/dec/Utils.java17
-rw-r--r--java/org/brotli/wrapper/dec/Decoder.java7
-rw-r--r--java/org/brotli/wrapper/enc/BrotliEncoderChannel.java5
-rw-r--r--java/org/brotli/wrapper/enc/BrotliEncoderChannelTest.java5
-rwxr-xr-xjava/org/brotli/wrapper/enc/EmptyInputTest.java29
-rw-r--r--java/org/brotli/wrapper/enc/Encoder.java13
7 files changed, 68 insertions, 20 deletions
diff --git a/java/org/brotli/dec/DictionaryData.java b/java/org/brotli/dec/DictionaryData.java
index a65e812..f969df3 100644
--- a/java/org/brotli/dec/DictionaryData.java
+++ b/java/org/brotli/dec/DictionaryData.java
@@ -6,7 +6,6 @@
package org.brotli.dec;
-import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
/**
@@ -22,14 +21,7 @@ final class DictionaryData {
private static void unpackDictionaryData(
ByteBuffer dictionary, String data0, String data1, String skipFlip) {
// Initialize lower 7 bits of every byte in the dictionary.
- byte[] dict;
- try {
- // NB: String#getBytes(String) is present in JDK 1.1, while other variants require JDK 1.6 and
- // above.
- dict = (data0 + data1).getBytes("US-ASCII");
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException(e); // cannot happen
- }
+ byte[] dict = Utils.toUsAsciiBytes(data0 + data1);
if (dict.length != dictionary.capacity()) {
throw new RuntimeException("Corrupted brotli dictionary");
}
@@ -53,7 +45,7 @@ final class DictionaryData {
static {
ByteBuffer dictionary = ByteBuffer.allocateDirect(122784);
unpackDictionaryData(dictionary, DATA0, DATA1, SKIP_FLIP);
- dictionary.flip();
+ Utils.flipBuffer(dictionary);
Dictionary.setData(dictionary.asReadOnlyBuffer());
}
}
diff --git a/java/org/brotli/dec/Utils.java b/java/org/brotli/dec/Utils.java
index 369e655..1583c75 100644
--- a/java/org/brotli/dec/Utils.java
+++ b/java/org/brotli/dec/Utils.java
@@ -8,6 +8,8 @@ package org.brotli.dec;
import java.io.IOException;
import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.nio.Buffer;
/**
* A set of utility methods.
@@ -71,4 +73,19 @@ final class Utils {
static void closeInput(InputStream src) throws IOException {
src.close();
}
+
+ static byte[] toUsAsciiBytes(String src) {
+ try {
+ // NB: String#getBytes(String) is present in JDK 1.1, while other variants require JDK 1.6 and
+ // above.
+ return src.getBytes("US-ASCII");
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException(e); // cannot happen
+ }
+ }
+
+ // Crazy pills factory: code compiled for JDK8 does not work on JRE9.
+ static void flipBuffer(Buffer buffer) {
+ buffer.flip();
+ }
}
diff --git a/java/org/brotli/wrapper/dec/Decoder.java b/java/org/brotli/wrapper/dec/Decoder.java
index 548c14d..ae4d817 100644
--- a/java/org/brotli/wrapper/dec/Decoder.java
+++ b/java/org/brotli/wrapper/dec/Decoder.java
@@ -7,6 +7,7 @@
package org.brotli.wrapper.dec;
import java.io.IOException;
+import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.util.ArrayList;
@@ -83,7 +84,7 @@ public class Decoder {
break;
}
ByteBuffer inputBuffer = decoder.getInputBuffer();
- inputBuffer.clear();
+ ((Buffer) inputBuffer).clear();
int bytesRead = source.read(inputBuffer);
if (bytesRead == -1) {
fail("unexpected end of input");
@@ -107,7 +108,7 @@ public class Decoder {
}
void discard(int length) {
- buffer.position(buffer.position() + length);
+ ((Buffer) buffer).position(buffer.position() + length);
if (!buffer.hasRemaining()) {
buffer = null;
}
@@ -116,7 +117,7 @@ public class Decoder {
int consume(ByteBuffer dst) {
ByteBuffer slice = buffer.slice();
int limit = Math.min(slice.remaining(), dst.remaining());
- slice.limit(limit);
+ ((Buffer) slice).limit(limit);
dst.put(slice);
discard(limit);
return limit;
diff --git a/java/org/brotli/wrapper/enc/BrotliEncoderChannel.java b/java/org/brotli/wrapper/enc/BrotliEncoderChannel.java
index 83fc3ec..047c356 100644
--- a/java/org/brotli/wrapper/enc/BrotliEncoderChannel.java
+++ b/java/org/brotli/wrapper/enc/BrotliEncoderChannel.java
@@ -7,6 +7,7 @@
package org.brotli.wrapper.enc;
import java.io.IOException;
+import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.WritableByteChannel;
@@ -65,10 +66,10 @@ public class BrotliEncoderChannel extends Encoder implements WritableByteChannel
while (src.hasRemaining() && encode(EncoderJNI.Operation.PROCESS)) {
int limit = Math.min(src.remaining(), inputBuffer.remaining());
ByteBuffer slice = src.slice();
- slice.limit(limit);
+ ((Buffer) slice).limit(limit);
inputBuffer.put(slice);
result += limit;
- src.position(src.position() + limit);
+ ((Buffer) src).position(src.position() + limit);
}
return result;
}
diff --git a/java/org/brotli/wrapper/enc/BrotliEncoderChannelTest.java b/java/org/brotli/wrapper/enc/BrotliEncoderChannelTest.java
index 2492589..aacc6da 100644
--- a/java/org/brotli/wrapper/enc/BrotliEncoderChannelTest.java
+++ b/java/org/brotli/wrapper/enc/BrotliEncoderChannelTest.java
@@ -10,6 +10,7 @@ import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
@@ -95,8 +96,8 @@ public class BrotliEncoderChannelTest extends BrotliJniTestBase {
while (src.hasRemaining()) {
int limit = Math.min(CHUNK_SIZE, src.remaining());
ByteBuffer slice = src.slice();
- slice.limit(limit);
- src.position(src.position() + limit);
+ ((Buffer) slice).limit(limit);
+ ((Buffer) src).position(src.position() + limit);
encoder.write(slice);
}
break;
diff --git a/java/org/brotli/wrapper/enc/EmptyInputTest.java b/java/org/brotli/wrapper/enc/EmptyInputTest.java
new file mode 100755
index 0000000..a536381
--- /dev/null
+++ b/java/org/brotli/wrapper/enc/EmptyInputTest.java
@@ -0,0 +1,29 @@
+/* Copyright 2018 Google Inc. All Rights Reserved.
+
+ Distributed under MIT license.
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+package org.brotli.wrapper.enc;
+
+import static org.junit.Assert.assertEquals;
+
+import org.brotli.integration.BrotliJniTestBase;
+import org.brotli.wrapper.dec.Decoder;
+import java.io.IOException;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests for {@link org.brotli.wrapper.enc.Encoder}. */
+@RunWith(JUnit4.class)
+public class EmptyInputTest extends BrotliJniTestBase {
+ @Test
+ public void testEmptyInput() throws IOException {
+ byte[] data = new byte[0];
+ byte[] encoded = Encoder.compress(data);
+ assertEquals(1, encoded.length);
+ byte[] decoded = Decoder.decompress(encoded);
+ assertEquals(0, decoded.length);
+ }
+}
diff --git a/java/org/brotli/wrapper/enc/Encoder.java b/java/org/brotli/wrapper/enc/Encoder.java
index f35bc22..1fde8d6 100644
--- a/java/org/brotli/wrapper/enc/Encoder.java
+++ b/java/org/brotli/wrapper/enc/Encoder.java
@@ -7,6 +7,7 @@
package org.brotli.wrapper.enc;
import java.io.IOException;
+import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
import java.util.ArrayList;
@@ -17,8 +18,8 @@ import java.util.ArrayList;
public class Encoder {
private final WritableByteChannel destination;
private final EncoderJNI.Wrapper encoder;
+ private ByteBuffer buffer;
final ByteBuffer inputBuffer;
- ByteBuffer buffer;
boolean closed;
/**
@@ -111,7 +112,7 @@ public class Encoder {
boolean encode(EncoderJNI.Operation op) throws IOException {
boolean force = (op != EncoderJNI.Operation.PROCESS);
if (force) {
- inputBuffer.limit(inputBuffer.position());
+ ((Buffer) inputBuffer).limit(inputBuffer.position());
} else if (inputBuffer.hasRemaining()) {
return true;
}
@@ -129,7 +130,7 @@ public class Encoder {
encoder.push(op, inputBuffer.limit());
hasInput = false;
} else {
- inputBuffer.clear();
+ ((Buffer) inputBuffer).clear();
return true;
}
}
@@ -156,6 +157,12 @@ public class Encoder {
* Encodes the given data buffer.
*/
public static byte[] compress(byte[] data, Parameters params) throws IOException {
+ if (data.length == 0) {
+ byte[] empty = new byte[1];
+ empty[0] = 6;
+ return empty;
+ }
+ /* data.length > 0 */
EncoderJNI.Wrapper encoder = new EncoderJNI.Wrapper(data.length, params.quality, params.lgwin);
ArrayList<byte[]> output = new ArrayList<byte[]>();
int totalOutputSize = 0;