You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ratis.apache.org by sz...@apache.org on 2021/05/05 11:31:19 UTC
[ratis] branch master updated: RATIS-1364. Fix Sonar Qube issues in
IOUtils (#468).
This is an automated email from the ASF dual-hosted git repository.
szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ratis.git
The following commit(s) were added to refs/heads/master by this push:
new 9577d56 RATIS-1364. Fix Sonar Qube issues in IOUtils (#468).
9577d56 is described below
commit 9577d564eeac36cf449843d34b7010b33d634818
Author: Roni Juntunen <18...@users.noreply.github.com>
AuthorDate: Wed May 5 14:31:11 2021 +0300
RATIS-1364. Fix Sonar Qube issues in IOUtils (#468).
---
.../src/main/java/org/apache/ratis/util/IOUtils.java | 19 ++++++++-----------
1 file changed, 8 insertions(+), 11 deletions(-)
diff --git a/ratis-common/src/main/java/org/apache/ratis/util/IOUtils.java b/ratis-common/src/main/java/org/apache/ratis/util/IOUtils.java
index 899eb15..bd50a87 100644
--- a/ratis-common/src/main/java/org/apache/ratis/util/IOUtils.java
+++ b/ratis-common/src/main/java/org/apache/ratis/util/IOUtils.java
@@ -33,6 +33,7 @@ import java.net.SocketTimeoutException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel;
+import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
@@ -51,9 +52,8 @@ public interface IOUtils {
}
static IOException asIOException(Throwable t) {
- return t == null? null
- : t instanceof IOException? (IOException)t
- : new IOException(t);
+ Objects.requireNonNull(t, "t == null");
+ return t instanceof IOException? (IOException)t : new IOException(t);
}
static IOException toIOException(ExecutionException e) {
@@ -215,16 +215,13 @@ public interface IOUtils {
}
static <T> T readObject(InputStream in, Class<T> clazz) {
+ Object obj = null;
try(ObjectInputStream oin = new ObjectInputStream(in)) {
- final Object obj = oin.readObject();
- try {
- return clazz.cast(obj);
- } catch (ClassCastException e) {
- throw new IllegalStateException("Failed to cast to " + clazz + ", object="
- + (obj instanceof Throwable? StringUtils.stringifyException((Throwable) obj): obj), e);
- }
+ obj = oin.readObject();
+ return clazz.cast(obj);
} catch (IOException | ClassNotFoundException e) {
- throw new IllegalStateException("Failed to read an object.", e);
+ throw new IllegalStateException("Failed to cast to " + clazz + ", object="
+ + (obj instanceof Throwable? StringUtils.stringifyException((Throwable) obj): obj), e);
}
}
}