From b1df8caa0e464382281a450cc66734362556a975 Mon Sep 17 00:00:00 2001 From: Patrick Favre-Bulle Date: Wed, 15 Feb 2023 19:25:47 +0100 Subject: [PATCH 1/2] Fix bugs found by sonar, mainly adding "& 0xff" to not promote int --- src/main/java/at/favre/lib/bytes/Base64.java | 2 +- src/main/java/at/favre/lib/bytes/Util.java | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/at/favre/lib/bytes/Base64.java b/src/main/java/at/favre/lib/bytes/Base64.java index 730c554..dd31fbf 100644 --- a/src/main/java/at/favre/lib/bytes/Base64.java +++ b/src/main/java/at/favre/lib/bytes/Base64.java @@ -93,7 +93,7 @@ static byte[] decode(CharSequence in) { } // Append this char's 6 bits to the word. - word = (word << 6) | (byte) bits; + word = (word << 6) | (byte) bits & 0xff; // For every 4 chars of input, we accumulate 24 bits of output. Emit 3 bytes. inCount++; diff --git a/src/main/java/at/favre/lib/bytes/Util.java b/src/main/java/at/favre/lib/bytes/Util.java index 710c4d3..4efb673 100644 --- a/src/main/java/at/favre/lib/bytes/Util.java +++ b/src/main/java/at/favre/lib/bytes/Util.java @@ -309,7 +309,7 @@ static byte[] shiftLeft(byte[] byteArray, int shiftBitCount, ByteOrder byteOrder byte src = byteArray[sourceIndex]; byte dst = (byte) (src << shiftMod); if (sourceIndex + 1 < byteArray.length) { - dst |= byteArray[sourceIndex + 1] >>> (8 - shiftMod) & carryMask; + dst |= byteArray[sourceIndex + 1] >>> (8 - shiftMod) & carryMask & 0xff; } byteArray[i] = dst; } @@ -323,7 +323,7 @@ static byte[] shiftLeft(byte[] byteArray, int shiftBitCount, ByteOrder byteOrder byte src = byteArray[sourceIndex]; byte dst = (byte) (src << shiftMod); if (sourceIndex - 1 >= 0) { - dst |= byteArray[sourceIndex - 1] >>> (8 - shiftMod) & carryMask; + dst |= byteArray[sourceIndex - 1] >>> (8 - shiftMod) & carryMask & 0xff; } byteArray[i] = dst; } @@ -365,7 +365,7 @@ static byte[] shiftRight(byte[] byteArray, int shiftBitCount, ByteOrder byteOrde byte src = byteArray[sourceIndex]; byte dst = (byte) ((0xff & src) >>> shiftMod); if (sourceIndex - 1 >= 0) { - dst |= byteArray[sourceIndex - 1] << (8 - shiftMod) & carryMask; + dst |= byteArray[sourceIndex - 1] << (8 - shiftMod) & carryMask & 0xff; } byteArray[i] = dst; } @@ -379,7 +379,7 @@ static byte[] shiftRight(byte[] byteArray, int shiftBitCount, ByteOrder byteOrde byte src = byteArray[sourceIndex]; byte dst = (byte) ((0xff & src) >>> shiftMod); if (sourceIndex + 1 < byteArray.length) { - dst |= byteArray[sourceIndex + 1] << (8 - shiftMod) & carryMask; + dst |= byteArray[sourceIndex + 1] << (8 - shiftMod) & carryMask & 0xff; } byteArray[i] = dst; } From cb0efd0233bcc8fa180704ee18c3f492b6099a59 Mon Sep 17 00:00:00 2001 From: Patrick Favre-Bulle Date: Wed, 15 Feb 2023 19:41:29 +0100 Subject: [PATCH 2/2] Fix various issues mostly javadoc and typos and some simple code fixes --- .../java/at/favre/lib/bytes/BaseEncoding.java | 6 +- src/main/java/at/favre/lib/bytes/Bytes.java | 76 +++++++++---------- .../at/favre/lib/bytes/BytesTransformer.java | 8 +- .../at/favre/lib/bytes/BytesTransformers.java | 10 +-- .../at/favre/lib/bytes/BytesValidators.java | 20 ++--- .../java/at/favre/lib/bytes/MutableBytes.java | 8 +- src/main/java/at/favre/lib/bytes/Util.java | 34 ++++----- 7 files changed, 82 insertions(+), 80 deletions(-) diff --git a/src/main/java/at/favre/lib/bytes/BaseEncoding.java b/src/main/java/at/favre/lib/bytes/BaseEncoding.java index 8fe8d3e..8e23588 100644 --- a/src/main/java/at/favre/lib/bytes/BaseEncoding.java +++ b/src/main/java/at/favre/lib/bytes/BaseEncoding.java @@ -28,10 +28,10 @@ /** * Encoder which supports arbitrary alphabet and padding. - * + *

* Derived from Google Guava's common/io/ BaseEncoding *

- * See: https://github.com/google/guava/blob/v26.0/guava/src/com/google/common/io/BaseEncoding.java + * See: BaseEncoding */ final class BaseEncoding implements BinaryToTextEncoding.EncoderDecoder { private static final char ASCII_MAX = 127; @@ -190,7 +190,7 @@ char encode(int bits) { } int decode(char ch) { - return (int) decodabet[ch]; + return decodabet[ch]; } } diff --git a/src/main/java/at/favre/lib/bytes/Bytes.java b/src/main/java/at/favre/lib/bytes/Bytes.java index c2b5b20..33fb08d 100644 --- a/src/main/java/at/favre/lib/bytes/Bytes.java +++ b/src/main/java/at/favre/lib/bytes/Bytes.java @@ -31,7 +31,7 @@ import java.util.*; /** - * Bytes is wrapper class for an byte-array that allows a lot of convenience operations on it: + * "Bytes" is wrapper class for a byte-array that allows a lot of convenience operations on it: *