Skip to content
This repository was archived by the owner on Feb 25, 2024. It is now read-only.

Commit d40e304

Browse files
committed
The byte fields of PacketHeads are now range-limited integers
1 parent 51922d6 commit d40e304

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/main/java/de/kaleidox/vban/Util.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,18 @@ public static byte[] createByteArray(Object data) {
7979
else if (data instanceof ByteArray) return ((ByteArray) data).getBytes();
8080
else throw new IllegalArgumentException("Unknown Data Type! Please contact the developer.");
8181
}
82+
83+
/**
84+
* Checks whether the given integer is within the given boundaries,
85+
* and if not, throws an {@link IllegalArgumentException}.
86+
*
87+
* @param check The int to check range of.
88+
* @param from The minimum value.
89+
* @param to The maximum value.
90+
* @throws IllegalArgumentException If the integer is out of bounds.
91+
*/
92+
public static void checkRange(int check, int from, int to) throws IllegalArgumentException {
93+
if (check < from || check > to)
94+
throw new IllegalArgumentException(String.format("Integer out of range. [%d;%d;%d]", from, check, to));
95+
}
8296
}

src/main/java/de/kaleidox/vban/packet/VBANPacketHead.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.intellij.lang.annotations.MagicConstant;
1111

1212
import static de.kaleidox.vban.Util.appendByteArray;
13+
import static de.kaleidox.vban.Util.checkRange;
1314
import static de.kaleidox.vban.Util.intToByteArray;
1415
import static de.kaleidox.vban.Util.stringToBytesASCII;
1516
import static de.kaleidox.vban.Util.trimArray;
@@ -21,17 +22,19 @@ public class VBANPacketHead implements ByteArray {
2122

2223
private VBANPacketHead(@MagicConstant(flagsFromClass = Protocol.class) int protocol,
2324
@MagicConstant(flagsFromClass = SampleRate.class) int sampleRateIndex,
24-
byte samples,
25-
byte channel,
25+
int samples,
26+
int channel,
2627
@MagicConstant(flagsFromClass = Format.class) int format,
2728
@MagicConstant(flagsFromClass = Codec.class) int codec,
2829
String streamName,
2930
int frameCounter) {
3031
byte[] bytes = new byte[0];
32+
checkRange(samples, 0, 255);
33+
checkRange(channel, 0, 255);
3134

3235
bytes = appendByteArray(bytes, "VBAN".getBytes());
3336
bytes = appendByteArray(bytes, (byte) (protocol | sampleRateIndex));
34-
bytes = appendByteArray(bytes, samples, channel);
37+
bytes = appendByteArray(bytes, (byte) samples, (byte) channel);
3538
bytes = appendByteArray(bytes, (byte) (format | codec));
3639
bytes = appendByteArray(bytes, trimArray(stringToBytesASCII(streamName), 16));
3740
bytes = appendByteArray(bytes, intToByteArray(frameCounter, 4));
@@ -114,16 +117,16 @@ public static class Factory implements de.kaleidox.util.model.Factory<VBANPacket
114117
private final int protocol;
115118
@MagicConstant(valuesFromClass = SampleRate.class)
116119
private final int sampleRate;
117-
private final byte samples;
118-
private final byte channel;
120+
private final int samples;
121+
private final int channel;
119122
@MagicConstant(valuesFromClass = Format.class)
120123
private final int format;
121124
@MagicConstant(valuesFromClass = Codec.class)
122125
private final int codec;
123126
private final String streamName;
124127
private int counter = 1;
125128

126-
private Factory(int protocol, int sampleRate, byte samples, byte channel, int format, int codec, String streamName) {
129+
private Factory(int protocol, int sampleRate, int samples, int channel, int format, int codec, String streamName) {
127130
this.protocol = protocol;
128131
this.sampleRate = sampleRate;
129132
this.samples = samples;
@@ -152,8 +155,8 @@ public static class Builder implements de.kaleidox.util.model.Builder<Factory> {
152155
private int protocol = -1;
153156
@MagicConstant(valuesFromClass = SampleRate.class)
154157
private int sampleRate = SampleRate.hz48000;
155-
private byte samples = -1;
156-
private byte channel = 2;
158+
private int samples = 255;
159+
private int channel = 2;
157160
@MagicConstant(valuesFromClass = Format.class)
158161
private int format = Format.INT16;
159162
@MagicConstant(valuesFromClass = Codec.class)
@@ -181,7 +184,7 @@ public Builder setSampleRate(int sampleRate) {
181184
return this;
182185
}
183186

184-
public byte getSamples() {
187+
public int getSamples() {
185188
return samples;
186189
}
187190

@@ -190,7 +193,7 @@ public Builder setSamples(byte samples) {
190193
return this;
191194
}
192195

193-
public byte getChannel() {
196+
public int getChannel() {
194197
return channel;
195198
}
196199

0 commit comments

Comments
 (0)