added final modifiers to parameters, and added more 'from' conversion methods

This commit is contained in:
nathan 2016-02-11 03:00:55 +01:00
parent 7184b5d616
commit 7a451e944d
2 changed files with 452 additions and 225 deletions

View File

@ -15,7 +15,6 @@
*/ */
package dorkbox.util.bytes; package dorkbox.util.bytes;
import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.math.BigInteger; import java.math.BigInteger;
@ -41,7 +40,7 @@ class BigEndian {
class Char_ { class Char_ {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
char from(byte[] bytes, int offset, int bytenum) { char from(final byte[] bytes, final int offset, final int bytenum) {
char number = 0; char number = 0;
switch (bytenum) { switch (bytenum) {
@ -56,7 +55,7 @@ class BigEndian {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
char from(byte[] bytes) { char from(final byte[] bytes) {
char number = 0; char number = 0;
switch (bytes.length) { switch (bytes.length) {
@ -70,28 +69,35 @@ class BigEndian {
} }
public static public static
char from(byte b0, byte b1) { char from(final byte b0, final byte b1) {
return (char) ((b0 & 0xFF) << 8 | (b1 & 0xFF) << 0); return (char) ((b0 & 0xFF) << 8 | (b1 & 0xFF) << 0);
} }
public static public static
byte[] toBytes(char x) { char from(final ByteBuffer buff) {
return new byte[] {(byte) (x >> 8), (byte) (x >> 0)};
}
public static
char from(ByteBuffer buff) {
return from(buff.get(), buff.get()); return from(buff.get(), buff.get());
} }
public static public static
char fromStream(InputStream inputStream) throws IOException { char fromStream(final InputStream inputStream) throws IOException {
byte[] b = new byte[2]; return from((byte) inputStream.read(), (byte) inputStream.read());
if (inputStream.read(b) != 2) { }
throw new EOFException();
}
return from(b[0], b[1]); public static
byte[] toBytes(final char x) {
return new byte[] {(byte) (x >> 8), (byte) (x >> 0)};
}
public static
void toBytes(final char x, final byte[] bytes, final int offset) {
bytes[offset + 0] = (byte) (x >> 8);
bytes[offset + 1] = (byte) (x >> 0);
}
public static
void toBytes(final char x, final byte[] bytes) {
bytes[0] = (byte) (x >> 8);
bytes[1] = (byte) (x >> 0);
} }
private private
@ -107,7 +113,7 @@ class BigEndian {
class UChar_ { class UChar_ {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
UShort from(byte[] bytes, int offset, int bytenum) { UShort from(final byte[] bytes, final int offset, final int bytenum) {
char number = 0; char number = 0;
switch (bytenum) { switch (bytenum) {
@ -122,7 +128,7 @@ class BigEndian {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
UShort from(byte[] bytes) { UShort from(final byte[] bytes) {
short number = 0; short number = 0;
switch (bytes.length) { switch (bytes.length) {
@ -136,32 +142,45 @@ class BigEndian {
} }
public static public static
UShort from(byte b0, byte b1) { UShort from(final byte b0, final byte b1) {
return UShort.valueOf((short) ((b0 & 0xFF) << 8) | (b1 & 0xFF) << 0); return UShort.valueOf((short) ((b0 & 0xFF) << 8) | (b1 & 0xFF) << 0);
} }
public static public static
byte[] toBytes(UShort x) { UShort from(final ByteBuffer buff) {
int num = x.intValue();
return new byte[] {(byte) ((num & 0xFF00) >> 8), (byte) (num & 0x00FF >> 0),};
}
public static
UShort from(ByteBuffer buff) {
return from(buff.get(), buff.get()); return from(buff.get(), buff.get());
} }
public static public static
UShort fromStream(InputStream inputStream) throws IOException { UShort fromStream(final InputStream inputStream) throws IOException {
byte[] b = new byte[2]; return from((byte) inputStream.read(), (byte) inputStream.read());
if (inputStream.read(b) != 2) {
throw new EOFException();
}
return from(b[0], b[1]);
} }
public static
byte[] toBytes(final UShort x) {
int num = x.intValue();
return new byte[] {(byte) ((num & 0xFF00) >> 8), (byte) (num & 0x00FF >> 0)};
}
public static
void toBytes(final UShort x, final byte[] bytes, final int offset) {
int num = x.intValue();
bytes[offset + 0] = (byte) ((num & 0xFF00) >> 8);
bytes[offset + 1] = (byte) (num & 0x00FF >> 0);
}
public static
void toBytes(final UShort x, final byte[] bytes) {
int num = x.intValue();
bytes[0] = (byte) ((num & 0xFF00) >> 8);
bytes[1] = (byte) (num & 0x00FF >> 0);
}
private private
UChar_() { UChar_() {
} }
@ -175,7 +194,7 @@ class BigEndian {
class Short_ { class Short_ {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
short from(byte[] bytes, int offset, int bytenum) { short from(final byte[] bytes, final int offset, final int bytenum) {
short number = 0; short number = 0;
switch (bytenum) { switch (bytenum) {
@ -190,7 +209,7 @@ class BigEndian {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
short from(byte[] bytes) { short from(final byte[] bytes) {
short number = 0; short number = 0;
switch (bytes.length) { switch (bytes.length) {
@ -204,28 +223,35 @@ class BigEndian {
} }
public static public static
short from(byte b0, byte b1) { short from(final byte b0, final byte b1) {
return (short) ((b0 & 0xFF) << 8 | (b1 & 0xFF) << 0); return (short) ((b0 & 0xFF) << 8 | (b1 & 0xFF) << 0);
} }
public static public static
byte[] toBytes(short x) { short from(final ByteBuffer buff) {
return new byte[] {(byte) (x >> 8), (byte) (x >> 0)};
}
public static
short from(ByteBuffer buff) {
return from(buff.get(), buff.get()); return from(buff.get(), buff.get());
} }
public static public static
short fromStream(InputStream inputStream) throws IOException { short fromStream(final InputStream inputStream) throws IOException {
byte[] b = new byte[2]; return from((byte) inputStream.read(), (byte) inputStream.read());
if (inputStream.read(b) != 2) { }
throw new EOFException();
}
return from(b[0], b[1]); public static
byte[] toBytes(final short x) {
return new byte[] {(byte) (x >> 8), (byte) (x >> 0)};
}
public static
void toBytes(final short x, final byte[] bytes, final int offset) {
bytes[offset + 0] = (byte) (x >> 8);
bytes[offset + 1] = (byte) (x >> 0);
}
public static
void toBytes(final short x, final byte[] bytes) {
bytes[0] = (byte) (x >> 8);
bytes[1] = (byte) (x >> 0);
} }
private private
@ -241,7 +267,7 @@ class BigEndian {
class UShort_ { class UShort_ {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
UShort from(byte[] bytes, int offset, int bytenum) { UShort from(final byte[] bytes, final int offset, final int bytenum) {
char number = 0; char number = 0;
switch (bytenum) { switch (bytenum) {
@ -256,7 +282,7 @@ class BigEndian {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
UShort from(byte[] bytes) { UShort from(final byte[] bytes) {
short number = 0; short number = 0;
switch (bytes.length) { switch (bytes.length) {
@ -270,30 +296,41 @@ class BigEndian {
} }
public static public static
UShort from(byte b0, byte b1) { UShort from(final byte b0, final byte b1) {
return UShort.valueOf((short) ((b0 & 0xFF) << 8) | (b1 & 0xFF) << 0); return UShort.valueOf((short) ((b0 & 0xFF) << 8) | (b1 & 0xFF) << 0);
} }
public static public static
byte[] toBytes(UShort x) { UShort from(final ByteBuffer buff) {
int num = x.intValue();
return new byte[] {(byte) ((num & 0xFF00) >> 8), (byte) (num & 0x00FF >> 0),};
}
public static
UShort from(ByteBuffer buff) {
return from(buff.get(), buff.get()); return from(buff.get(), buff.get());
} }
public static public static
UShort fromStream(InputStream inputStream) throws IOException { UShort fromStream(final InputStream inputStream) throws IOException {
byte[] b = new byte[2]; return from((byte) inputStream.read(), (byte) inputStream.read());
if (inputStream.read(b) != 2) { }
throw new EOFException();
}
return from(b[0], b[1]); public static
byte[] toBytes(final UShort x) {
final int num = x.intValue();
return new byte[] {(byte) ((num & 0xFF00) >> 8), (byte) (num & 0x00FF >> 0)};
}
public static
void toBytes(final UShort x, final byte[] bytes, final int offset) {
int num = x.intValue();
bytes[offset + 0] = (byte) ((num & 0xFF00) >> 8);
bytes[offset + 1] = (byte) (num & 0x00FF >> 0);
}
public static
void toBytes(final UShort x, final byte[] bytes) {
int num = x.intValue();
bytes[0] = (byte) ((num & 0xFF00) >> 8);
bytes[1] = (byte) (num & 0x00FF >> 0);
} }
private private
@ -309,7 +346,7 @@ class BigEndian {
class Int_ { class Int_ {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
int from(byte[] bytes, int offset, int bytenum) { int from(final byte[] bytes, final int offset, final int bytenum) {
int number = 0; int number = 0;
switch (bytenum) { switch (bytenum) {
@ -328,7 +365,7 @@ class BigEndian {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
int from(byte[] bytes) { int from(final byte[] bytes) {
int number = 0; int number = 0;
switch (bytes.length) { switch (bytes.length) {
@ -346,7 +383,7 @@ class BigEndian {
} }
public static public static
int from(byte b0, byte b1, byte b2, byte b3) { int from(final byte b0, final byte b1, final byte b2, final byte b3) {
return (b0 & 0xFF) << 24 | return (b0 & 0xFF) << 24 |
(b1 & 0xFF) << 16 | (b1 & 0xFF) << 16 |
(b2 & 0xFF) << 8 | (b2 & 0xFF) << 8 |
@ -354,23 +391,34 @@ class BigEndian {
} }
public static public static
byte[] toBytes(int x) { int from(final ByteBuffer buff) {
return new byte[] {(byte) (x >> 24), (byte) (x >> 16), (byte) (x >> 8), (byte) (x >> 0)};
}
public static
int from(ByteBuffer buff) {
return from(buff.get(), buff.get(), buff.get(), buff.get()); return from(buff.get(), buff.get(), buff.get(), buff.get());
} }
public static public static
int fromStream(InputStream inputStream) throws IOException { int fromStream(InputStream inputStream) throws IOException {
byte[] b = new byte[4]; return from((byte) inputStream.read(), (byte) inputStream.read(), (byte) inputStream.read(), (byte) inputStream.read());
if (inputStream.read(b) != 4) { }
throw new EOFException();
}
return from(b[0], b[1], b[2], b[3]); public static
byte[] toBytes(final int x) {
return new byte[] {(byte) (x >> 24), (byte) (x >> 16), (byte) (x >> 8), (byte) (x >> 0)};
}
public static
void toBytes(final int x, final byte[] bytes, final int offset) {
bytes[offset + 0] = (byte) (x >> 24);
bytes[offset + 1] = (byte) (x >> 16);
bytes[offset + 2] = (byte) (x >> 8);
bytes[offset + 3] = (byte) (x >> 0);
}
public static
void toBytes(final int x, final byte[] bytes) {
bytes[0] = (byte) (x >> 24);
bytes[1] = (byte) (x >> 16);
bytes[2] = (byte) (x >> 8);
bytes[3] = (byte) (x >> 0);
} }
private private
@ -386,7 +434,7 @@ class BigEndian {
class UInt_ { class UInt_ {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
UInteger from(byte[] bytes, int offset, int bytenum) { UInteger from(final byte[] bytes, final int offset, final int bytenum) {
int number = 0; int number = 0;
switch (bytenum) { switch (bytenum) {
@ -405,7 +453,7 @@ class BigEndian {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
UInteger from(byte[] bytes) { UInteger from(final byte[] bytes) {
int number = 0; int number = 0;
switch (bytes.length) { switch (bytes.length) {
@ -423,7 +471,7 @@ class BigEndian {
} }
public static public static
UInteger from(byte b0, byte b1, byte b2, byte b3) { UInteger from(final byte b0, final byte b1, final byte b2, final byte b3) {
int number = (b0 & 0xFF) << 24 | int number = (b0 & 0xFF) << 24 |
(b1 & 0xFF) << 16 | (b1 & 0xFF) << 16 |
(b2 & 0xFF) << 8 | (b2 & 0xFF) << 8 |
@ -433,7 +481,17 @@ class BigEndian {
} }
public static public static
byte[] toBytes(UInteger x) { UInteger from(final ByteBuffer buff) {
return from(buff.get(), buff.get(), buff.get(), buff.get());
}
public static
UInteger fromStream(final InputStream inputStream) throws IOException {
return from((byte) inputStream.read(), (byte) inputStream.read(), (byte) inputStream.read(), (byte) inputStream.read());
}
public static
byte[] toBytes(final UInteger x) {
long num = x.longValue(); long num = x.longValue();
return new byte[] {(byte) ((num & 0xFF000000L) >> 24), (byte) ((num & 0x00FF0000L) >> 16), (byte) ((num & 0x0000FF00L) >> 8), return new byte[] {(byte) ((num & 0xFF000000L) >> 24), (byte) ((num & 0x00FF0000L) >> 16), (byte) ((num & 0x0000FF00L) >> 8),
@ -441,18 +499,23 @@ class BigEndian {
} }
public static public static
UInteger from(ByteBuffer buff) { void toBytes(final UInteger x, final byte[] bytes, final int offset) {
return from(buff.get(), buff.get(), buff.get(), buff.get()); long num = x.longValue();
bytes[offset + 0] = (byte) ((num & 0xFF000000L) >> 24);
bytes[offset + 1] = (byte) ((num & 0x00FF0000L) >> 16);
bytes[offset + 2] = (byte) ((num & 0x0000FF00L) >> 8);
bytes[offset + 3] = (byte) (num & 0x000000FFL >> 0);
} }
public static public static
UInteger fromStream(InputStream inputStream) throws IOException { void toBytes(final UInteger x, final byte[] bytes) {
byte[] b = new byte[4]; long num = x.longValue();
if (inputStream.read(b) != 4) {
throw new EOFException();
}
return from(b[0], b[1], b[2], b[3]); bytes[0] = (byte) ((num & 0xFF000000L) >> 24);
bytes[1] = (byte) ((num & 0x00FF0000L) >> 16);
bytes[2] = (byte) ((num & 0x0000FF00L) >> 8);
bytes[3] = (byte) (num & 0x000000FFL >> 0);
} }
private private
@ -468,7 +531,7 @@ class BigEndian {
class Long_ { class Long_ {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
long from(byte[] bytes, int offset, int bytenum) { long from(final byte[] bytes, final int offset, final int bytenum) {
long number = 0; long number = 0;
switch (bytenum) { switch (bytenum) {
@ -495,7 +558,7 @@ class BigEndian {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
long from(byte[] bytes) { long from(final byte[] bytes) {
long number = 0L; long number = 0L;
switch (bytes.length) { switch (bytes.length) {
@ -521,7 +584,7 @@ class BigEndian {
} }
public static public static
long from(byte b0, byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7) { long from(final byte b0, final byte b1, final byte b2, final byte b3, final byte b4, final byte b5, final byte b6, final byte b7) {
return (long) (b0 & 0xFF) << 56 | return (long) (b0 & 0xFF) << 56 |
(long) (b1 & 0xFF) << 48 | (long) (b1 & 0xFF) << 48 |
(long) (b2 & 0xFF) << 40 | (long) (b2 & 0xFF) << 40 |
@ -533,26 +596,53 @@ class BigEndian {
} }
public static public static
byte[] toBytes(long x) { long from(final ByteBuffer buff) {
return from(buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get());
}
public static
long fromStream(final InputStream inputStream) throws IOException {
return from((byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read());
}
public static
byte[] toBytes(final long x) {
return new byte[] {(byte) (x >> 56), (byte) (x >> 48), (byte) (x >> 40), (byte) (x >> 32), (byte) (x >> 24), (byte) (x >> 16), return new byte[] {(byte) (x >> 56), (byte) (x >> 48), (byte) (x >> 40), (byte) (x >> 32), (byte) (x >> 24), (byte) (x >> 16),
(byte) (x >> 8), (byte) (x >> 0)}; (byte) (x >> 8), (byte) (x >> 0)};
} }
public static public static
long from(ByteBuffer buff) { void toBytes(final long x, final byte[] bytes, final int offset) {
return from(buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get()); bytes[offset + 0] = (byte) (x >> 56);
bytes[offset + 1] = (byte) (x >> 48);
bytes[offset + 2] = (byte) (x >> 40);
bytes[offset + 3] = (byte) (x >> 32);
bytes[offset + 4] = (byte) (x >> 24);
bytes[offset + 5] = (byte) (x >> 16);
bytes[offset + 6] = (byte) (x >> 8);
bytes[offset + 7] = (byte) (x >> 0);
} }
public static public static
long fromStream(InputStream inputStream) throws IOException { void toBytes(final long x, final byte[] bytes) {
byte[] b = new byte[8]; bytes[0] = (byte) (x >> 56);
if (inputStream.read(b) != 8) { bytes[1] = (byte) (x >> 48);
throw new EOFException(); bytes[2] = (byte) (x >> 40);
} bytes[3] = (byte) (x >> 32);
bytes[4] = (byte) (x >> 24);
return from(b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]); bytes[5] = (byte) (x >> 16);
bytes[6] = (byte) (x >> 8);
bytes[7] = (byte) (x >> 0);
} }
private private
Long_() { Long_() {
} }
@ -566,7 +656,7 @@ class BigEndian {
class ULong_ { class ULong_ {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
ULong from(byte[] bytes, int offset, int bytenum) { ULong from(final byte[] bytes, final int offset, final int bytenum) {
long number = 0; long number = 0;
switch (bytenum) { switch (bytenum) {
@ -592,20 +682,37 @@ class BigEndian {
} }
public static public static
ULong from(byte[] bytes) { ULong from(final byte[] bytes) {
BigInteger ulong = new BigInteger(1, bytes); BigInteger ulong = new BigInteger(1, bytes);
return ULong.valueOf(ulong); return ULong.valueOf(ulong);
} }
public static public static
ULong from(byte b0, byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7) { ULong from(final byte b0, final byte b1, final byte b2, final byte b3, final byte b4, final byte b5, final byte b6, final byte b7) {
byte[] bytes = new byte[] {b0, b1, b2, b3, b4, b5, b6, b7}; byte[] bytes = new byte[] {b0, b1, b2, b3, b4, b5, b6, b7};
BigInteger ulong = new BigInteger(1, bytes); BigInteger ulong = new BigInteger(1, bytes);
return ULong.valueOf(ulong); return ULong.valueOf(ulong);
} }
public static public static
byte[] toBytes(ULong x) { ULong from(final ByteBuffer buff) {
return from(buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get());
}
public static
ULong from(final InputStream inputStream) throws IOException {
return from((byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read());
}
public static
byte[] toBytes(final ULong x) {
// returns the shortest length byte array possible // returns the shortest length byte array possible
byte[] bytes = x.toBigInteger() byte[] bytes = x.toBigInteger()
.toByteArray(); .toByteArray();
@ -630,20 +737,28 @@ class BigEndian {
} }
public static public static
ULong from(ByteBuffer buff) { void toBytes(final ULong x, final byte[] bytes, final int offset) {
return from(buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get()); final byte[] bytes1 = toBytes(x);
int length = bytes.length;
int pos = 8;
while (length > 0) {
bytes[pos--] = bytes1[offset + length--];
}
} }
public static public static
ULong from(InputStream inputStream) throws IOException { void toBytes(final ULong x, final byte[] bytes) {
byte[] b = new byte[8]; final byte[] bytes1 = toBytes(x);
if (inputStream.read(b) != 8) { int length = bytes.length;
throw new EOFException(); int pos = 8;
}
return from(b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]); while (length > 0) {
bytes[pos--] = bytes1[length--];
}
} }
private private
ULong_() { ULong_() {
} }

View File

@ -15,7 +15,6 @@
*/ */
package dorkbox.util.bytes; package dorkbox.util.bytes;
import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.math.BigInteger; import java.math.BigInteger;
@ -41,7 +40,7 @@ class LittleEndian {
class Char_ { class Char_ {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
char from(byte[] bytes, int offset, int byteNum) { char from(final byte[] bytes, final int offset, final int byteNum) {
char number = 0; char number = 0;
switch (byteNum) { switch (byteNum) {
@ -56,7 +55,7 @@ class LittleEndian {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
char from(byte[] bytes) { char from(final byte[] bytes) {
char number = 0; char number = 0;
switch (bytes.length) { switch (bytes.length) {
@ -70,30 +69,38 @@ class LittleEndian {
} }
public static public static
char from(byte b0, byte b1) { char from(final byte b0, final byte b1) {
return (char) ((b1 & 0xFF) << 8 | (b0 & 0xFF) << 0); return (char) ((b1 & 0xFF) << 8 | (b0 & 0xFF) << 0);
} }
public static public static
byte[] toBytes(char x) { char from(final ByteBuffer buff) {
return new byte[] {(byte) (x >> 0), (byte) (x >> 8)};
}
public static
char from(ByteBuffer buff) {
return from(buff.get(), buff.get()); return from(buff.get(), buff.get());
} }
public static public static
char from(InputStream inputStream) throws IOException { char from(final InputStream inputStream) throws IOException {
byte[] b = new byte[2]; return from((byte) inputStream.read(), (byte) inputStream.read());
if (inputStream.read(b) != 2) {
throw new EOFException();
}
return from(b[0], b[1]);
} }
public static
byte[] toBytes(final char x) {
return new byte[] {(byte) (x >> 0), (byte) (x >> 8)};
}
public static
void toBytes(final char x, final byte[] bytes, final int offset) {
bytes[offset + 1] = (byte) (x >> 8);
bytes[offset + 0] = (byte) (x >> 0);
}
public static
void toBytes(final char x, final byte[] bytes) {
bytes[1] = (byte) (x >> 8);
bytes[0] = (byte) (x >> 0);
}
private private
Char_() { Char_() {
} }
@ -107,7 +114,7 @@ class LittleEndian {
class UChar_ { class UChar_ {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
UShort from(byte[] bytes, int offset, int bytenum) { UShort from(final byte[] bytes, final int offset, final int bytenum) {
char number = 0; char number = 0;
switch (bytenum) { switch (bytenum) {
@ -122,7 +129,7 @@ class LittleEndian {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
UShort from(byte[] bytes) { UShort from(final byte[] bytes) {
short number = 0; short number = 0;
switch (bytes.length) { switch (bytes.length) {
@ -136,10 +143,20 @@ class LittleEndian {
} }
public static public static
UShort from(byte b0, byte b1) { UShort from(final byte b0, final byte b1) {
return UShort.valueOf((short) ((b1 & 0xFF) << 8) | (b0 & 0xFF) << 0); return UShort.valueOf((short) ((b1 & 0xFF) << 8) | (b0 & 0xFF) << 0);
} }
public static
UShort from(final ByteBuffer buff) {
return from(buff.get(), buff.get());
}
public static
UShort from(final InputStream inputStream) throws IOException {
return from((byte) inputStream.read(), (byte) inputStream.read());
}
public static public static
byte[] toBytes(UShort x) { byte[] toBytes(UShort x) {
int num = x.intValue(); int num = x.intValue();
@ -148,18 +165,19 @@ class LittleEndian {
} }
public static public static
UShort from(ByteBuffer buff) { void toBytes(final UShort x, final byte[] bytes, final int offset) {
return from(buff.get(), buff.get()); int num = x.intValue();
bytes[offset + 1] = (byte) ((num & 0xFF00) >> 8);
bytes[offset + 0] = (byte) (num & 0x00FF >> 0);
} }
public static public static
UShort from(InputStream inputStream) throws IOException { void toBytes(final UShort x, final byte[] bytes) {
byte[] b = new byte[2]; int num = x.intValue();
if (inputStream.read(b) != 2) {
throw new EOFException();
}
return from(b[0], b[1]); bytes[1] = (byte) ((num & 0xFF00) >> 8);
bytes[0] = (byte) (num & 0x00FF >> 0);
} }
private private
@ -175,7 +193,7 @@ class LittleEndian {
class Short_ { class Short_ {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
short from(byte[] bytes, int offset, int bytenum) { short from(final byte[] bytes, final int offset, final int bytenum) {
short number = 0; short number = 0;
switch (bytenum) { switch (bytenum) {
@ -190,7 +208,7 @@ class LittleEndian {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
short from(byte[] bytes) { short from(final byte[] bytes) {
short number = 0; short number = 0;
switch (bytes.length) { switch (bytes.length) {
@ -204,28 +222,35 @@ class LittleEndian {
} }
public static public static
short from(byte b0, byte b1) { short from(final byte b0, final byte b1) {
return (short) ((b1 & 0xFF) << 8 | (b0 & 0xFF) << 0); return (short) ((b1 & 0xFF) << 8 | (b0 & 0xFF) << 0);
} }
public static public static
byte[] toBytes(short x) { short from(final ByteBuffer buff) {
return new byte[] {(byte) (x >> 0), (byte) (x >> 8)};
}
public static
short from(ByteBuffer buff) {
return from(buff.get(), buff.get()); return from(buff.get(), buff.get());
} }
public static public static
short from(InputStream inputStream) throws IOException { short from(final InputStream inputStream) throws IOException {
byte[] b = new byte[2]; return from((byte) inputStream.read(), (byte) inputStream.read());
if (inputStream.read(b) != 2) { }
throw new EOFException();
}
return from(b[0], b[1]); public static
byte[] toBytes(final short x) {
return new byte[] {(byte) (x >> 0), (byte) (x >> 8)};
}
public static
void toBytes(final short x, final byte[] bytes, final int offset) {
bytes[offset + 1] = (byte) (x >> 8);
bytes[offset + 0] = (byte) (x >> 0);
}
public static
void toBytes(final short x, final byte[] bytes) {
bytes[1] = (byte) (x >> 8);
bytes[0] = (byte) (x >> 0);
} }
private private
@ -241,7 +266,7 @@ class LittleEndian {
class UShort_ { class UShort_ {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
UShort from(byte[] bytes, int offset, int bytenum) { UShort from(final byte[] bytes, final int offset, final int bytenum) {
short number = 0; short number = 0;
switch (bytenum) { switch (bytenum) {
@ -256,7 +281,7 @@ class LittleEndian {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
UShort from(byte[] bytes) { UShort from(final byte[] bytes) {
short number = 0; short number = 0;
switch (bytes.length) { switch (bytes.length) {
@ -270,30 +295,40 @@ class LittleEndian {
} }
public static public static
UShort from(byte b0, byte b1) { UShort from(final byte b0, final byte b1) {
return UShort.valueOf((short) ((b1 & 0xFF) << 8 | (b0 & 0xFF) << 0)); return UShort.valueOf((short) ((b1 & 0xFF) << 8 | (b0 & 0xFF) << 0));
} }
public static public static
byte[] toBytes(UShort x) { UShort from(final ByteBuffer buff) {
return from(buff.get(), buff.get());
}
public static
UShort from(final InputStream inputStream) throws IOException {
return from((byte) inputStream.read(), (byte) inputStream.read());
}
public static
byte[] toBytes(final UShort x) {
int num = x.intValue(); int num = x.intValue();
return new byte[] {(byte) (num & 0x00FF >> 0), (byte) ((num & 0xFF00) >> 8)}; return new byte[] {(byte) (num & 0x00FF >> 0), (byte) ((num & 0xFF00) >> 8)};
} }
public static public static
UShort from(ByteBuffer buff) { void toBytes(final UShort x, final byte[] bytes, final int offset) {
return from(buff.get(), buff.get()); int num = x.intValue();
bytes[offset + 1] = (byte) ((num & 0xFF00) >> 8);
bytes[offset + 0] = (byte) (num & 0x00FF >> 0);
} }
public static public static
UShort from(InputStream inputStream) throws IOException { void toBytes(final UShort x, final byte[] bytes) {
byte[] b = new byte[2]; int num = x.intValue();
if (inputStream.read(b) != 2) {
throw new EOFException();
}
return from(b[0], b[1]); bytes[1] = (byte) ((num & 0xFF00) >> 8);
bytes[0] = (byte) (num & 0x00FF >> 0);
} }
private private
@ -309,7 +344,7 @@ class LittleEndian {
class Int_ { class Int_ {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
int from(byte[] bytes, int offset, int bytenum) { int from(final byte[] bytes, final int offset, final int bytenum) {
int number = 0; int number = 0;
switch (bytenum) { switch (bytenum) {
@ -328,7 +363,7 @@ class LittleEndian {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
int from(byte[] bytes) { int from(final byte[] bytes) {
int number = 0; int number = 0;
switch (bytes.length) { switch (bytes.length) {
@ -346,7 +381,7 @@ class LittleEndian {
} }
public static public static
int from(byte b0, byte b1, byte b2, byte b3) { int from(final byte b0, final byte b1, final byte b2, final byte b3) {
return (b3 & 0xFF) << 24 | return (b3 & 0xFF) << 24 |
(b2 & 0xFF) << 16 | (b2 & 0xFF) << 16 |
(b1 & 0xFF) << 8 | (b1 & 0xFF) << 8 |
@ -354,23 +389,34 @@ class LittleEndian {
} }
public static public static
byte[] toBytes(int x) { int from(final ByteBuffer buff) {
return new byte[] {(byte) (x >> 0), (byte) (x >> 8), (byte) (x >> 16), (byte) (x >> 24)};
}
public static
int from(ByteBuffer buff) {
return from(buff.get(), buff.get(), buff.get(), buff.get()); return from(buff.get(), buff.get(), buff.get(), buff.get());
} }
public static public static
int from(InputStream inputStream) throws IOException { int from(final InputStream inputStream) throws IOException {
byte[] b = new byte[4]; return from((byte) inputStream.read(), (byte) inputStream.read(), (byte) inputStream.read(), (byte) inputStream.read());
if (inputStream.read(b) != 4) { }
throw new EOFException();
}
return from(b[0], b[1], b[2], b[3]); public static
byte[] toBytes(final int x) {
return new byte[] {(byte) (x >> 0), (byte) (x >> 8), (byte) (x >> 16), (byte) (x >> 24)};
}
public static
void toBytes(final int x, final byte[] bytes, final int offset) {
bytes[offset + 3] = (byte) (x >> 24);
bytes[offset + 2] = (byte) (x >> 16);
bytes[offset + 1] = (byte) (x >> 8);
bytes[offset + 0] = (byte) (x >> 0);
}
public static
void toBytes(final int x, final byte[] bytes) {
bytes[3] = (byte) (x >> 24);
bytes[2] = (byte) (x >> 16);
bytes[1] = (byte) (x >> 8);
bytes[0] = (byte) (x >> 0);
} }
private private
@ -386,7 +432,7 @@ class LittleEndian {
class UInt_ { class UInt_ {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
UInteger from(byte[] bytes, int offset, int bytenum) { UInteger from(final byte[] bytes, final int offset, final int bytenum) {
int number = 0; int number = 0;
switch (bytenum) { switch (bytenum) {
@ -405,7 +451,7 @@ class LittleEndian {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
UInteger from(byte[] bytes) { UInteger from(final byte[] bytes) {
int number = 0; int number = 0;
switch (bytes.length) { switch (bytes.length) {
@ -423,7 +469,7 @@ class LittleEndian {
} }
public static public static
UInteger from(byte b0, byte b1, byte b2, byte b3) { UInteger from(final byte b0, final byte b1, final byte b2, final byte b3) {
int number = (b3 & 0xFF) << 24 | int number = (b3 & 0xFF) << 24 |
(b2 & 0xFF) << 16 | (b2 & 0xFF) << 16 |
(b1 & 0xFF) << 8 | (b1 & 0xFF) << 8 |
@ -433,7 +479,17 @@ class LittleEndian {
} }
public static public static
byte[] toBytes(UInteger x) { UInteger from(final ByteBuffer buff) {
return from(buff.get(), buff.get(), buff.get(), buff.get());
}
public static
UInteger from(final InputStream inputStream) throws IOException {
return from((byte) inputStream.read(), (byte) inputStream.read(), (byte) inputStream.read(), (byte) inputStream.read());
}
public static
byte[] toBytes(final UInteger x) {
long num = x.longValue(); long num = x.longValue();
return new byte[] {(byte) (num & 0x000000FFL >> 0), (byte) ((num & 0x0000FF00L) >> 8), (byte) ((num & 0x00FF0000L) >> 16), return new byte[] {(byte) (num & 0x000000FFL >> 0), (byte) ((num & 0x0000FF00L) >> 8), (byte) ((num & 0x00FF0000L) >> 16),
@ -441,18 +497,24 @@ class LittleEndian {
} }
public static public static
UInteger from(ByteBuffer buff) { void toBytes(final UInteger x, final byte[] bytes, final int offset) {
return from(buff.get(), buff.get(), buff.get(), buff.get()); long num = x.longValue();
bytes[offset + 3] = (byte) ((num & 0xFF000000L) >> 24);
bytes[offset + 2] = (byte) ((num & 0x00FF0000L) >> 16);
bytes[offset + 1] = (byte) ((num & 0x0000FF00L) >> 8);
bytes[offset + 0] = (byte) (num & 0x000000FFL >> 0);
} }
public static
UInteger from(InputStream inputStream) throws IOException {
byte[] b = new byte[4];
if (inputStream.read(b) != 4) {
throw new EOFException();
}
return from(b[0], b[1], b[2], b[3]); public static
void toBytes(final UInteger x, final byte[] bytes) {
long num = x.longValue();
bytes[3] = (byte) ((num & 0xFF000000L) >> 24);
bytes[2] = (byte) ((num & 0x00FF0000L) >> 16);
bytes[1] = (byte) ((num & 0x0000FF00L) >> 8);
bytes[0] = (byte) (num & 0x000000FFL >> 0);
} }
private private
@ -468,7 +530,7 @@ class LittleEndian {
class Long_ { class Long_ {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
long from(byte[] bytes, int offset, int bytenum) { long from(final byte[] bytes, final int offset, final int bytenum) {
long number = 0; long number = 0;
switch (bytenum) { switch (bytenum) {
@ -495,7 +557,7 @@ class LittleEndian {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
long from(byte[] bytes) { long from(final byte[] bytes) {
long number = 0L; long number = 0L;
switch (bytes.length) { switch (bytes.length) {
@ -521,7 +583,7 @@ class LittleEndian {
} }
public static public static
long from(byte b0, byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7) { long from(final byte b0, final byte b1, final byte b2, final byte b3, final byte b4, final byte b5, final byte b6, final byte b7) {
return (long) (b7 & 0xFF) << 56 | return (long) (b7 & 0xFF) << 56 |
(long) (b6 & 0xFF) << 48 | (long) (b6 & 0xFF) << 48 |
(long) (b5 & 0xFF) << 40 | (long) (b5 & 0xFF) << 40 |
@ -533,24 +595,50 @@ class LittleEndian {
} }
public static public static
byte[] toBytes(long x) { long from(final ByteBuffer buff) {
return from(buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get());
}
public static
long from(final InputStream inputStream) throws IOException {
return from((byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read());
}
public static
byte[] toBytes(final long x) {
return new byte[] {(byte) (x >> 0), (byte) (x >> 8), (byte) (x >> 16), (byte) (x >> 24), (byte) (x >> 32), (byte) (x >> 40), return new byte[] {(byte) (x >> 0), (byte) (x >> 8), (byte) (x >> 16), (byte) (x >> 24), (byte) (x >> 32), (byte) (x >> 40),
(byte) (x >> 48), (byte) (x >> 56),}; (byte) (x >> 48), (byte) (x >> 56),};
} }
public static public static
long from(ByteBuffer buff) { void toBytes(final long x, final byte[] bytes, final int offset) {
return from(buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get()); bytes[offset + 7] = (byte) (x >> 56);
bytes[offset + 6] = (byte) (x >> 48);
bytes[offset + 5] = (byte) (x >> 40);
bytes[offset + 4] = (byte) (x >> 32);
bytes[offset + 3] = (byte) (x >> 24);
bytes[offset + 2] = (byte) (x >> 16);
bytes[offset + 1] = (byte) (x >> 8);
bytes[offset + 0] = (byte) (x >> 0);
} }
public static public static
long from(InputStream inputStream) throws IOException { void toBytes(final long x, final byte[] bytes) {
byte[] b = new byte[8]; bytes[7] = (byte) (x >> 56);
if (inputStream.read(b) != 8) { bytes[6] = (byte) (x >> 48);
throw new EOFException(); bytes[5] = (byte) (x >> 40);
} bytes[4] = (byte) (x >> 32);
bytes[3] = (byte) (x >> 24);
return from(b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]); bytes[2] = (byte) (x >> 16);
bytes[1] = (byte) (x >> 8);
bytes[0] = (byte) (x >> 0);
} }
private private
@ -566,7 +654,7 @@ class LittleEndian {
class ULong_ { class ULong_ {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public static public static
ULong from(byte[] bytes, int offset, int bytenum) { ULong from(final byte[] bytes, final int offset, final int bytenum) {
long number = 0; long number = 0;
switch (bytenum) { switch (bytenum) {
@ -592,20 +680,37 @@ class LittleEndian {
} }
public static public static
ULong from(byte[] bytes) { ULong from(final byte[] bytes) {
BigInteger ulong = new BigInteger(1, bytes); BigInteger ulong = new BigInteger(1, bytes);
return ULong.valueOf(ulong); return ULong.valueOf(ulong);
} }
public static public static
ULong from(byte b0, byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7) { ULong from(final byte b0, final byte b1, final byte b2, final byte b3, final byte b4, final byte b5, final byte b6, final byte b7) {
byte[] bytes = new byte[] {b7, b6, b5, b4, b3, b2, b1, b0}; byte[] bytes = new byte[] {b7, b6, b5, b4, b3, b2, b1, b0};
BigInteger ulong = new BigInteger(1, bytes); BigInteger ulong = new BigInteger(1, bytes);
return ULong.valueOf(ulong); return ULong.valueOf(ulong);
} }
public static public static
byte[] toBytes(ULong x) { ULong from(final ByteBuffer buff) {
return from(buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get());
}
public static
ULong from(final InputStream inputStream) throws IOException {
return from((byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read(),
(byte) inputStream.read());
}
public static
byte[] toBytes(final ULong x) {
byte[] bytes = new byte[8]; byte[] bytes = new byte[8];
int offset = 0; int offset = 0;
@ -629,18 +734,25 @@ class LittleEndian {
} }
public static public static
ULong from(ByteBuffer buff) { void toBytes(final ULong x, final byte[] bytes, final int offset) {
return from(buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get(), buff.get()); final byte[] bytes1 = toBytes(x);
int length = bytes.length;
int pos = 8;
while (length > 0) {
bytes[pos--] = bytes1[offset + length--];
}
} }
public static public static
ULong from(InputStream inputStream) throws IOException { void toBytes(final ULong x, final byte[] bytes) {
byte[] b = new byte[8]; final byte[] bytes1 = toBytes(x);
if (inputStream.read(b) != 8) { int length = bytes.length;
throw new EOFException(); int pos = 8;
}
return from(b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]); while (length > 0) {
bytes[pos--] = bytes1[length--];
}
} }
private private