forked from sporst/JHexView
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArrayHelpers.java
More file actions
97 lines (83 loc) · 2.56 KB
/
ArrayHelpers.java
File metadata and controls
97 lines (83 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package tv.porst.splib.arrays;
/**
* Contains helper functions for working with arrays.
*/
public final class ArrayHelpers
{
/**
* Returns a sub array of a given array.
*
* @param source
* The source array from which the sub array is extracted.
* @param offset
* The start offset of the sub array.
* @param length
* The length of the sub array in bytes.
*
* @return The sub array.
*/
public static byte[] getSubArray(final byte[] source, final int offset, final int length)
{
if (source == null) {
throw new IllegalArgumentException("Source argument must not be null.");
}
if (offset < 0) {
throw new IllegalArgumentException("Start offset must not be negative.");
}
if (offset >= source.length) {
throw new IllegalArgumentException("Start offset is too big.");
}
if (length < 0) {
throw new IllegalArgumentException("Length must not be negative.");
}
if (offset + length > source.length) {
throw new IllegalArgumentException("Range is too big.");
}
final byte[] subArray = new byte[length];
System.arraycopy(source, offset, subArray, 0, length);
return subArray;
}
/**
* Removes data from a byte array.
*
* @param data
* The input byte array.
* @param startOffset
* The start offset of the data to remove.
* @param length
* Length in bytes of the data to remove.
*
* @return The new array without the removed data.
*/
public static byte[] removeData(final byte[] data, final int startOffset, final int length)
{
final byte[] newData = new byte[data.length - length];
System.arraycopy(data, 0, newData, 0, startOffset);
System.arraycopy(data, startOffset + length, newData, startOffset, data.length - startOffset
- length);
return newData;
}
/**
* Replaces data in a byte array.
*
* @param data
* The input byte array.
* @param startOffset
* The start offset of the data to change.
* @param length
* The length in bytes of the data to change.
* @param replacementValue
* The new value for each byte in the replacement range.
*
* @return The new array with the replaced data.
*/
public static byte[] replaceData(final byte[] data, final int startOffset, final int length,
final byte replacementValue)
{
final byte[] newData = data.clone();
for (int i = startOffset; i < startOffset + length; i++) {
newData[i] = replacementValue;
}
return newData;
}
}