Skip to content

Commit 7f6caed

Browse files
authored
Merge pull request #53 from patrickfav/feature/bugfixes-0xff
2 parents 8985a4a + cb0efd0 commit 7f6caed

File tree

8 files changed

+87
-85
lines changed

8 files changed

+87
-85
lines changed

src/main/java/at/favre/lib/bytes/Base64.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static byte[] decode(CharSequence in) {
9393
}
9494

9595
// Append this char's 6 bits to the word.
96-
word = (word << 6) | (byte) bits;
96+
word = (word << 6) | (byte) bits & 0xff;
9797

9898
// For every 4 chars of input, we accumulate 24 bits of output. Emit 3 bytes.
9999
inCount++;

src/main/java/at/favre/lib/bytes/BaseEncoding.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828

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

192192
int decode(char ch) {
193-
return (int) decodabet[ch];
193+
return decodabet[ch];
194194
}
195195
}
196196

src/main/java/at/favre/lib/bytes/Bytes.java

Lines changed: 38 additions & 38 deletions
Large diffs are not rendered by default.

src/main/java/at/favre/lib/bytes/BytesTransformer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public boolean supportInPlaceTransformation() {
231231
* contain identical values. For any indices that are valid in the
232232
* copy but not the original, the copy will contain {@code (byte)0}.
233233
* <p>
234-
* If if the internal array will be grown, zero bytes will be added on the left,
234+
* If the internal array will be increased, zero bytes will be added on the left,
235235
* keeping the value the same.
236236
*/
237237
final class ResizeTransformer implements BytesTransformer {
@@ -265,10 +265,12 @@ public byte[] transform(byte[] currentArray, boolean inPlace) {
265265
byte[] resizedArray = new byte[newSize];
266266

267267
if (mode == Mode.RESIZE_KEEP_FROM_MAX_LENGTH) {
268+
int max = Math.max(0, Math.abs(newSize - currentArray.length));
269+
268270
if (newSize > currentArray.length) {
269-
System.arraycopy(currentArray, 0, resizedArray, Math.max(0, Math.abs(newSize - currentArray.length)), Math.min(newSize, currentArray.length));
271+
System.arraycopy(currentArray, 0, resizedArray, max, currentArray.length);
270272
} else {
271-
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));
273+
System.arraycopy(currentArray, max, resizedArray, 0, newSize);
272274
}
273275
} else {
274276
System.arraycopy(currentArray, 0, resizedArray, 0, Math.min(currentArray.length, resizedArray.length));

src/main/java/at/favre/lib/bytes/BytesTransformers.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static BytesTransformer shuffle(Random random) {
4444
}
4545

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

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

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

197197
SortTransformer() {
198198
this(null);

src/main/java/at/favre/lib/bytes/BytesValidators.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private BytesValidators() {
3737
* Checks the length of a byte array
3838
*
3939
* @param byteLength to check against
40-
* @return true if longer or equal to given value
40+
* @return validator that returns true if longer or equal to given value
4141
*/
4242
public static BytesValidator atLeast(int byteLength) {
4343
return new BytesValidator.Length(byteLength, BytesValidator.Length.Mode.GREATER_OR_EQ_THAN);
@@ -47,7 +47,7 @@ public static BytesValidator atLeast(int byteLength) {
4747
* Checks the length of a byte array
4848
*
4949
* @param byteLength to check against
50-
* @return true if smaller or equal to given value
50+
* @return validator that returns true if smaller or equal to given value
5151
*/
5252
public static BytesValidator atMost(int byteLength) {
5353
return new BytesValidator.Length(byteLength, BytesValidator.Length.Mode.SMALLER_OR_EQ_THAN);
@@ -57,7 +57,7 @@ public static BytesValidator atMost(int byteLength) {
5757
* Checks the length of a byte array
5858
*
5959
* @param byteLength to check against
60-
* @return true if equal to given value
60+
* @return validator that returns true if equal to given value
6161
*/
6262
public static BytesValidator exactLength(int byteLength) {
6363
return new BytesValidator.Length(byteLength, BytesValidator.Length.Mode.EXACT);
@@ -67,7 +67,7 @@ public static BytesValidator exactLength(int byteLength) {
6767
* Checks individual byte content
6868
*
6969
* @param refByte to check against
70-
* @return true if array only consists of refByte
70+
* @return validator that returns true if array only consists of refByte
7171
*/
7272
public static BytesValidator onlyOf(byte refByte) {
7373
return new BytesValidator.IdenticalContent(refByte, BytesValidator.IdenticalContent.Mode.ONLY_OF);
@@ -77,7 +77,7 @@ public static BytesValidator onlyOf(byte refByte) {
7777
* Checks individual byte content
7878
*
7979
* @param refByte to check against
80-
* @return true if array has at least one byte that is not refByte
80+
* @return validator that returns true if array has at least one byte that is not refByte
8181
*/
8282
public static BytesValidator notOnlyOf(byte refByte) {
8383
return new BytesValidator.IdenticalContent(refByte, BytesValidator.IdenticalContent.Mode.NOT_ONLY_OF);
@@ -87,7 +87,7 @@ public static BytesValidator notOnlyOf(byte refByte) {
8787
* Checks if the internal byte array starts with given bytes
8888
*
8989
* @param startsWithBytes the supposed prefix
90-
* @return true all startsWithBytes match the first bytes in the internal array
90+
* @return validator that returns true all startsWithBytes match the first bytes in the internal array
9191
*/
9292
public static BytesValidator startsWith(byte... startsWithBytes) {
9393
return new BytesValidator.PrePostFix(true, startsWithBytes);
@@ -97,7 +97,7 @@ public static BytesValidator startsWith(byte... startsWithBytes) {
9797
* Checks if the internal byte array ends with given bytes
9898
*
9999
* @param endsWithBytes the supposed postfix
100-
* @return true all startsWithBytes match the first bytes in the internal array
100+
* @return validator that returns true all startsWithBytes match the first bytes in the internal array
101101
*/
102102
public static BytesValidator endsWith(byte... endsWithBytes) {
103103
return new BytesValidator.PrePostFix(false, endsWithBytes);
@@ -107,7 +107,7 @@ public static BytesValidator endsWith(byte... endsWithBytes) {
107107
* Checks individual byte content
108108
*
109109
* @param refByte to check against
110-
* @return true if array has no value refByte
110+
* @return validator that returns true if array has no value refByte
111111
*/
112112
public static BytesValidator noneOf(byte refByte) {
113113
return new BytesValidator.IdenticalContent(refByte, BytesValidator.IdenticalContent.Mode.NONE_OF);
@@ -117,7 +117,7 @@ public static BytesValidator noneOf(byte refByte) {
117117
* This will execute all passed validators and returns true if at least one returns true (i.e. OR concatenation)
118118
*
119119
* @param validators at least one validator must be passed
120-
* @return true if at least one validator returns true
120+
* @return validator that returns true if at least one validator returns true
121121
*/
122122
public static BytesValidator or(BytesValidator... validators) {
123123
return new BytesValidator.Logical(Arrays.asList(validators), BytesValidator.Logical.Operator.OR);
@@ -127,7 +127,7 @@ public static BytesValidator or(BytesValidator... validators) {
127127
* This will execute all passed validators and returns true if all return true (i.e. AND concatenation)
128128
*
129129
* @param validators at least one validator must be passed
130-
* @return true if all return true
130+
* @return validator that returns true if all return true
131131
*/
132132
public static BytesValidator and(BytesValidator... validators) {
133133
return new BytesValidator.Logical(Arrays.asList(validators), BytesValidator.Logical.Operator.AND);

src/main/java/at/favre/lib/bytes/MutableBytes.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public boolean isMutable() {
6969
*
7070
* @param newArray used to overwrite internal
7171
* @return this instance
72-
* @throws IndexOutOfBoundsException if newArray.length &gt; internal length
72+
* @throws IndexOutOfBoundsException if newArray.length() &gt; internal length
7373
*/
7474
public MutableBytes overwrite(byte[] newArray) {
7575
return overwrite(newArray, 0);
@@ -80,7 +80,7 @@ public MutableBytes overwrite(byte[] newArray) {
8080
*
8181
* @param newBytes used to overwrite internal
8282
* @return this instance
83-
* @throws IndexOutOfBoundsException if newArray.length &gt; internal length
83+
* @throws IndexOutOfBoundsException if newArray.length() &gt; internal length
8484
*/
8585
public MutableBytes overwrite(Bytes newBytes) {
8686
return overwrite(newBytes, 0);
@@ -92,7 +92,7 @@ public MutableBytes overwrite(Bytes newBytes) {
9292
* @param newArray used to overwrite internal
9393
* @param offsetInternalArray index of the internal array to start overwriting
9494
* @return this instance
95-
* @throws IndexOutOfBoundsException if newArray.length + offsetInternalArray &gt; internal length
95+
* @throws IndexOutOfBoundsException if newArray.length() + offsetInternalArray &gt; internal length
9696
*/
9797
public MutableBytes overwrite(byte[] newArray, int offsetInternalArray) {
9898
Objects.requireNonNull(newArray, "must provide non-null array as source");
@@ -106,7 +106,7 @@ public MutableBytes overwrite(byte[] newArray, int offsetInternalArray) {
106106
* @param newBytes used to overwrite internal
107107
* @param offsetInternalArray index of the internal array to start overwriting
108108
* @return this instance
109-
* @throws IndexOutOfBoundsException if newBytes.length + offsetInternalArray &gt; internal length
109+
* @throws IndexOutOfBoundsException if newBytes.length() + offsetInternalArray &gt; internal length
110110
*/
111111
public MutableBytes overwrite(Bytes newBytes, int offsetInternalArray) {
112112
return overwrite(Objects.requireNonNull(newBytes, "must provide non-null array as source").array(), offsetInternalArray);

0 commit comments

Comments
 (0)