From 2843bb280b95972e61c0d05841f84ecd9c2a7a49 Mon Sep 17 00:00:00 2001 From: Nicholas Walter Knize Date: Mon, 19 Jun 2023 11:21:33 -0500 Subject: [PATCH] more PR cleanup Signed-off-by: Nicholas Walter Knize --- .../java/org/opensearch/ExceptionsHelper.java | 2 +- .../DefaultShardOperationFailedException.java | 5 +-- .../opensearch/common/ParsingException.java | 3 +- .../io/stream/InputStreamStreamInput.java | 4 +-- .../core/common/io/StreamsUtil.java | 34 ------------------- .../org/opensearch/common/io/Streams.java | 3 +- .../org/opensearch/index/store/Store.java | 3 +- 7 files changed, 8 insertions(+), 46 deletions(-) delete mode 100644 libs/core/src/main/java/org/opensearch/core/common/io/StreamsUtil.java diff --git a/libs/core/src/main/java/org/opensearch/ExceptionsHelper.java b/libs/core/src/main/java/org/opensearch/ExceptionsHelper.java index d5c905f5dc528..385dc24a0c25d 100644 --- a/libs/core/src/main/java/org/opensearch/ExceptionsHelper.java +++ b/libs/core/src/main/java/org/opensearch/ExceptionsHelper.java @@ -72,7 +72,7 @@ * @opensearch.internal */ public final class ExceptionsHelper { - protected static final Logger logger = LogManager.getLogger(ExceptionsHelper.class); + private static final Logger logger = LogManager.getLogger(ExceptionsHelper.class); // utility class: no ctor private ExceptionsHelper() {} diff --git a/libs/core/src/main/java/org/opensearch/action/support/DefaultShardOperationFailedException.java b/libs/core/src/main/java/org/opensearch/action/support/DefaultShardOperationFailedException.java index 71f0980e56d4b..5a7e48a5efef7 100644 --- a/libs/core/src/main/java/org/opensearch/action/support/DefaultShardOperationFailedException.java +++ b/libs/core/src/main/java/org/opensearch/action/support/DefaultShardOperationFailedException.java @@ -47,6 +47,7 @@ import java.io.IOException; import static org.opensearch.ExceptionsHelper.detailedMessage; +import static org.opensearch.OpenSearchException.generateThrowableXContent; import static org.opensearch.core.xcontent.ConstructingObjectParser.constructorArg; /** @@ -93,7 +94,7 @@ public DefaultShardOperationFailedException(OpenSearchException e) { } public DefaultShardOperationFailedException(String index, int shardId, Throwable cause) { - super(index, shardId, ExceptionsHelper.detailedMessage(cause), ExceptionsHelper.status(cause), cause); + super(index, shardId, detailedMessage(cause), ExceptionsHelper.status(cause), cause); } public static DefaultShardOperationFailedException readShardOperationFailed(StreamInput in) throws IOException { @@ -135,7 +136,7 @@ protected XContentBuilder innerToXContent(XContentBuilder builder, Params params builder.field("status", status.name()); if (reason != null) { builder.startObject("reason"); - OpenSearchException.generateThrowableXContent(builder, params, cause); + generateThrowableXContent(builder, params, cause); builder.endObject(); } return builder; diff --git a/libs/core/src/main/java/org/opensearch/common/ParsingException.java b/libs/core/src/main/java/org/opensearch/common/ParsingException.java index 5991cc3d77eff..5a9e99f5987d9 100644 --- a/libs/core/src/main/java/org/opensearch/common/ParsingException.java +++ b/libs/core/src/main/java/org/opensearch/common/ParsingException.java @@ -35,7 +35,6 @@ import org.opensearch.OpenSearchException; import org.opensearch.common.io.stream.StreamInput; import org.opensearch.common.io.stream.StreamOutput; -import org.opensearch.core.xcontent.ToXContent; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.core.xcontent.XContentLocation; import org.opensearch.core.xcontent.XContentParser; @@ -112,7 +111,7 @@ public RestStatus status() { } @Override - protected void metadataToXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { + protected void metadataToXContent(XContentBuilder builder, Params params) throws IOException { if (lineNumber != UNKNOWN_POSITION) { builder.field("line", lineNumber); builder.field("col", columnNumber); diff --git a/libs/core/src/main/java/org/opensearch/common/io/stream/InputStreamStreamInput.java b/libs/core/src/main/java/org/opensearch/common/io/stream/InputStreamStreamInput.java index a4bbdd1bfb1ac..06e10c67cf972 100644 --- a/libs/core/src/main/java/org/opensearch/common/io/stream/InputStreamStreamInput.java +++ b/libs/core/src/main/java/org/opensearch/common/io/stream/InputStreamStreamInput.java @@ -32,8 +32,6 @@ package org.opensearch.common.io.stream; -import org.opensearch.core.common.io.StreamsUtil; - import java.io.EOFException; import java.io.IOException; import java.io.InputStream; @@ -80,7 +78,7 @@ public byte readByte() throws IOException { @Override public void readBytes(byte[] b, int offset, int len) throws IOException { if (len < 0) throw new IndexOutOfBoundsException(); - final int read = StreamsUtil.readFully(is, b, offset, len); + final int read = is.readNBytes(b, offset, len); if (read != len) { throw new EOFException(); } diff --git a/libs/core/src/main/java/org/opensearch/core/common/io/StreamsUtil.java b/libs/core/src/main/java/org/opensearch/core/common/io/StreamsUtil.java deleted file mode 100644 index 6ef33a190c352..0000000000000 --- a/libs/core/src/main/java/org/opensearch/core/common/io/StreamsUtil.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -package org.opensearch.core.common.io; - -import java.io.IOException; -import java.io.InputStream; - -/** - * Util library for reading streams - * - * @opensearch.internal - */ -public final class StreamsUtil { - - private StreamsUtil() {} - - public static int readFully(InputStream reader, byte[] dest, int offset, int len) throws IOException { - int read = 0; - while (read < len) { - final int r = reader.read(dest, offset + read, len - read); - if (r == -1) { - break; - } - read += r; - } - return read; - } -} diff --git a/server/src/main/java/org/opensearch/common/io/Streams.java b/server/src/main/java/org/opensearch/common/io/Streams.java index 2a833b81cacca..6e44f18b7f4d5 100644 --- a/server/src/main/java/org/opensearch/common/io/Streams.java +++ b/server/src/main/java/org/opensearch/common/io/Streams.java @@ -36,7 +36,6 @@ import org.opensearch.common.io.stream.BytesStream; import org.opensearch.common.io.stream.BytesStreamOutput; import org.opensearch.common.io.stream.StreamOutput; -import org.opensearch.core.common.io.StreamsUtil; import java.io.BufferedReader; import java.io.FilterInputStream; @@ -165,7 +164,7 @@ public static String copyToString(Reader in) throws IOException { @Deprecated public static int readFully(InputStream reader, byte[] dest) throws IOException { - return StreamsUtil.readFully(reader, dest, 0, dest.length); + return reader.readNBytes(dest, 0, dest.length); } /** diff --git a/server/src/main/java/org/opensearch/index/store/Store.java b/server/src/main/java/org/opensearch/index/store/Store.java index edb1473724e07..90832b4c77756 100644 --- a/server/src/main/java/org/opensearch/index/store/Store.java +++ b/server/src/main/java/org/opensearch/index/store/Store.java @@ -82,7 +82,6 @@ import org.opensearch.common.util.concurrent.RefCounted; import org.opensearch.common.util.iterable.Iterables; import org.opensearch.common.util.io.IOUtils; -import org.opensearch.core.common.io.StreamsUtil; import org.opensearch.env.NodeEnvironment; import org.opensearch.env.ShardLock; import org.opensearch.env.ShardLockObtainFailedException; @@ -1240,7 +1239,7 @@ public static void hashFile(BytesRefBuilder fileHash, InputStream in, long size) final int len = (int) Math.min(1024 * 1024, size); // for safety we limit this to 1MB fileHash.grow(len); fileHash.setLength(len); - final int readBytes = StreamsUtil.readFully(in, fileHash.bytes(), 0, len); + final int readBytes = in.readNBytes(fileHash.bytes(), 0, len); assert readBytes == len : Integer.toString(readBytes) + " != " + Integer.toString(len); assert fileHash.length() == len : Integer.toString(fileHash.length()) + " != " + Integer.toString(len); }