Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions ratis-common/src/main/java/org/apache/ratis/util/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
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;
Expand All @@ -51,9 +52,8 @@ static InterruptedIOException toInterruptedIOException(
}

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) {
Expand Down Expand Up @@ -215,16 +215,13 @@ static <T> T bytes2Object(byte[] bytes, Class<T> clazz) {
}

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);
}
}
}