Skip to content

Commit 84382ae

Browse files
committed
Improve javadoc in Util.Bytes with time & space complexity analysis
1 parent 9d29f7b commit 84382ae

File tree

1 file changed

+124
-4
lines changed

1 file changed

+124
-4
lines changed

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

Lines changed: 124 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ private Byte() {
4545
* For example, {@code append(new byte[] {a, b}, new byte[] {}, new
4646
* byte[] {c}} returns the byteArray {@code {a, b, c}}.
4747
*
48+
* <p>
49+
* <strong>Analysis</strong>
50+
* <ul>
51+
* <li>Time Complexity: <code>O(n)</code></li>
52+
* <li>Space Complexity: <code>O(n)</code></li>
53+
* <li>Alters Parameters: <code>false</code></li>
54+
* </ul>
55+
* </p>
56+
*
4857
* @param arrays zero or more {@code byte} arrays
4958
* @return a single byteArray containing all the values from the source arrays, in
5059
* order
@@ -67,6 +76,15 @@ static byte[] concat(byte[]... arrays) {
6776
/**
6877
* Combines a single argument with a vararg to a single array
6978
*
79+
* <p>
80+
* <strong>Analysis</strong>
81+
* <ul>
82+
* <li>Time Complexity: <code>O(n)</code></li>
83+
* <li>Space Complexity: <code>O(n)</code></li>
84+
* <li>Alters Parameters: <code>false</code></li>
85+
* </ul>
86+
* </p>
87+
*
7088
* @param firstByte first arg
7189
* @param moreBytes varargs
7290
* @return array containing all args
@@ -87,6 +105,15 @@ static byte[] concatVararg(byte firstByte, byte[] moreBytes) {
87105
* java.util.Arrays.copyOfRange(array, i, i + target.length)} contains exactly
88106
* the same elements as {@code target}.
89107
*
108+
* <p>
109+
* <strong>Analysis</strong>
110+
* <ul>
111+
* <li>Time Complexity: <code>O(n*m)</code></li>
112+
* <li>Space Complexity: <code>O(1)</code></li>
113+
* <li>Alters Parameters: <code>false</code></li>
114+
* </ul>
115+
* </p>
116+
*
90117
* @param array the array to search for the sequence {@code target}
91118
* @param target the array to search for as a sub-sequence of {@code array}
92119
*/
@@ -113,6 +140,15 @@ static int indexOf(byte[] array, byte[] target, int start, int end) {
113140
* Returns the index of the last appearance of the value {@code target} in
114141
* {@code array}.
115142
*
143+
* <p>
144+
* <strong>Analysis</strong>
145+
* <ul>
146+
* <li>Time Complexity: <code>O(n)</code></li>
147+
* <li>Space Complexity: <code>O(1)</code></li>
148+
* <li>Alters Parameters: <code>false</code></li>
149+
* </ul>
150+
* </p>
151+
*
116152
* @param array an array of {@code byte} values, possibly empty
117153
* @param target a primitive {@code byte} value
118154
* @return the greatest index {@code i} for which {@code array[i] == target},
@@ -130,6 +166,15 @@ static int lastIndexOf(byte[] array, byte target, int start, int end) {
130166
/**
131167
* Counts the occurrence of target in the the in the subject array
132168
*
169+
* <p>
170+
* <strong>Analysis</strong>
171+
* <ul>
172+
* <li>Time Complexity: <code>O(n)</code></li>
173+
* <li>Space Complexity: <code>O(1)</code></li>
174+
* <li>Alters Parameters: <code>false</code></li>
175+
* </ul>
176+
* </p>
177+
*
133178
* @param array to count in
134179
* @param target to count
135180
* @return number of times target is in subject
@@ -147,6 +192,15 @@ static int countByte(byte[] array, byte target) {
147192
/**
148193
* Counts the times given pattern (ie. an array) can be found in given array
149194
*
195+
* <p>
196+
* <strong>Analysis</strong>
197+
* <ul>
198+
* <li>Time Complexity: <code>O(n*m)</code></li>
199+
* <li>Space Complexity: <code>O(1)</code></li>
200+
* <li>Alters Parameters: <code>false</code></li>
201+
* </ul>
202+
* </p>
203+
*
150204
* @param array to count in
151205
* @param pattern to match in array
152206
* @return number of times pattern is in subject
@@ -175,6 +229,15 @@ static int countByteArray(byte[] array, byte[] pattern) {
175229
* <p>
176230
* See: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
177231
*
232+
* <p>
233+
* <strong>Analysis</strong>
234+
* <ul>
235+
* <li>Time Complexity: <code>O(n)</code></li>
236+
* <li>Space Complexity: <code>O(1)</code></li>
237+
* <li>Alters Parameters: <code>true</code></li>
238+
* </ul>
239+
* </p>
240+
*
178241
* @param array to shuffle
179242
* @param random used to derive entropy - use {@link java.security.SecureRandom} instance if you want this to be secure
180243
*/
@@ -193,6 +256,15 @@ static void shuffle(byte[] array, Random random) {
193256
* Collections.reverse(Bytes.asList(array).subList(fromIndex, toIndex))}, but is likely to be more
194257
* efficient.
195258
*
259+
* <p>
260+
* <strong>Analysis</strong>
261+
* <ul>
262+
* <li>Time Complexity: <code>O(n)</code></li>
263+
* <li>Space Complexity: <code>O(1)</code></li>
264+
* <li>Alters Parameters: <code>true</code></li>
265+
* </ul>
266+
* </p>
267+
*
196268
* @throws IndexOutOfBoundsException if {@code fromIndex < 0}, {@code toIndex > array.length}, or
197269
* {@code toIndex > fromIndex}
198270
*/
@@ -208,6 +280,19 @@ static void reverse(byte[] array, int fromIndex, int toIndex) {
208280
/**
209281
* Light shift of whole byte array by shiftBitCount bits.
210282
* This method will alter the input byte array.
283+
*
284+
* <p>
285+
* <strong>Analysis</strong>
286+
* <ul>
287+
* <li>Time Complexity: <code>O(n)</code></li>
288+
* <li>Space Complexity: <code>O(1)</code></li>
289+
* <li>Alters Parameters: <code>true</code></li>
290+
* </ul>
291+
* </p>
292+
*
293+
* @param byteArray to shift
294+
* @param shiftBitCount how many bits to shift
295+
* @return shifted byte array
211296
*/
212297
static byte[] shiftLeft(byte[] byteArray, int shiftBitCount) {
213298
final int shiftMod = shiftBitCount % 8;
@@ -234,6 +319,19 @@ static byte[] shiftLeft(byte[] byteArray, int shiftBitCount) {
234319
/**
235320
* Unsigned/logical right shift of whole byte array by shiftBitCount bits.
236321
* This method will alter the input byte array.
322+
*
323+
* <p>
324+
* <strong>Analysis</strong>
325+
* <ul>
326+
* <li>Time Complexity: <code>O(n)</code></li>
327+
* <li>Space Complexity: <code>O(1)</code></li>
328+
* <li>Alters Parameters: <code>true</code></li>
329+
* </ul>
330+
* </p>
331+
*
332+
* @param byteArray to shift
333+
* @param shiftBitCount how many bits to shift
334+
* @return shifted byte array
237335
*/
238336
static byte[] shiftRight(byte[] byteArray, int shiftBitCount) {
239337
final int shiftMod = shiftBitCount % 8;
@@ -259,13 +357,26 @@ static byte[] shiftRight(byte[] byteArray, int shiftBitCount) {
259357

260358
/**
261359
* See https://codahale.com/a-lesson-in-timing-attacks/
360+
*
361+
* <p>
362+
* <strong>Analysis</strong>
363+
* <ul>
364+
* <li>Time Complexity: <code>O(n)</code></li>
365+
* <li>Space Complexity: <code>O(1)</code></li>
366+
* <li>Alters Parameters: <code>false</code></li>
367+
* </ul>
368+
* </p>
369+
*
370+
* @param array to check for equals
371+
* @param anotherArray to check against array
372+
* @return if both arrays have the same length and same length for every index
262373
*/
263-
static boolean constantTimeEquals(byte[] obj, byte[] anotherArray) {
264-
if (anotherArray == null || obj.length != anotherArray.length) return false;
374+
static boolean constantTimeEquals(byte[] array, byte[] anotherArray) {
375+
if (anotherArray == null || array.length != anotherArray.length) return false;
265376

266377
int result = 0;
267-
for (int i = 0; i < obj.length; i++) {
268-
result |= obj[i] ^ anotherArray[i];
378+
for (int i = 0; i < array.length; i++) {
379+
result |= array[i] ^ anotherArray[i];
269380
}
270381
return result == 0;
271382
}
@@ -276,6 +387,15 @@ static boolean constantTimeEquals(byte[] obj, byte[] anotherArray) {
276387
* This implementation will not create a copy of the internal array and will only internally initialize
277388
* a int array with 256 elements as temporary buffer.
278389
*
390+
* <p>
391+
* <strong>Analysis</strong>
392+
* <ul>
393+
* <li>Time Complexity: <code>O(n)</code></li>
394+
* <li>Space Complexity: <code>O(1)</code></li>
395+
* <li>Alters Parameters: <code>false</code></li>
396+
* </ul>
397+
* </p>
398+
*
279399
* @param array to calculate the entropy from
280400
* @return entropy factor, higher means higher entropy
281401
*/

0 commit comments

Comments
 (0)