-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockNumber.java
More file actions
143 lines (131 loc) · 3.38 KB
/
Copy pathBlockNumber.java
File metadata and controls
143 lines (131 loc) · 3.38 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import java.util.Arrays;
/**
* Represents a 16-bit block number used in TFTP transmission.
*
* @author James Li
*/
class BlockNumber {
byte[] value = { 0x00, 0x00 };
/**
* Returns the byte array representation of the block number.
*
* @return Byte array representation
*/
byte[] val() {
return this.value;
}
/**
* Returns the `int` representation of the block number.
*
* @return `int` representation
*/
int intVal() {
return Utils.shortToInt(Utils.byteArrayToShort(this.val()));
}
/**
* Increments block number by one.
*
* @return This object
*/
BlockNumber incr() {
this.value = addOne(this.value);
return this;
}
/**
* Returns byte array representation of a block number incremented by one.
* <p>
* This wraps at maximum unsigned 16-bit value.
*
* @param value
* Byte array representation of block number
* @return Incremented byte array
*/
private static byte[] addOne(byte[] value) {
int shortVal = Utils.shortToInt(Utils.byteArrayToShort(value));
return Utils.shortToByteArray(Utils.intToShort(++shortVal));
}
/**
* Evaluates whether two block numbers are equal.
*
* @param a
* Byte array representation of block number
* @param b
* Byte array representation of block number
* @return `true` if equal, `false` otherwise
*/
static boolean equals(byte[] a, byte[] b) {
return Arrays.equals(a, b);
}
/**
* Evaluates whether two block numbers are equal.
*
* @param a
* `BlockNumber` instance
* @param b
* Byte array representation of block number
* @return `true` if equal, `false` otherwise
*/
static boolean equals(BlockNumber a, byte[] b) {
return equals(a.val(), b);
}
/**
* Evaluates whether two block numbers are equal.
*
* @param a
* `BlockNumber` instance
* @param b
* `BlockNumber` instance
* @return `true` if equal, `false` otherwise
*/
static boolean equals(BlockNumber a, BlockNumber b) {
return equals(a.val(), b.val());
}
/**
* Checks if the block numbers are in sequence (`a` then `b`).
*
* @param a
* Byte representation of block number
* @param b
* Byte representation of block number
* @return `true` if in sequence, `false` otherwise
*/
static boolean isInSeq(byte[] a, byte[] b) {
return Arrays.equals(addOne(a), b);
}
/**
* Checks if the block numbers are in sequence (`a` then `b`).
*
* @param a
* `BlockNumber` instance
* @param b
* Byte representation of block number
* @return `true` if in sequence, `false` otherwise
*/
static boolean isInSeq(BlockNumber a, byte[] b) {
return isInSeq(a.val(), b);
}
/**
* Checks if the block numbers are in sequence (`a` then `b`).
*
* @param a
* Byte representation of block number
* @param b
* `BlockNumber` instance
* @return `true` if in sequence, `false` otherwise
*/
static boolean isInSeq(byte[] a, BlockNumber b) throws Exception {
return isInSeq(a, b.val());
}
/**
* Checks if the block numbers are in sequence (`a` then `b`).
*
* @param a
* `BlockNumber` instance
* @param b
* `BlockNumber` instance
* @return `true` if in sequence, `false` otherwise
*/
static boolean isInSeq(BlockNumber a, BlockNumber b) throws Exception {
return Arrays.equals(addOne(a.val()), b.val());
}
}