mirror of
https://github.com/collinsmith/riiablo.git
synced 2025-07-09 23:38:15 +07:00
Increased robustness of UnsafeNarrowing exception
UnsafeNarrowing now requires a ByteInput argument to track where exception was thrown Changed visibility of BitConstraints to public Added BitConstraints safeXXu checkers for BitInput as well as ByteInput
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
package com.riiablo.io;
|
package com.riiablo.io;
|
||||||
|
|
||||||
class BitConstraints {
|
public class BitConstraints {
|
||||||
private BitConstraints() {}
|
private BitConstraints() {}
|
||||||
|
|
||||||
private static int _validateSize(int min, int max, int bits) {
|
private static int _validateSize(int min, int max, int bits) {
|
||||||
@ -60,33 +60,65 @@ class BitConstraints {
|
|||||||
return _validateSize(Byte.SIZE - 1, Byte.SIZE, bits);
|
return _validateSize(Byte.SIZE - 1, Byte.SIZE, bits);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte safe8u(short i) {
|
public static byte safe8u(ByteInput byteInput, short i) {
|
||||||
if (!BitUtils.isUnsigned(i, Byte.SIZE)) {
|
if (!BitUtils.isUnsigned(i, Byte.SIZE)) {
|
||||||
throw new UnsafeNarrowing(i);
|
throw new UnsafeNarrowing(byteInput, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (byte) i;
|
return (byte) i;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static short safe16u(int i) {
|
public static short safe16u(ByteInput byteInput, int i) {
|
||||||
if (!BitUtils.isUnsigned(i, Short.SIZE)) {
|
if (!BitUtils.isUnsigned(i, Short.SIZE)) {
|
||||||
throw new UnsafeNarrowing(i);
|
throw new UnsafeNarrowing(byteInput, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (short) i;
|
return (short) i;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int safe32u(long i) {
|
public static int safe32u(ByteInput byteInput, long i) {
|
||||||
if (!BitUtils.isUnsigned(i, Integer.SIZE)) {
|
if (!BitUtils.isUnsigned(i, Integer.SIZE)) {
|
||||||
throw new UnsafeNarrowing(i);
|
throw new UnsafeNarrowing(byteInput, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (int) i;
|
return (int) i;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static long safe64u(long i) {
|
public static long safe64u(ByteInput byteInput, long i) {
|
||||||
if (!BitUtils.isUnsigned(i, Long.SIZE)) {
|
if (!BitUtils.isUnsigned(i, Long.SIZE)) {
|
||||||
throw new UnsafeNarrowing(i);
|
throw new UnsafeNarrowing(byteInput, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (long) i;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte safe8u(BitInput bitInput, short i) {
|
||||||
|
if (!BitUtils.isUnsigned(i, Byte.SIZE)) {
|
||||||
|
throw new UnsafeNarrowing(bitInput.byteInput(), i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (byte) i;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static short safe16u(BitInput bitInput, int i) {
|
||||||
|
if (!BitUtils.isUnsigned(i, Short.SIZE)) {
|
||||||
|
throw new UnsafeNarrowing(bitInput.byteInput(), i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (short) i;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int safe32u(BitInput bitInput, long i) {
|
||||||
|
if (!BitUtils.isUnsigned(i, Integer.SIZE)) {
|
||||||
|
throw new UnsafeNarrowing(bitInput.byteInput(), i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (int) i;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long safe64u(BitInput bitInput, long i) {
|
||||||
|
if (!BitUtils.isUnsigned(i, Long.SIZE)) {
|
||||||
|
throw new UnsafeNarrowing(bitInput.byteInput(), i);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (long) i;
|
return (long) i;
|
||||||
|
@ -459,7 +459,7 @@ public class BitInput {
|
|||||||
public byte readSafe8u() {
|
public byte readSafe8u() {
|
||||||
try {
|
try {
|
||||||
final short value = read8u();
|
final short value = read8u();
|
||||||
return BitConstraints.safe8u(value);
|
return BitConstraints.safe8u(byteInput, value);
|
||||||
} catch (IndexOutOfBoundsException t) {
|
} catch (IndexOutOfBoundsException t) {
|
||||||
throw new EndOfInput(t);
|
throw new EndOfInput(t);
|
||||||
}
|
}
|
||||||
@ -475,7 +475,7 @@ public class BitInput {
|
|||||||
public short readSafe16u() {
|
public short readSafe16u() {
|
||||||
try {
|
try {
|
||||||
final int value = read16u();
|
final int value = read16u();
|
||||||
return BitConstraints.safe16u(value);
|
return BitConstraints.safe16u(byteInput, value);
|
||||||
} catch (IndexOutOfBoundsException t) {
|
} catch (IndexOutOfBoundsException t) {
|
||||||
throw new EndOfInput(t);
|
throw new EndOfInput(t);
|
||||||
}
|
}
|
||||||
@ -491,7 +491,7 @@ public class BitInput {
|
|||||||
public int readSafe32u() {
|
public int readSafe32u() {
|
||||||
try {
|
try {
|
||||||
final long value = read32u();
|
final long value = read32u();
|
||||||
return BitConstraints.safe32u(value);
|
return BitConstraints.safe32u(byteInput, value);
|
||||||
} catch (IndexOutOfBoundsException t) {
|
} catch (IndexOutOfBoundsException t) {
|
||||||
throw new EndOfInput(t);
|
throw new EndOfInput(t);
|
||||||
}
|
}
|
||||||
@ -507,7 +507,7 @@ public class BitInput {
|
|||||||
public long readSafe64u() {
|
public long readSafe64u() {
|
||||||
try {
|
try {
|
||||||
final long value = read64();
|
final long value = read64();
|
||||||
return BitConstraints.safe64u(value);
|
return BitConstraints.safe64u(byteInput, value);
|
||||||
} catch (IndexOutOfBoundsException t) {
|
} catch (IndexOutOfBoundsException t) {
|
||||||
throw new EndOfInput(t);
|
throw new EndOfInput(t);
|
||||||
}
|
}
|
||||||
|
@ -401,7 +401,7 @@ public class ByteInput {
|
|||||||
assert aligned() : "not aligned";
|
assert aligned() : "not aligned";
|
||||||
try {
|
try {
|
||||||
final short value = read8u(); // increments bits
|
final short value = read8u(); // increments bits
|
||||||
return BitConstraints.safe8u(value);
|
return BitConstraints.safe8u(this, value);
|
||||||
} catch (IndexOutOfBoundsException t) {
|
} catch (IndexOutOfBoundsException t) {
|
||||||
throw new EndOfInput(t);
|
throw new EndOfInput(t);
|
||||||
}
|
}
|
||||||
@ -418,7 +418,7 @@ public class ByteInput {
|
|||||||
assert aligned() : "not aligned";
|
assert aligned() : "not aligned";
|
||||||
try {
|
try {
|
||||||
final int value = read16u(); // increments bits
|
final int value = read16u(); // increments bits
|
||||||
return BitConstraints.safe16u(value);
|
return BitConstraints.safe16u(this, value);
|
||||||
} catch (IndexOutOfBoundsException t) {
|
} catch (IndexOutOfBoundsException t) {
|
||||||
throw new EndOfInput(t);
|
throw new EndOfInput(t);
|
||||||
}
|
}
|
||||||
@ -435,7 +435,7 @@ public class ByteInput {
|
|||||||
assert aligned() : "not aligned";
|
assert aligned() : "not aligned";
|
||||||
try {
|
try {
|
||||||
final long value = read32u(); // increments bits
|
final long value = read32u(); // increments bits
|
||||||
return BitConstraints.safe32u(value);
|
return BitConstraints.safe32u(this, value);
|
||||||
} catch (IndexOutOfBoundsException t) {
|
} catch (IndexOutOfBoundsException t) {
|
||||||
throw new EndOfInput(t);
|
throw new EndOfInput(t);
|
||||||
}
|
}
|
||||||
@ -452,7 +452,7 @@ public class ByteInput {
|
|||||||
assert aligned() : "not aligned";
|
assert aligned() : "not aligned";
|
||||||
try {
|
try {
|
||||||
final long value = read64(); // increments bits
|
final long value = read64(); // increments bits
|
||||||
return BitConstraints.safe64u(value);
|
return BitConstraints.safe64u(this, value);
|
||||||
} catch (IndexOutOfBoundsException t) {
|
} catch (IndexOutOfBoundsException t) {
|
||||||
throw new EndOfInput(t);
|
throw new EndOfInput(t);
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,19 @@
|
|||||||
package com.riiablo.io;
|
package com.riiablo.io;
|
||||||
|
|
||||||
public class UnsafeNarrowing extends RuntimeException {
|
public class UnsafeNarrowing extends RuntimeException {
|
||||||
|
public final ByteInput byteInput;
|
||||||
public final long value;
|
public final long value;
|
||||||
|
|
||||||
UnsafeNarrowing(long value) {
|
UnsafeNarrowing(ByteInput byteInput, long value) {
|
||||||
super("value(" + value + ") cannot be safely narrowed!");
|
super("value(" + value + ") cannot be safely narrowed!");
|
||||||
|
this.byteInput = byteInput;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ByteInput byteInput() {
|
||||||
|
return byteInput;
|
||||||
|
}
|
||||||
|
|
||||||
public short u8() {
|
public short u8() {
|
||||||
return (short) value;
|
return (short) value;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user