Skip to content

Commit cb0efd0

Browse files
committed
Fix various issues mostly javadoc and typos and some simple code fixes
1 parent b1df8ca commit cb0efd0

File tree

7 files changed

+82
-80
lines changed

7 files changed

+82
-80
lines changed

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
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import java.util.*;
3232

3333
/**
34-
* Bytes is wrapper class for an byte-array that allows a lot of convenience operations on it:
34+
* "Bytes" is wrapper class for a byte-array that allows a lot of convenience operations on it:
3535
* <ul>
3636
* <li>Creation from various source: arrays, primitives, parsed or random</li>
3737
* <li>Encoding in many formats: hex, base64, etc.</li>
@@ -45,7 +45,7 @@
4545
* It supports byte ordering (little/big endianness).
4646
* <p>
4747
* This class is immutable as long as the internal array is not changed from outside (which can't be assured, when
48-
* using using <code>wrap()</code>). It is possible to create a mutable version (see {@link MutableBytes}).
48+
* using <code>wrap()</code>). It is possible to create a mutable version (see {@link MutableBytes}).
4949
* <p>
5050
* <strong>Example:</strong>
5151
* <pre>
@@ -92,7 +92,7 @@ public static Bytes allocate(int length, byte defaultValue) {
9292
}
9393

9494
/**
95-
* Creates an Byte instance with an internal empty byte array. Same as calling {@link #allocate(int)} with 0.
95+
* Creates a Byte instance with an internal empty byte array. Same as calling {@link #allocate(int)} with 0.
9696
*
9797
* @return the empty instance (always the same reference
9898
*/
@@ -451,7 +451,7 @@ public static Bytes from(InputStream stream) {
451451

452452
/**
453453
* Reads given input stream up to maxLength and creates a new instance from read data.
454-
* Read maxLength is never longer than stream size (ie. maxLength is only limiting, not assuring maxLength)
454+
* Read maxLength is never longer than stream size (i.e. maxLength is only limiting, not assuring maxLength)
455455
*
456456
* @param stream to read from
457457
* @param maxLength read to this maxLength or end of stream
@@ -568,7 +568,7 @@ public static Bytes from(char[] charArray, Charset charset, int offset, int leng
568568

569569
/**
570570
* Convert UUID to a newly generated 16 byte long array representation. Puts the 8 byte most significant bits and
571-
* 8 byte least significant bits into an byte array.
571+
* 8 byte the least significant bits into a byte array.
572572
*
573573
* @param uuid to convert to array
574574
* @return new instance
@@ -611,7 +611,7 @@ public static Bytes parseDec(CharSequence decString) {
611611
* Encodes with given radix string representation (e.g. radix 16 would be hex).
612612
* See also {@link BigInteger#toString(int)}.
613613
* <p>
614-
* This is usually a number encoding, not a data encoding (ie. leading zeros are not preserved), but this implementation
614+
* This is usually a number encoding, not a data encoding (i.e. leading zeros are not preserved), but this implementation
615615
* tries to preserve the leading zeros, to keep the in/output byte length size the same, but use at your own risk!
616616
*
617617
* @param radixNumberString the encoded string
@@ -628,7 +628,7 @@ public static Bytes parseRadix(CharSequence radixNumberString, int radix) {
628628
* <ul>
629629
* <li>Upper- and lowercase <code>a-f</code> (also mixed case)</li>
630630
* <li>Prefix with <code>0x</code> which will be ignored</li>
631-
* <li>Even and odd number of string length with auto zero padding (ie. 'E3F' is same as '0E3F')</li>
631+
* <li>Even and odd number of string length with auto zero padding (i.e. 'E3F' is same as '0E3F')</li>
632632
* </ul>
633633
*
634634
* @param hexString the encoded string
@@ -654,7 +654,7 @@ public static Bytes parseBase32(CharSequence base32Rfc4648String) {
654654
/**
655655
* Parsing of base36 encoded byte arrays.
656656
* <p>
657-
* This is usually a number encoding, not a data encoding (ie. leading zeros are not preserved), but this implementation
657+
* This is usually a number encoding, not a data encoding (i.e. leading zeros are not preserved), but this implementation
658658
* tries to preserve the leading zeros, to keep the in/output byte length size the same.
659659
*
660660
* @param base36String the encoded string
@@ -766,7 +766,7 @@ public static Bytes random(int length, Random random) {
766766
/* TRANSFORMER **********************************************************************************************/
767767

768768
/**
769-
* Creates a new instance with the current array appended to the provided data (ie. append at the end).
769+
* Creates a new instance with the current array appended to the provided data (i.e. append at the end).
770770
* <p>
771771
* This will create a new byte array internally, so it is not suitable to use as extensive builder pattern -
772772
* use {@link ByteBuffer} or {@link java.io.ByteArrayOutputStream} for that.
@@ -779,7 +779,7 @@ public Bytes append(Bytes bytes) {
779779
}
780780

781781
/**
782-
* Creates a new instance with the current array appended to the provided data (ie. append at the end)
782+
* Creates a new instance with the current array appended to the provided data (i.e. append at the end)
783783
*
784784
* @param singleByte to append
785785
* @return appended instance
@@ -789,7 +789,7 @@ public Bytes append(byte singleByte) {
789789
}
790790

791791
/**
792-
* Creates a new instance with the current array appended to the provided data (ie. append at the end)
792+
* Creates a new instance with the current array appended to the provided data (i.e. append at the end)
793793
*
794794
* @param char2Bytes to append
795795
* @return appended instance
@@ -799,7 +799,7 @@ public Bytes append(char char2Bytes) {
799799
}
800800

801801
/**
802-
* Creates a new instance with the current array appended to the provided data (ie. append at the end)
802+
* Creates a new instance with the current array appended to the provided data (i.e. append at the end)
803803
*
804804
* @param short2Bytes to append
805805
* @return appended instance
@@ -809,7 +809,7 @@ public Bytes append(short short2Bytes) {
809809
}
810810

811811
/**
812-
* Creates a new instance with the current array appended to the provided data (ie. append at the end)
812+
* Creates a new instance with the current array appended to the provided data (i.e. append at the end)
813813
*
814814
* @param integer4Bytes to append
815815
* @return appended instance
@@ -819,7 +819,7 @@ public Bytes append(int integer4Bytes) {
819819
}
820820

821821
/**
822-
* Creates a new instance with the current array appended to the provided data (ie. append at the end)
822+
* Creates a new instance with the current array appended to the provided data (i.e. append at the end)
823823
*
824824
* @param long8Bytes to append
825825
* @return appended instance
@@ -829,7 +829,7 @@ public Bytes append(long long8Bytes) {
829829
}
830830

831831
/**
832-
* Creates a new instance with the current array appended to the provided data (ie. append at the end).
832+
* Creates a new instance with the current array appended to the provided data (i.e. append at the end).
833833
* You may use this to append multiple byte arrays without the need for chaining the {@link #append(byte[])} call
834834
* and therefore generating intermediate copies of the byte array, making this approach use less memory.
835835
*
@@ -841,7 +841,7 @@ public Bytes append(byte[]... arrays) {
841841
}
842842

843843
/**
844-
* Creates a new instance with the current array appended to the provided data (ie. append at the end)
844+
* Creates a new instance with the current array appended to the provided data (i.e. append at the end)
845845
*
846846
* @param secondArray to append
847847
* @return appended instance
@@ -851,7 +851,7 @@ public Bytes append(byte[] secondArray) {
851851
}
852852

853853
/**
854-
* Creates a new instance with the current array appended to the provided data (ie. append at the end)
854+
* Creates a new instance with the current array appended to the provided data (i.e. append at the end)
855855
* <p>
856856
* If given array is null, the nothing will be appended.
857857
*
@@ -1004,7 +1004,7 @@ public Bytes rightShift(int shiftCount) {
10041004
}
10051005

10061006
/**
1007-
* Returns a Byte whose value is equivalent to this Byte with the designated bit set to newBitValue. Bits start to count from the LSB (ie. Bytes.from(0).switchBit(0,true) == 1)
1007+
* Returns a Byte whose value is equivalent to this Byte with the designated bit set to newBitValue. Bits start to count from the LSB (i.e. Bytes.from(0).switchBit(0,true) == 1)
10081008
*
10091009
* @param bitPosition not to confuse with byte position
10101010
* @param newBitValue if true set to 1, 0 otherwise
@@ -1063,7 +1063,7 @@ public Bytes reverse() {
10631063
* copy but not the original, the copy will contain {@code (byte)0}.
10641064
* <p>
10651065
* Resize from LSB or length, so an array [0,1,2,3] resized to 3 will result in [1,2,3] or resized to 5 [0,0,1,2,3].
1066-
* So when a 8 byte value resized to 4 byte will result in the same 32 bit integer value
1066+
* So when an 8 byte value resized to 4 byte will result in the same 32-bit integer value
10671067
*
10681068
* @param newByteLength the length of the copy to be returned
10691069
* @return a copy with the desired size or "this" instance if newByteLength == current length
@@ -1099,7 +1099,7 @@ public Bytes resize(int newByteLength, BytesTransformer.ResizeTransformer.Mode m
10991099
* Calculates md5 on the underlying byte array and returns a byte instance containing the hash.
11001100
* This hash algorithm SHOULD be supported by every JVM implementation (see
11011101
* <a href="https://docs.oracle.com/javase/7/docs/api/java/security/MessageDigest.html">Javadoc for MessageDigest</a>)
1102-
*
1102+
* <p>
11031103
* <strong>Do not use this algorithm in security relevant applications.</strong>
11041104
*
11051105
* @return md5 (16 bytes) hash of internal byte array
@@ -1114,7 +1114,7 @@ public Bytes hashMd5() {
11141114
* Calculates sha1 on the underlying byte array and returns a byte instance containing the hash.
11151115
* This hash algorithm SHOULD be supported by every JVM implementation (see
11161116
* <a href="https://docs.oracle.com/javase/7/docs/api/java/security/MessageDigest.html">Javadoc for MessageDigest</a>)
1117-
*
1117+
* <p>
11181118
* <strong>Do not use this algorithm in security relevant applications.</strong>
11191119
*
11201120
* @return sha1 (20 bytes) hash of internal byte array
@@ -1151,9 +1151,9 @@ public Bytes hash(String algorithm) {
11511151
/**
11521152
* Generic transformation of this instance.
11531153
* <p>
1154-
* This transformation might be done in-place (ie. without copying the internal array and overwriting its old state),
1154+
* This transformation might be done in-place (i.e. without copying the internal array and overwriting its old state),
11551155
* or on a copy of the internal data, depending on the type (e.g. {@link MutableBytes}) and if the operation can be done
1156-
* in-place. Therefore the caller has to ensure that certain side-effects, which occur due to the changing of the internal
1156+
* in-place. Therefore, the caller has to ensure that certain side effects, which occur due to the changing of the internal
11571157
* data, do not create bugs in his/her code. Usually immutability is preferred, but when handling many or big byte arrays,
11581158
* mutability enables drastically better performance.
11591159
*
@@ -1199,7 +1199,7 @@ public int length() {
11991199
/**
12001200
* The bit length of the underlying byte array.
12011201
*
1202-
* @return bit length
1202+
* @return the bit length
12031203
*/
12041204
public int lengthBit() {
12051205
return length() * 8;
@@ -1227,7 +1227,7 @@ public ByteOrder byteOrder() {
12271227
/**
12281228
* Checks if instance is mutable
12291229
*
1230-
* @return true if mutable, ie. transformers will change internal array
1230+
* @return true if mutable, i.e. transformers will change internal array
12311231
*/
12321232
public boolean isMutable() {
12331233
return false;
@@ -1384,7 +1384,7 @@ public boolean endsWith(byte[] subArray) {
13841384
* <code>1000 0000</code> has <code>bitAt(0) == false</code> and <code>bitAt(7) == true</code>.
13851385
*
13861386
* @param bitIndex the index of the {@code bit} value.
1387-
* @return true if bit at given index is set, false otherwise
1387+
* @return true if the bit at given index is set, false otherwise
13881388
* @throws IndexOutOfBoundsException if the {@code bitIndex} argument is negative or not less than the length of this array in bits.
13891389
*/
13901390
public boolean bitAt(int bitIndex) {
@@ -1434,7 +1434,7 @@ public int unsignedByteAt(int index) {
14341434
*/
14351435
public char charAt(int index) {
14361436
Util.Validation.checkIndexBounds(length(), index, 2, "char");
1437-
return ((ByteBuffer) internalBuffer().position(index)).getChar();
1437+
return internalBuffer().position(index).getChar();
14381438
}
14391439

14401440
/**
@@ -1447,7 +1447,7 @@ public char charAt(int index) {
14471447
*/
14481448
public short shortAt(int index) {
14491449
Util.Validation.checkIndexBounds(length(), index, 2, "short");
1450-
return ((ByteBuffer) internalBuffer().position(index)).getShort();
1450+
return internalBuffer().position(index).getShort();
14511451
}
14521452

14531453
/**
@@ -1460,7 +1460,7 @@ public short shortAt(int index) {
14601460
*/
14611461
public int intAt(int index) {
14621462
Util.Validation.checkIndexBounds(length(), index, 4, "int");
1463-
return ((ByteBuffer) internalBuffer().position(index)).getInt();
1463+
return internalBuffer().position(index).getInt();
14641464
}
14651465

14661466
/**
@@ -1473,7 +1473,7 @@ public int intAt(int index) {
14731473
*/
14741474
public long longAt(int index) {
14751475
Util.Validation.checkIndexBounds(length(), index, 8, "long");
1476-
return ((ByteBuffer) internalBuffer().position(index)).getLong();
1476+
return internalBuffer().position(index).getLong();
14771477
}
14781478

14791479
/**
@@ -1606,7 +1606,7 @@ public InputStream inputStream() {
16061606
/**
16071607
* The reference of te internal byte-array. This call requires no conversation or additional memory allocation.
16081608
* <p>
1609-
* Modifications to this bytes's content will cause the returned
1609+
* Modifications to these byte's content will cause the returned
16101610
* array's content to be modified, and vice versa.
16111611
*
16121612
* @return the direct reference of the internal byte array
@@ -1662,12 +1662,12 @@ public String encodeDec() {
16621662
/**
16631663
* Encodes the internal array in given radix representation (e.g. 2 = binary, 10 = decimal, 16 = hex).
16641664
* <p>
1665-
* This is usually a number encoding, not a data encoding (ie. leading zeros are not preserved), but this implementation
1665+
* This is usually a number encoding, not a data encoding (i.e. leading zeros are not preserved), but this implementation
16661666
* tries to preserve the leading zeros, to keep the in/output byte length size the same. To preserve the length padding
16671667
* would be required, but is not supported in this implementation.
16681668
* <p>
16691669
* But still full disclaimer:
1670-
*
1670+
* <p>
16711671
* <strong>This is NOT recommended for data encoding, only for number encoding</strong>
16721672
* <p>
16731673
* See <a href="https://en.wikipedia.org/wiki/Radix_economy">Radix Economy</a> and {@link BigInteger#toString(int)}.
@@ -1868,7 +1868,7 @@ public BitSet toBitSet() {
18681868
* The internal byte array wrapped in a {@link BigInteger} instance.
18691869
* <p>
18701870
* If the internal byte order is {@link ByteOrder#LITTLE_ENDIAN}, a copy of the internal
1871-
* array will be reversed and used as backing array with the big integer. Otherwise the internal
1871+
* array will be reversed and used as backing array with the big integer. Otherwise, the internal
18721872
* array will be used directly.
18731873
*
18741874
* @return big integer
@@ -2005,7 +2005,7 @@ public long toLong() {
20052005
}
20062006

20072007
/**
2008-
* Converts the internal byte array to an long array, that is, every 8 bytes will be packed into a single long.
2008+
* Converts the internal byte array to a long array, that is, every 8 bytes will be packed into a single long.
20092009
* <p>
20102010
* E.g. 8 bytes will be packed to a length 1 long array:
20112011
* <pre>
@@ -2037,7 +2037,7 @@ public float toFloat() {
20372037
}
20382038

20392039
/**
2040-
* Converts the internal byte array to an float array, that is, every 4 bytes will be packed into a single float.
2040+
* Converts the internal byte array to a float array, that is, every 4 bytes will be packed into a single float.
20412041
* <p>
20422042
* E.g. 4 bytes will be packed to a length 1 float array:
20432043
* <pre>
@@ -2069,7 +2069,7 @@ public double toDouble() {
20692069
}
20702070

20712071
/**
2072-
* Converts the internal byte array to an double array, that is, every 8 bytes will be packed into a single double.
2072+
* Converts the internal byte array to a double array, that is, every 8 bytes will be packed into a single double.
20732073
* <p>
20742074
* E.g. 8 bytes will be packed to a length 1 double array:
20752075
* <pre>
@@ -2178,7 +2178,7 @@ public boolean equals(byte[] anotherArray) {
21782178
* will not break on the first mismatch. This method is useful to prevent some side-channel attacks,
21792179
* but is slower on average.
21802180
* <p>
2181-
* This implementation uses the algorithm suggested in https://codahale.com/a-lesson-in-timing-attacks/
2181+
* This implementation uses the algorithm suggested in <a href="https://codahale.com/a-lesson-in-timing-attacks/">a-lesson-in-timing-attacks</a>
21822182
*
21832183
* @param anotherArray to compare with
21842184
* @return true if {@link Arrays#equals(byte[], byte[])} returns true on given and internal array

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

0 commit comments

Comments
 (0)