diff options
author | Eugene Kliuchnikov <eustas@google.com> | 2017-03-22 12:41:19 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-22 12:41:19 +0100 |
commit | a657d9969dfb9655f4b483a658ad5da9dda3e52e (patch) | |
tree | 4c422b8d194ab563edf0c50dfc2045b7487dc91a /java | |
parent | 8a06e02935abadcfff46099c43c879a677d56280 (diff) | |
download | brotli-a657d9969dfb9655f4b483a658ad5da9dda3e52e.zip brotli-a657d9969dfb9655f4b483a658ad5da9dda3e52e.tar.gz brotli-a657d9969dfb9655f4b483a658ad5da9dda3e52e.tar.bz2 |
Add go wrapper, streamline java decoder: (#524)
* add (c)brotli golang wrapper
* remove (language-specific) enums in java decoder
Diffstat (limited to 'java')
-rwxr-xr-x | java/org/brotli/dec/BUILD | 6 | ||||
-rwxr-xr-x | java/org/brotli/dec/EnumTest.java | 53 | ||||
-rwxr-xr-x | java/org/brotli/dec/RunningState.java | 28 | ||||
-rwxr-xr-x | java/org/brotli/dec/State.java | 4 | ||||
-rwxr-xr-x | java/org/brotli/dec/Transform.java | 10 | ||||
-rwxr-xr-x | java/org/brotli/dec/WordTransformType.java | 57 |
6 files changed, 107 insertions, 51 deletions
diff --git a/java/org/brotli/dec/BUILD b/java/org/brotli/dec/BUILD index 8b1c9f3..6119041 100755 --- a/java/org/brotli/dec/BUILD +++ b/java/org/brotli/dec/BUILD @@ -39,6 +39,12 @@ java_test( ) java_test( + name = "EnumTest", + test_class = "org.brotli.dec.EnumTest", + runtime_deps = [":test_lib"], +) + +java_test( name = "SynthTest", test_class = "org.brotli.dec.SynthTest", runtime_deps = [":test_lib"], diff --git a/java/org/brotli/dec/EnumTest.java b/java/org/brotli/dec/EnumTest.java new file mode 100755 index 0000000..8e7b593 --- /dev/null +++ b/java/org/brotli/dec/EnumTest.java @@ -0,0 +1,53 @@ +/* Copyright 2017 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.dec; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.fail; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.TreeSet; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for Enum-like classes. + */ +@RunWith(JUnit4.class) +public class EnumTest { + + private void checkEnumClass(Class<?> clazz) { + TreeSet<Integer> values = new TreeSet<Integer>(); + for (Field f : clazz.getDeclaredFields()) { + assertEquals("int", f.getType().getName()); + assertEquals(Modifier.FINAL | Modifier.STATIC, f.getModifiers()); + Integer value = null; + try { + value = f.getInt(null); + } catch (IllegalAccessException ex) { + fail("Inaccessible field"); + } + assertFalse(values.contains(value)); + values.add(value); + } + assertEquals(0, values.first().intValue()); + assertEquals(values.size(), values.last() + 1); + } + + @Test + public void testRunningState() { + checkEnumClass(RunningState.class); + } + + @Test + public void testWordTransformType() { + checkEnumClass(WordTransformType.class); + } +} diff --git a/java/org/brotli/dec/RunningState.java b/java/org/brotli/dec/RunningState.java index 4fe22be..692267f 100755 --- a/java/org/brotli/dec/RunningState.java +++ b/java/org/brotli/dec/RunningState.java @@ -9,18 +9,18 @@ package org.brotli.dec; /** * Enumeration of decoding state-machine. */ -enum RunningState { - UNINITIALIZED, - BLOCK_START, - COMPRESSED_BLOCK_START, - MAIN_LOOP, - READ_METADATA, - COPY_UNCOMPRESSED, - INSERT_LOOP, - COPY_LOOP, - COPY_WRAP_BUFFER, - TRANSFORM, - FINISHED, - CLOSED, - WRITE +final class RunningState { + static final int UNINITIALIZED = 0; + static final int BLOCK_START = 1; + static final int COMPRESSED_BLOCK_START = 2; + static final int MAIN_LOOP = 3; + static final int READ_METADATA = 4; + static final int COPY_UNCOMPRESSED = 5; + static final int INSERT_LOOP = 6; + static final int COPY_LOOP = 7; + static final int COPY_WRAP_BUFFER = 8; + static final int TRANSFORM = 9; + static final int FINISHED = 10; + static final int CLOSED = 11; + static final int WRITE = 12; } diff --git a/java/org/brotli/dec/State.java b/java/org/brotli/dec/State.java index 3da3358..c4646a8 100755 --- a/java/org/brotli/dec/State.java +++ b/java/org/brotli/dec/State.java @@ -14,8 +14,8 @@ import java.io.IOException; import java.io.InputStream; final class State { - RunningState runningState = UNINITIALIZED; - RunningState nextRunningState; + int runningState = UNINITIALIZED; + int nextRunningState; final BitReader br = new BitReader(); byte[] ringBuffer; final int[] blockTypeTrees = new int[3 * Huffman.HUFFMAN_MAX_TABLE_SIZE]; diff --git a/java/org/brotli/dec/Transform.java b/java/org/brotli/dec/Transform.java index ecfef45..59a8450 100755 --- a/java/org/brotli/dec/Transform.java +++ b/java/org/brotli/dec/Transform.java @@ -33,10 +33,10 @@ import static org.brotli.dec.WordTransformType.UPPERCASE_FIRST; final class Transform { private final byte[] prefix; - private final WordTransformType type; + private final int type; private final byte[] suffix; - Transform(String prefix, WordTransformType type, String suffix) { + Transform(String prefix, int type, String suffix) { this.prefix = readUniBytes(prefix); this.type = type; this.suffix = readUniBytes(suffix); @@ -188,14 +188,14 @@ final class Transform { } // Copy trimmed word. - WordTransformType op = transform.type; - tmp = op.omitFirst; + int op = transform.type; + tmp = WordTransformType.getOmitFirst(op); if (tmp > len) { tmp = len; } wordOffset += tmp; len -= tmp; - len -= op.omitLast; + len -= WordTransformType.getOmitLast(op); i = len; while (i > 0) { dst[offset++] = word[wordOffset++]; diff --git a/java/org/brotli/dec/WordTransformType.java b/java/org/brotli/dec/WordTransformType.java index 357b978..ed67b51 100755 --- a/java/org/brotli/dec/WordTransformType.java +++ b/java/org/brotli/dec/WordTransformType.java @@ -12,37 +12,34 @@ package org.brotli.dec; * <p>There are two simple types of transforms: omit X first/last symbols, two character-case * transforms and the identity transform. */ -enum WordTransformType { - IDENTITY(0, 0), - OMIT_LAST_1(0, 1), - OMIT_LAST_2(0, 2), - OMIT_LAST_3(0, 3), - OMIT_LAST_4(0, 4), - OMIT_LAST_5(0, 5), - OMIT_LAST_6(0, 6), - OMIT_LAST_7(0, 7), - OMIT_LAST_8(0, 8), - OMIT_LAST_9(0, 9), - UPPERCASE_FIRST(0, 0), - UPPERCASE_ALL(0, 0), - OMIT_FIRST_1(1, 0), - OMIT_FIRST_2(2, 0), - OMIT_FIRST_3(3, 0), - OMIT_FIRST_4(4, 0), - OMIT_FIRST_5(5, 0), - OMIT_FIRST_6(6, 0), - OMIT_FIRST_7(7, 0), - /* - * brotli specification doesn't use OMIT_FIRST_8(8, 0) transform. - * Probably, it would be used in future format extensions. - */ - OMIT_FIRST_9(9, 0); +final class WordTransformType { + static final int IDENTITY = 0; + static final int OMIT_LAST_1 = 1; + static final int OMIT_LAST_2 = 2; + static final int OMIT_LAST_3 = 3; + static final int OMIT_LAST_4 = 4; + static final int OMIT_LAST_5 = 5; + static final int OMIT_LAST_6 = 6; + static final int OMIT_LAST_7 = 7; + static final int OMIT_LAST_8 = 8; + static final int OMIT_LAST_9 = 9; + static final int UPPERCASE_FIRST = 10; + static final int UPPERCASE_ALL = 11; + static final int OMIT_FIRST_1 = 12; + static final int OMIT_FIRST_2 = 13; + static final int OMIT_FIRST_3 = 14; + static final int OMIT_FIRST_4 = 15; + static final int OMIT_FIRST_5 = 16; + static final int OMIT_FIRST_6 = 17; + static final int OMIT_FIRST_7 = 18; + static final int OMIT_FIRST_8 = 19; + static final int OMIT_FIRST_9 = 20; - final int omitFirst; - final int omitLast; + static int getOmitFirst(int type) { + return type >= OMIT_FIRST_1 ? (type - OMIT_FIRST_1 + 1) : 0; + } - WordTransformType(int omitFirst, int omitLast) { - this.omitFirst = omitFirst; - this.omitLast = omitLast; + static int getOmitLast(int type) { + return type <= OMIT_LAST_9 ? (type - OMIT_LAST_1 + 1) : 0; } } |