Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs found by sonar, mainly adding "& 0xff" to not promote int #53

Merged
merged 2 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/main/java/at/favre/lib/bytes/Base64.java
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/at/favre/lib/bytes/BaseEncoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@

/**
* Encoder which supports arbitrary alphabet and padding.
*
* <p>
* Derived from Google Guava's common/io/ BaseEncoding
* <p>
* See: https://github.com/google/guava/blob/v26.0/guava/src/com/google/common/io/BaseEncoding.java
* See: <a href="https://github.com/google/guava/blob/v26.0/guava/src/com/google/common/io/BaseEncoding.java">BaseEncoding</a>
*/
final class BaseEncoding implements BinaryToTextEncoding.EncoderDecoder {
private static final char ASCII_MAX = 127;
Expand Down Expand Up @@ -190,7 +190,7 @@ char encode(int bits) {
}

int decode(char ch) {
return (int) decodabet[ch];
return decodabet[ch];
}
}

Expand Down
76 changes: 38 additions & 38 deletions src/main/java/at/favre/lib/bytes/Bytes.java

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions src/main/java/at/favre/lib/bytes/BytesTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public boolean supportInPlaceTransformation() {
* contain identical values. For any indices that are valid in the
* copy but not the original, the copy will contain {@code (byte)0}.
* <p>
* If if the internal array will be grown, zero bytes will be added on the left,
* If the internal array will be increased, zero bytes will be added on the left,
* keeping the value the same.
*/
final class ResizeTransformer implements BytesTransformer {
Expand Down Expand Up @@ -265,10 +265,12 @@ public byte[] transform(byte[] currentArray, boolean inPlace) {
byte[] resizedArray = new byte[newSize];

if (mode == Mode.RESIZE_KEEP_FROM_MAX_LENGTH) {
int max = Math.max(0, Math.abs(newSize - currentArray.length));

if (newSize > currentArray.length) {
System.arraycopy(currentArray, 0, resizedArray, Math.max(0, Math.abs(newSize - currentArray.length)), Math.min(newSize, currentArray.length));
System.arraycopy(currentArray, 0, resizedArray, max, currentArray.length);
} else {
System.arraycopy(currentArray, Math.max(0, Math.abs(newSize - currentArray.length)), resizedArray, Math.min(0, Math.abs(newSize - currentArray.length)), Math.min(newSize, currentArray.length));
System.arraycopy(currentArray, max, resizedArray, 0, newSize);
}
} else {
System.arraycopy(currentArray, 0, resizedArray, 0, Math.min(currentArray.length, resizedArray.length));
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/at/favre/lib/bytes/BytesTransformers.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static BytesTransformer shuffle(Random random) {
}

/**
* Create a {@link BytesTransformer} which sorts the internal byte array with it's natural ordering treating
* Create a {@link BytesTransformer} which sorts the internal byte array with its natural ordering treating
* each byte as signed byte (-128...127). Using inplace sorting, this can be reasonable fast.
*
* @return transformer
Expand All @@ -54,9 +54,9 @@ public static BytesTransformer sort() {
}

/**
* Create a {@link BytesTransformer} which sorts the internal byte array with it's natural ordering treating
* Create a {@link BytesTransformer} which sorts the internal byte array with its natural ordering treating
* each byte as unsigned byte (0...255). That is, the byte string {@code ff} sorts after {@code 00}.
*
* <p>
* <strong>Note:</strong> this requires 2 copies of the internal array and a lot of unboxing due to
* the fact that no primitives are not allowed as generic type arguments - so only use on small arrays.
*
Expand All @@ -68,7 +68,7 @@ public static BytesTransformer sortUnsigned() {

/**
* Create a {@link BytesTransformer} which sorts the internal byte array according to given comparator.
*
* <p>
* <strong>Note:</strong> this requires 2 copies of the internal array and a lot of unboxing due to
* the fact that no primitives are not allowed as generic type arguments - so only use on small arrays.
*
Expand Down Expand Up @@ -192,7 +192,7 @@ public boolean supportInPlaceTransformation() {
* Sorts the internal byte array with given {@link java.util.Comparator}
*/
public static final class SortTransformer implements BytesTransformer {
private final Comparator comparator;
private final Comparator<Byte> comparator;

SortTransformer() {
this(null);
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/at/favre/lib/bytes/BytesValidators.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private BytesValidators() {
* Checks the length of a byte array
*
* @param byteLength to check against
* @return true if longer or equal to given value
* @return validator that returns true if longer or equal to given value
*/
public static BytesValidator atLeast(int byteLength) {
return new BytesValidator.Length(byteLength, BytesValidator.Length.Mode.GREATER_OR_EQ_THAN);
Expand All @@ -47,7 +47,7 @@ public static BytesValidator atLeast(int byteLength) {
* Checks the length of a byte array
*
* @param byteLength to check against
* @return true if smaller or equal to given value
* @return validator that returns true if smaller or equal to given value
*/
public static BytesValidator atMost(int byteLength) {
return new BytesValidator.Length(byteLength, BytesValidator.Length.Mode.SMALLER_OR_EQ_THAN);
Expand All @@ -57,7 +57,7 @@ public static BytesValidator atMost(int byteLength) {
* Checks the length of a byte array
*
* @param byteLength to check against
* @return true if equal to given value
* @return validator that returns true if equal to given value
*/
public static BytesValidator exactLength(int byteLength) {
return new BytesValidator.Length(byteLength, BytesValidator.Length.Mode.EXACT);
Expand All @@ -67,7 +67,7 @@ public static BytesValidator exactLength(int byteLength) {
* Checks individual byte content
*
* @param refByte to check against
* @return true if array only consists of refByte
* @return validator that returns true if array only consists of refByte
*/
public static BytesValidator onlyOf(byte refByte) {
return new BytesValidator.IdenticalContent(refByte, BytesValidator.IdenticalContent.Mode.ONLY_OF);
Expand All @@ -77,7 +77,7 @@ public static BytesValidator onlyOf(byte refByte) {
* Checks individual byte content
*
* @param refByte to check against
* @return true if array has at least one byte that is not refByte
* @return validator that returns true if array has at least one byte that is not refByte
*/
public static BytesValidator notOnlyOf(byte refByte) {
return new BytesValidator.IdenticalContent(refByte, BytesValidator.IdenticalContent.Mode.NOT_ONLY_OF);
Expand All @@ -87,7 +87,7 @@ public static BytesValidator notOnlyOf(byte refByte) {
* Checks if the internal byte array starts with given bytes
*
* @param startsWithBytes the supposed prefix
* @return true all startsWithBytes match the first bytes in the internal array
* @return validator that returns true all startsWithBytes match the first bytes in the internal array
*/
public static BytesValidator startsWith(byte... startsWithBytes) {
return new BytesValidator.PrePostFix(true, startsWithBytes);
Expand All @@ -97,7 +97,7 @@ public static BytesValidator startsWith(byte... startsWithBytes) {
* Checks if the internal byte array ends with given bytes
*
* @param endsWithBytes the supposed postfix
* @return true all startsWithBytes match the first bytes in the internal array
* @return validator that returns true all startsWithBytes match the first bytes in the internal array
*/
public static BytesValidator endsWith(byte... endsWithBytes) {
return new BytesValidator.PrePostFix(false, endsWithBytes);
Expand All @@ -107,7 +107,7 @@ public static BytesValidator endsWith(byte... endsWithBytes) {
* Checks individual byte content
*
* @param refByte to check against
* @return true if array has no value refByte
* @return validator that returns true if array has no value refByte
*/
public static BytesValidator noneOf(byte refByte) {
return new BytesValidator.IdenticalContent(refByte, BytesValidator.IdenticalContent.Mode.NONE_OF);
Expand All @@ -117,7 +117,7 @@ public static BytesValidator noneOf(byte refByte) {
* This will execute all passed validators and returns true if at least one returns true (i.e. OR concatenation)
*
* @param validators at least one validator must be passed
* @return true if at least one validator returns true
* @return validator that returns true if at least one validator returns true
*/
public static BytesValidator or(BytesValidator... validators) {
return new BytesValidator.Logical(Arrays.asList(validators), BytesValidator.Logical.Operator.OR);
Expand All @@ -127,7 +127,7 @@ public static BytesValidator or(BytesValidator... validators) {
* This will execute all passed validators and returns true if all return true (i.e. AND concatenation)
*
* @param validators at least one validator must be passed
* @return true if all return true
* @return validator that returns true if all return true
*/
public static BytesValidator and(BytesValidator... validators) {
return new BytesValidator.Logical(Arrays.asList(validators), BytesValidator.Logical.Operator.AND);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/at/favre/lib/bytes/MutableBytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public boolean isMutable() {
*
* @param newArray used to overwrite internal
* @return this instance
* @throws IndexOutOfBoundsException if newArray.length &gt; internal length
* @throws IndexOutOfBoundsException if newArray.length() &gt; internal length
*/
public MutableBytes overwrite(byte[] newArray) {
return overwrite(newArray, 0);
Expand All @@ -80,7 +80,7 @@ public MutableBytes overwrite(byte[] newArray) {
*
* @param newBytes used to overwrite internal
* @return this instance
* @throws IndexOutOfBoundsException if newArray.length &gt; internal length
* @throws IndexOutOfBoundsException if newArray.length() &gt; internal length
*/
public MutableBytes overwrite(Bytes newBytes) {
return overwrite(newBytes, 0);
Expand All @@ -92,7 +92,7 @@ public MutableBytes overwrite(Bytes newBytes) {
* @param newArray used to overwrite internal
* @param offsetInternalArray index of the internal array to start overwriting
* @return this instance
* @throws IndexOutOfBoundsException if newArray.length + offsetInternalArray &gt; internal length
* @throws IndexOutOfBoundsException if newArray.length() + offsetInternalArray &gt; internal length
*/
public MutableBytes overwrite(byte[] newArray, int offsetInternalArray) {
Objects.requireNonNull(newArray, "must provide non-null array as source");
Expand All @@ -106,7 +106,7 @@ public MutableBytes overwrite(byte[] newArray, int offsetInternalArray) {
* @param newBytes used to overwrite internal
* @param offsetInternalArray index of the internal array to start overwriting
* @return this instance
* @throws IndexOutOfBoundsException if newBytes.length + offsetInternalArray &gt; internal length
* @throws IndexOutOfBoundsException if newBytes.length() + offsetInternalArray &gt; internal length
*/
public MutableBytes overwrite(Bytes newBytes, int offsetInternalArray) {
return overwrite(Objects.requireNonNull(newBytes, "must provide non-null array as source").array(), offsetInternalArray);
Expand Down
Loading