You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/03/17 20:41:37 UTC
[commons-compress] 02/02: Refactor duplicate code, no magic numbers
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit d2a862c359b119331ee5ed74a9e842c4e5d1abea
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Mar 17 16:41:30 2023 -0400
Refactor duplicate code, no magic numbers
- Better exception message for debugging
- More precise throws signature in some private method
---
.../compress/archivers/sevenz/SevenZFile.java | 41 +++++++++++-----------
1 file changed, 21 insertions(+), 20 deletions(-)
diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
index 44e5d240..7622b4e4 100644
--- a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
+++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
@@ -177,31 +177,32 @@ public class SevenZFile implements Closeable {
}
return (int) value;
}
- private static void get(final ByteBuffer buf, final byte[] to) throws IOException {
- if (buf.remaining() < to.length) {
- throw new EOFException();
+
+ private static ByteBuffer checkEndOfFile(final ByteBuffer buf, final int expectRemaining) throws EOFException {
+ final int remaining = buf.remaining();
+ if (remaining < expectRemaining) {
+ throw new EOFException(String.format("remaining %,d < expectRemaining %,d", remaining, expectRemaining));
}
- buf.get(to);
+ return buf;
}
- private static char getChar(final ByteBuffer buf) throws IOException {
- if (buf.remaining() < 2) {
- throw new EOFException();
- }
- return buf.getChar();
+
+ private static void get(final ByteBuffer buf, final byte[] to) throws EOFException {
+ checkEndOfFile(buf, to.length).get(to);
}
- private static int getInt(final ByteBuffer buf) throws IOException {
- if (buf.remaining() < 4) {
- throw new EOFException();
- }
- return buf.getInt();
+
+ private static char getChar(final ByteBuffer buf) throws EOFException {
+ return checkEndOfFile(buf, Character.BYTES).getChar();
}
- private static long getLong(final ByteBuffer buf) throws IOException {
- if (buf.remaining() < 8) {
- throw new EOFException();
- }
- return buf.getLong();
+
+ private static int getInt(final ByteBuffer buf) throws EOFException {
+ return checkEndOfFile(buf, Integer.BYTES).getInt();
}
- private static int getUnsignedByte(final ByteBuffer buf) throws IOException {
+
+ private static long getLong(final ByteBuffer buf) throws EOFException {
+ return checkEndOfFile(buf, Long.BYTES).getLong();
+ }
+
+ private static int getUnsignedByte(final ByteBuffer buf) throws EOFException {
if (!buf.hasRemaining()) {
throw new EOFException();
}