From b7814c9de60fe60e72dce59c840ae84733165b85 Mon Sep 17 00:00:00 2001 From: Tianli Feng Date: Thu, 3 Nov 2022 14:37:09 -0700 Subject: [PATCH] Revert "Fix bug in SlicedInputStream with zero length (#4863) (#5044)" This reverts commit 870d13d1c40b2691d8e2dedadb0d199fb1bb965b. --- CHANGELOG.md | 1 - .../blobstore/SlicedInputStream.java | 6 -- .../blobstore/SlicedInputStreamTests.java | 70 +++++-------------- 3 files changed, 16 insertions(+), 61 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b2e2db957d66..6bc19960c53a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -133,7 +133,6 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Better plural stemmer than minimal_english ([#4738](https://github.com/opensearch-project/OpenSearch/pull/4738)) - Fix AbstractStringFieldDataTestCase tests to account for TotalHits lower bound ([4867](https://github.com/opensearch-project/OpenSearch/pull/4867)) - [Segment Replication] Fix bug of replica shard's translog not purging on index flush when segment replication is enabled ([4975](https://github.com/opensearch-project/OpenSearch/pull/4975)) -- Fix bug in SlicedInputStream with zero length ([#4863](https://github.com/opensearch-project/OpenSearch/pull/4863)) - ### Security diff --git a/server/src/main/java/org/opensearch/index/snapshots/blobstore/SlicedInputStream.java b/server/src/main/java/org/opensearch/index/snapshots/blobstore/SlicedInputStream.java index ae2e33d41c6ce..b9b27547fe50c 100644 --- a/server/src/main/java/org/opensearch/index/snapshots/blobstore/SlicedInputStream.java +++ b/server/src/main/java/org/opensearch/index/snapshots/blobstore/SlicedInputStream.java @@ -35,7 +35,6 @@ import java.io.IOException; import java.io.InputStream; -import java.util.Objects; /** * A {@link SlicedInputStream} is a logical @@ -101,11 +100,6 @@ public final int read() throws IOException { @Override public final int read(byte[] buffer, int offset, int length) throws IOException { - Objects.checkFromIndexSize(offset, length, buffer.length); - if (length == 0) { - return 0; - } - final InputStream stream = currentStream(); if (stream == null) { return -1; diff --git a/server/src/test/java/org/opensearch/index/snapshots/blobstore/SlicedInputStreamTests.java b/server/src/test/java/org/opensearch/index/snapshots/blobstore/SlicedInputStreamTests.java index 76fb8f62b5468..3e337bbe3adae 100644 --- a/server/src/test/java/org/opensearch/index/snapshots/blobstore/SlicedInputStreamTests.java +++ b/server/src/test/java/org/opensearch/index/snapshots/blobstore/SlicedInputStreamTests.java @@ -32,8 +32,6 @@ package org.opensearch.index.snapshots.blobstore; import com.carrotsearch.randomizedtesting.generators.RandomNumbers; - -import org.hamcrest.MatcherAssert; import org.opensearch.test.OpenSearchTestCase; import java.io.ByteArrayInputStream; @@ -41,9 +39,6 @@ import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; import java.util.Random; import static org.hamcrest.Matchers.equalTo; @@ -91,9 +86,11 @@ protected InputStream openSlice(int slice) throws IOException { assertThat(random.nextInt(Byte.MAX_VALUE), equalTo(input.read())); break; default: - byte[] expectedBytes = randomBytes(random); - byte[] actualBytes = input.readNBytes(expectedBytes.length); - assertArrayEquals(expectedBytes, actualBytes); + byte[] b = randomBytes(random); + byte[] buffer = new byte[b.length]; + int read = readFully(input, buffer); + assertThat(b.length, equalTo(read)); + assertArrayEquals(b, buffer); break; } } @@ -110,45 +107,19 @@ protected InputStream openSlice(int slice) throws IOException { } - public void testReadZeroLength() throws IOException { - try (InputStream input = createSingleByteStream()) { - final byte[] buffer = new byte[100]; - final int read = input.read(buffer, 0, 0); - MatcherAssert.assertThat(read, equalTo(0)); - } - } - - public void testInvalidOffsetAndLength() throws IOException { - try (InputStream input = createSingleByteStream()) { - final byte[] buffer = new byte[100]; - expectThrows(NullPointerException.class, () -> input.read(null, 0, 10)); - expectThrows(IndexOutOfBoundsException.class, () -> input.read(buffer, -1, 10)); - expectThrows(IndexOutOfBoundsException.class, () -> input.read(buffer, 0, -1)); - expectThrows(IndexOutOfBoundsException.class, () -> input.read(buffer, 0, buffer.length + 1)); - } - } - - public void testReadAllBytes() throws IOException { - final byte[] expectedResults = randomByteArrayOfLength(50_000); - final int numSlices = 200; - final int slizeSize = expectedResults.length / numSlices; - - final List arraySlices = new ArrayList<>(numSlices); - for (int i = 0; i < numSlices; i++) { - final int offset = slizeSize * i; - arraySlices.add(Arrays.copyOfRange(expectedResults, offset, offset + slizeSize)); - } - // Create a SlicedInputStream that will return the expected result in 2 slices - final byte[] actualResults; - try (InputStream is = new SlicedInputStream(numSlices) { - @Override - protected InputStream openSlice(int slice) { - return new ByteArrayInputStream(arraySlices.get(slice)); + private int readFully(InputStream stream, byte[] buffer) throws IOException { + for (int i = 0; i < buffer.length;) { + int read = stream.read(buffer, i, buffer.length - i); + if (read == -1) { + if (i == 0) { + return -1; + } else { + return i; + } } - }) { - actualResults = is.readAllBytes(); + i += read; } - assertArrayEquals(expectedResults, actualResults); + return buffer.length; } private byte[] randomBytes(Random random) { @@ -158,15 +129,6 @@ private byte[] randomBytes(Random random) { return data; } - private static InputStream createSingleByteStream() { - return new SlicedInputStream(1) { - @Override - protected InputStream openSlice(int slice) { - return new ByteArrayInputStream(new byte[] { 1 }); - } - }; - } - private static final class CheckClosedInputStream extends FilterInputStream { public boolean closed = false;