From 6ba678a7ce0f85f980351ed388c47ed6812ba56c Mon Sep 17 00:00:00 2001 From: Evgenii Kliuchnikov Date: Wed, 29 Nov 2023 10:47:47 -0800 Subject: pull "InputStream" reference out of "pure" code PiperOrigin-RevId: 586390725 --- java/org/brotli/dec/BitReader.java | 4 ++-- java/org/brotli/dec/BitReaderTest.java | 6 ++++-- java/org/brotli/dec/BrotliInputStream.java | 3 ++- java/org/brotli/dec/Decode.java | 9 ++------- java/org/brotli/dec/Utils.java | 12 +++++++----- 5 files changed, 17 insertions(+), 17 deletions(-) (limited to 'java') diff --git a/java/org/brotli/dec/BitReader.java b/java/org/brotli/dec/BitReader.java index bae9452..c0a15d2 100644 --- a/java/org/brotli/dec/BitReader.java +++ b/java/org/brotli/dec/BitReader.java @@ -68,7 +68,7 @@ final class BitReader { s.halfOffset = 0; while (bytesInBuffer < CAPACITY) { final int spaceLeft = CAPACITY - bytesInBuffer; - final int len = Utils.readInput(s.input, s.byteBuffer, bytesInBuffer, spaceLeft); + final int len = Utils.readInput(s, s.byteBuffer, bytesInBuffer, spaceLeft); // EOF is -1 in Java, but 0 in C#. if (len <= 0) { s.endOfStreamReached = 1; @@ -255,7 +255,7 @@ final class BitReader { // Now it is possible to copy bytes directly. while (length > 0) { - final int len = Utils.readInput(s.input, data, offset, length); + final int len = Utils.readInput(s, data, offset, length); if (len == -1) { throw new BrotliRuntimeException("Unexpected end of input"); } diff --git a/java/org/brotli/dec/BitReaderTest.java b/java/org/brotli/dec/BitReaderTest.java index da59ebc..6a66c6b 100644 --- a/java/org/brotli/dec/BitReaderTest.java +++ b/java/org/brotli/dec/BitReaderTest.java @@ -22,7 +22,8 @@ public class BitReaderTest { @Test public void testReadAfterEos() { State reader = new State(); - Decode.initState(reader, new ByteArrayInputStream(new byte[1])); + reader.input = new ByteArrayInputStream(new byte[1]); + Decode.initState(reader); BitReader.readBits(reader, 9); try { BitReader.checkHealth(reader, 0); @@ -36,7 +37,8 @@ public class BitReaderTest { @Test public void testAccumulatorUnderflowDetected() { State reader = new State(); - Decode.initState(reader, new ByteArrayInputStream(new byte[8])); + reader.input = new ByteArrayInputStream(new byte[8]); + Decode.initState(reader); // 65 bits is enough for both 32 and 64 bit systems. BitReader.readBits(reader, 13); BitReader.readBits(reader, 13); diff --git a/java/org/brotli/dec/BrotliInputStream.java b/java/org/brotli/dec/BrotliInputStream.java index 7bbe2f6..46df98f 100644 --- a/java/org/brotli/dec/BrotliInputStream.java +++ b/java/org/brotli/dec/BrotliInputStream.java @@ -86,7 +86,8 @@ public class BrotliInputStream extends InputStream { this.remainingBufferBytes = 0; this.bufferOffset = 0; try { - Decode.initState(state, source); + state.input = source; + Decode.initState(state); } catch (BrotliRuntimeException ex) { throw new IOException("Brotli decoder initialization failed", ex); } diff --git a/java/org/brotli/dec/Decode.java b/java/org/brotli/dec/Decode.java index bf9b681..ebe25d1 100644 --- a/java/org/brotli/dec/Decode.java +++ b/java/org/brotli/dec/Decode.java @@ -7,7 +7,6 @@ package org.brotli.dec; import java.io.IOException; -import java.io.InputStream; import java.nio.ByteBuffer; /** @@ -290,7 +289,7 @@ final class Decode { * @param s uninitialized state without associated input * @param input compressed data source */ - static void initState(State s, InputStream input) { + static void initState(State s) { if (s.runningState != UNINITIALIZED) { throw new IllegalStateException("State MUST be uninitialized"); } @@ -302,7 +301,6 @@ final class Decode { calculateDistanceAlphabetLimit(MAX_ALLOWED_DISTANCE, 3, 15 << 3); s.distExtraBits = new byte[maxDistanceAlphabetLimit]; s.distOffset = new int[maxDistanceAlphabetLimit]; - s.input = input; BitReader.initBitReader(s); s.runningState = INITIALIZED; } @@ -315,10 +313,7 @@ final class Decode { return; } s.runningState = CLOSED; - if (s.input != null) { - Utils.closeInput(s.input); - s.input = null; - } + Utils.closeInput(s); } /** diff --git a/java/org/brotli/dec/Utils.java b/java/org/brotli/dec/Utils.java index cc4a9f0..ff5f65d 100644 --- a/java/org/brotli/dec/Utils.java +++ b/java/org/brotli/dec/Utils.java @@ -7,7 +7,6 @@ package org.brotli.dec; import java.io.IOException; -import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.nio.Buffer; import java.nio.ByteBuffer; @@ -67,16 +66,19 @@ final class Utils { System.arraycopy(bytes, start, bytes, target, end - start); } - static int readInput(InputStream src, byte[] dst, int offset, int length) { + static int readInput(State s, byte[] dst, int offset, int length) { try { - return src.read(dst, offset, length); + return s.input.read(dst, offset, length); } catch (IOException e) { throw new BrotliRuntimeException("Failed to read input", e); } } - static void closeInput(InputStream src) throws IOException { - src.close(); + static void closeInput(State s) throws IOException { + if (s.input != null) { + s.input.close(); + s.input = null; + } } static byte[] toUsAsciiBytes(String src) { -- cgit v1.1