Moved util subprojects into their own projects

This commit is contained in:
Robinson 2021-01-30 16:17:04 +01:00
parent 86ae8e36da
commit 6a368ccd13
16 changed files with 20 additions and 6259 deletions

View File

@ -1,772 +0,0 @@
/*
* Copyright 2014 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util.bytes;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.nio.ByteBuffer;
/**
* This is "motorola endian", or commonly as "network byte order".
* <p/>
* This is also the default for Java.
* <p/>
* arm is technically bi-endian
*/
@SuppressWarnings("ALL")
public
class BigEndian {
// the following are ALL in Bit-Endian (byte[0] is MOST significant)
/**
* CHAR to and from bytes
*/
public static final
class Char_ {
@SuppressWarnings("fallthrough")
public static
char from(final byte[] bytes, final int offset, final int bytenum) {
char number = 0;
switch (bytenum) {
case 2:
number |= (bytes[offset + 0] & 0xFF) << 8;
case 1:
number |= (bytes[offset + 1] & 0xFF) << 0;
}
return number;
}
@SuppressWarnings("fallthrough")
public static
char from(final byte[] bytes) {
char number = 0;
switch (bytes.length) {
default:
case 2:
number |= (bytes[0] & 0xFF) << 8;
case 1:
number |= (bytes[1] & 0xFF) << 0;
}
return number;
}
public static
char from(final byte b0, final byte b1) {
return (char) ((b0 & 0xFF) << 8 | (b1 & 0xFF) << 0);
}
public static
char from(final ByteBuffer buff) {
return from(buff.get(), buff.get());
}
public static
char from(final InputStream inputStream) throws IOException {
return from((byte) inputStream.read(), (byte) inputStream.read());
}
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
Char_() {
}
}
/**
* UNSIGNED CHAR to and from bytes
*/
public static final
class UChar_ {
@SuppressWarnings("fallthrough")
public static
UShort from(final byte[] bytes, final int offset, final int bytenum) {
char number = 0;
switch (bytenum) {
case 2:
number |= (bytes[offset + 0] & 0xFF) << 8;
case 1:
number |= (bytes[offset + 1] & 0xFF) << 0;
}
return UShort.valueOf(number);
}
@SuppressWarnings("fallthrough")
public static
UShort from(final byte[] bytes) {
short number = 0;
switch (bytes.length) {
default:
case 2:
number |= (bytes[0] & 0xFF) << 8;
case 1:
number |= (bytes[1] & 0xFF) << 0;
}
return UShort.valueOf(number);
}
public static
UShort from(final byte b0, final byte b1) {
return UShort.valueOf((short) ((b0 & 0xFF) << 8) | (b1 & 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
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
UChar_() {
}
}
/**
* SHORT to and from bytes
*/
public static final
class Short_ {
@SuppressWarnings("fallthrough")
public static
short from(final byte[] bytes, final int offset, final int bytenum) {
short number = 0;
switch (bytenum) {
case 2:
number |= (bytes[offset + 0] & 0xFF) << 8;
case 1:
number |= (bytes[offset + 1] & 0xFF) << 0;
}
return number;
}
@SuppressWarnings("fallthrough")
public static
short from(final byte[] bytes) {
short number = 0;
switch (bytes.length) {
default:
case 2:
number |= (bytes[0] & 0xFF) << 8;
case 1:
number |= (bytes[1] & 0xFF) << 0;
}
return number;
}
public static
short from(final byte b0, final byte b1) {
return (short) ((b0 & 0xFF) << 8 | (b1 & 0xFF) << 0);
}
public static
short from(final ByteBuffer buff) {
return from(buff.get(), buff.get());
}
public static
short from(final InputStream inputStream) throws IOException {
return from((byte) inputStream.read(), (byte) inputStream.read());
}
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
Short_() {
}
}
/**
* UNSIGNED SHORT to and from bytes
*/
public static final
class UShort_ {
@SuppressWarnings("fallthrough")
public static
UShort from(final byte[] bytes, final int offset, final int bytenum) {
char number = 0;
switch (bytenum) {
case 2:
number |= (bytes[offset + 0] & 0xFF) << 8;
case 1:
number |= (bytes[offset + 1] & 0xFF) << 0;
}
return UShort.valueOf(number);
}
@SuppressWarnings("fallthrough")
public static
UShort from(final byte[] bytes) {
short number = 0;
switch (bytes.length) {
default:
case 2:
number |= (bytes[0] & 0xFF) << 8;
case 1:
number |= (bytes[1] & 0xFF) << 0;
}
return UShort.valueOf(number);
}
public static
UShort from(final byte b0, final byte b1) {
return UShort.valueOf((short) ((b0 & 0xFF) << 8) | (b1 & 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
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
UShort_() {
}
}
/**
* INT to and from bytes
*/
public static final
class Int_ {
@SuppressWarnings("fallthrough")
public static
int from(final byte[] bytes, final int offset, final int bytenum) {
int number = 0;
switch (bytenum) {
case 4:
number |= (bytes[offset + 0] & 0xFF) << 24;
case 3:
number |= (bytes[offset + 1] & 0xFF) << 16;
case 2:
number |= (bytes[offset + 2] & 0xFF) << 8;
case 1:
number |= (bytes[offset + 3] & 0xFF) << 0;
}
return number;
}
@SuppressWarnings("fallthrough")
public static
int from(final byte[] bytes) {
int number = 0;
switch (bytes.length) {
default:
case 4:
number |= (bytes[0] & 0xFF) << 24;
case 3:
number |= (bytes[1] & 0xFF) << 16;
case 2:
number |= (bytes[2] & 0xFF) << 8;
case 1:
number |= (bytes[3] & 0xFF) << 0;
}
return number;
}
public static
int from(final byte b0, final byte b1, final byte b2, final byte b3) {
return (b0 & 0xFF) << 24 |
(b1 & 0xFF) << 16 |
(b2 & 0xFF) << 8 |
(b3 & 0xFF) << 0;
}
public static
int from(final ByteBuffer buff) {
return from(buff.get(), buff.get(), buff.get(), buff.get());
}
public static
int from(InputStream inputStream) throws IOException {
return from((byte) inputStream.read(), (byte) inputStream.read(), (byte) inputStream.read(), (byte) inputStream.read());
}
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
Int_() {
}
}
/**
* UNSIGNED INT to and from bytes
*/
public static final
class UInt_ {
@SuppressWarnings("fallthrough")
public static
UInteger from(final byte[] bytes, final int offset, final int bytenum) {
int number = 0;
switch (bytenum) {
case 4:
number |= (bytes[offset + 0] & 0xFF) << 24;
case 3:
number |= (bytes[offset + 1] & 0xFF) << 16;
case 2:
number |= (bytes[offset + 2] & 0xFF) << 8;
case 1:
number |= (bytes[offset + 3] & 0xFF) << 0;
}
return UInteger.valueOf(number);
}
@SuppressWarnings("fallthrough")
public static
UInteger from(final byte[] bytes) {
int number = 0;
switch (bytes.length) {
default:
case 4:
number |= (bytes[0] & 0xFF) << 24;
case 3:
number |= (bytes[1] & 0xFF) << 16;
case 2:
number |= (bytes[2] & 0xFF) << 8;
case 1:
number |= (bytes[3] & 0xFF) << 0;
}
return UInteger.valueOf(number);
}
public static
UInteger from(final byte b0, final byte b1, final byte b2, final byte b3) {
int number = (b0 & 0xFF) << 24 |
(b1 & 0xFF) << 16 |
(b2 & 0xFF) << 8 |
(b3 & 0xFF) << 0;
return UInteger.valueOf(number);
}
public static
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();
return new byte[] {(byte) ((num & 0xFF000000L) >> 24), (byte) ((num & 0x00FF0000L) >> 16), (byte) ((num & 0x0000FF00L) >> 8),
(byte) (num & 0x000000FFL >> 0)};
}
public static
void toBytes(final UInteger x, final byte[] bytes, final int offset) {
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
void toBytes(final UInteger x, final byte[] bytes) {
long num = x.longValue();
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
UInt_() {
}
}
/**
* LONG to and from bytes
*/
public static final
class Long_ {
@SuppressWarnings("fallthrough")
public static
long from(final byte[] bytes, final int offset, final int bytenum) {
long number = 0;
switch (bytenum) {
case 8:
number |= (long) (bytes[offset + 0] & 0xFF) << 56;
case 7:
number |= (long) (bytes[offset + 1] & 0xFF) << 48;
case 6:
number |= (long) (bytes[offset + 2] & 0xFF) << 40;
case 5:
number |= (long) (bytes[offset + 3] & 0xFF) << 32;
case 4:
number |= (long) (bytes[offset + 4] & 0xFF) << 24;
case 3:
number |= (long) (bytes[offset + 5] & 0xFF) << 16;
case 2:
number |= (long) (bytes[offset + 6] & 0xFF) << 8;
case 1:
number |= (long) (bytes[offset + 7] & 0xFF) << 0;
}
return number;
}
@SuppressWarnings("fallthrough")
public static
long from(final byte[] bytes) {
long number = 0L;
switch (bytes.length) {
default:
case 8:
number |= (long) (bytes[0] & 0xFF) << 56;
case 7:
number |= (long) (bytes[1] & 0xFF) << 48;
case 6:
number |= (long) (bytes[2] & 0xFF) << 40;
case 5:
number |= (long) (bytes[3] & 0xFF) << 32;
case 4:
number |= (long) (bytes[4] & 0xFF) << 24;
case 3:
number |= (long) (bytes[5] & 0xFF) << 16;
case 2:
number |= (long) (bytes[6] & 0xFF) << 8;
case 1:
number |= (long) (bytes[7] & 0xFF) << 0;
}
return number;
}
public static
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 |
(long) (b1 & 0xFF) << 48 |
(long) (b2 & 0xFF) << 40 |
(long) (b3 & 0xFF) << 32 |
(long) (b4 & 0xFF) << 24 |
(long) (b5 & 0xFF) << 16 |
(long) (b6 & 0xFF) << 8 |
(long) (b7 & 0xFF) << 0;
}
public static
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 >> 56), (byte) (x >> 48), (byte) (x >> 40), (byte) (x >> 32), (byte) (x >> 24), (byte) (x >> 16),
(byte) (x >> 8), (byte) (x >> 0)};
}
public static
void toBytes(final long x, final byte[] bytes, final int offset) {
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
void toBytes(final long x, final byte[] bytes) {
bytes[0] = (byte) (x >> 56);
bytes[1] = (byte) (x >> 48);
bytes[2] = (byte) (x >> 40);
bytes[3] = (byte) (x >> 32);
bytes[4] = (byte) (x >> 24);
bytes[5] = (byte) (x >> 16);
bytes[6] = (byte) (x >> 8);
bytes[7] = (byte) (x >> 0);
}
private
Long_() {
}
}
/**
* UNSIGNED LONG to and from bytes
*/
public static final
class ULong_ {
@SuppressWarnings("fallthrough")
public static
ULong from(final byte[] bytes, final int offset, final int bytenum) {
long number = 0;
switch (bytenum) {
case 8:
number |= (long) (bytes[offset + 0] & 0xFF) << 56;
case 7:
number |= (long) (bytes[offset + 1] & 0xFF) << 48;
case 6:
number |= (long) (bytes[offset + 2] & 0xFF) << 40;
case 5:
number |= (long) (bytes[offset + 3] & 0xFF) << 32;
case 4:
number |= (long) (bytes[offset + 4] & 0xFF) << 24;
case 3:
number |= (long) (bytes[offset + 5] & 0xFF) << 16;
case 2:
number |= (long) (bytes[offset + 6] & 0xFF) << 8;
case 1:
number |= (long) (bytes[offset + 7] & 0xFF) << 0;
}
return ULong.valueOf(number);
}
public static
ULong from(final byte[] bytes) {
BigInteger ulong = new BigInteger(1, bytes);
return ULong.valueOf(ulong);
}
public static
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};
BigInteger ulong = new BigInteger(1, bytes);
return ULong.valueOf(ulong);
}
public static
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
byte[] bytes = x.toBigInteger()
.toByteArray();
if (bytes.length < 8) {
byte[] fixedBytes = new byte[8];
int length = bytes.length;
for (int i = 0; i < 8; i++) {
if (i < length) {
fixedBytes[i] = bytes[i];
}
else {
fixedBytes[i] = 0;
}
}
bytes = fixedBytes;
}
return bytes;
}
public static
void toBytes(final ULong x, final byte[] bytes, final int offset) {
final byte[] bytes1 = toBytes(x);
int length = bytes.length;
int pos = 8;
while (length > 0) {
bytes[pos--] = bytes1[offset + length--];
}
}
public static
void toBytes(final ULong x, final byte[] bytes) {
final byte[] bytes1 = toBytes(x);
int length = bytes.length;
int pos = 8;
while (length > 0) {
bytes[pos--] = bytes1[length--];
}
}
private
ULong_() {
}
}
}

View File

@ -1,114 +0,0 @@
/*
* Copyright 2014 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util.bytes;
import java.util.Arrays;
/**
* Necessary to provide equals and hashcode methods on a byte arrays, if they are to be used as keys in a map/set/etc
*/
public
class ByteArrayWrapper {
private byte[] data;
private Integer hashCode;
private
ByteArrayWrapper() {
// this is necessary for kryo
}
/**
* Permits the re-use of a byte array.
*
* @param copyBytes if TRUE, then the byteArray is copied. if FALSE, the byte array is used as-is.
* Using FALSE IS DANGEROUS!!!! If the underlying byte array is modified, this changes as well.
*/
public
ByteArrayWrapper(byte[] data, boolean copyBytes) {
if (data == null) {
throw new NullPointerException();
}
int length = data.length;
if (copyBytes) {
this.data = new byte[length];
// copy so it's immutable as a key.
System.arraycopy(data, 0, this.data, 0, length);
}
else {
this.data = data;
}
}
/**
* Makes a safe copy of the byte array, so that changes to the original do not affect the wrapper.
* Side affect is additional memory is used.
*/
public static
ByteArrayWrapper copy(byte[] data) {
if (data == null) {
return null;
}
return new ByteArrayWrapper(data, true);
}
/**
* Does not make a copy of the data, so changes to the original will also affect the wrapper.
* Side affect is no extra memory is needed.
*/
public static
ByteArrayWrapper wrap(byte[] data) {
if (data == null) {
return null;
}
return new ByteArrayWrapper(data, false);
}
public
byte[] getBytes() {
return this.data;
}
@Override
public
int hashCode() {
// might be null for a thread because it's stale. who cares, get the value again
Integer hashCode = this.hashCode;
if (hashCode == null) {
hashCode = Arrays.hashCode(this.data);
this.hashCode = hashCode;
}
return hashCode;
}
@Override
public
boolean equals(Object other) {
if (!(other instanceof ByteArrayWrapper)) {
return false;
}
// CANNOT be null, so we don't have to null check!
return Arrays.equals(this.data, ((ByteArrayWrapper) other).data);
}
@Override
public
String toString() {
return "ByteArrayWrapper " + java.util.Arrays.toString(this.data);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,769 +0,0 @@
/*
* Copyright 2014 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util.bytes;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.nio.ByteBuffer;
/**
* This is intel/amd/arm arch!
* <p/>
* arm is technically bi-endian
* <p/>
* Network byte order IS big endian, as is Java.
*/
@SuppressWarnings("ALL")
public
class LittleEndian {
// the following are ALL in Little-Endian (byte[0] is LEAST significant)
/**
* CHAR to and from bytes
*/
public static final
class Char_ {
@SuppressWarnings("fallthrough")
public static
char from(final byte[] bytes, final int offset, final int byteNum) {
char number = 0;
switch (byteNum) {
case 2:
number |= (bytes[offset + 1] & 0xFF) << 8;
case 1:
number |= (bytes[offset + 0] & 0xFF) << 0;
}
return number;
}
@SuppressWarnings("fallthrough")
public static
char from(final byte[] bytes) {
char number = 0;
switch (bytes.length) {
default:
case 2:
number |= (bytes[1] & 0xFF) << 8;
case 1:
number |= (bytes[0] & 0xFF) << 0;
}
return number;
}
public static
char from(final byte b0, final byte b1) {
return (char) ((b1 & 0xFF) << 8 | (b0 & 0xFF) << 0);
}
public static
char from(final ByteBuffer buff) {
return from(buff.get(), buff.get());
}
public static
char from(final InputStream inputStream) throws IOException {
return from((byte) inputStream.read(), (byte) inputStream.read());
}
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
Char_() {
}
}
/**
* UNSIGNED CHAR to and from bytes
*/
public static final
class UChar_ {
@SuppressWarnings("fallthrough")
public static
UShort from(final byte[] bytes, final int offset, final int bytenum) {
char number = 0;
switch (bytenum) {
case 2:
number |= (bytes[offset + 1] & 0xFF) << 8;
case 1:
number |= (bytes[offset + 0] & 0xFF) << 0;
}
return UShort.valueOf(number);
}
@SuppressWarnings("fallthrough")
public static
UShort from(final byte[] bytes) {
short number = 0;
switch (bytes.length) {
default:
case 2:
number |= (bytes[1] & 0xFF) << 8;
case 1:
number |= (bytes[0] & 0xFF) << 0;
}
return UShort.valueOf(number);
}
public static
UShort from(final byte b0, final byte b1) {
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
byte[] toBytes(UShort x) {
int num = x.intValue();
return new byte[] {(byte) (num & 0x00FF >> 0), (byte) ((num & 0xFF00) >> 8)};
}
public static
void toBytes(final UShort x, final byte[] bytes, final int offset) {
int num = x.intValue();
bytes[offset + 1] = (byte) ((num & 0xFF00) >> 8);
bytes[offset + 0] = (byte) (num & 0x00FF >> 0);
}
public static
void toBytes(final UShort x, final byte[] bytes) {
int num = x.intValue();
bytes[1] = (byte) ((num & 0xFF00) >> 8);
bytes[0] = (byte) (num & 0x00FF >> 0);
}
private
UChar_() {
}
}
/**
* SHORT to and from bytes
*/
public static final
class Short_ {
@SuppressWarnings("fallthrough")
public static
short from(final byte[] bytes, final int offset, final int bytenum) {
short number = 0;
switch (bytenum) {
case 2:
number |= (bytes[offset + 1] & 0xFF) << 8;
case 1:
number |= (bytes[offset + 0] & 0xFF) << 0;
}
return number;
}
@SuppressWarnings("fallthrough")
public static
short from(final byte[] bytes) {
short number = 0;
switch (bytes.length) {
default:
case 2:
number |= (bytes[1] & 0xFF) << 8;
case 1:
number |= (bytes[0] & 0xFF) << 0;
}
return number;
}
public static
short from(final byte b0, final byte b1) {
return (short) ((b1 & 0xFF) << 8 | (b0 & 0xFF) << 0);
}
public static
short from(final ByteBuffer buff) {
return from(buff.get(), buff.get());
}
public static
short from(final InputStream inputStream) throws IOException {
return from((byte) inputStream.read(), (byte) inputStream.read());
}
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
Short_() {
}
}
/**
* UNSIGNED SHORT to and from bytes
*/
public static final
class UShort_ {
@SuppressWarnings("fallthrough")
public static
UShort from(final byte[] bytes, final int offset, final int bytenum) {
short number = 0;
switch (bytenum) {
case 2:
number |= (bytes[offset + 1] & 0xFF) << 8;
case 1:
number |= (bytes[offset + 0] & 0xFF) << 0;
}
return UShort.valueOf(number);
}
@SuppressWarnings("fallthrough")
public static
UShort from(final byte[] bytes) {
short number = 0;
switch (bytes.length) {
default:
case 2:
number |= (bytes[1] & 0xFF) << 8;
case 1:
number |= (bytes[0] & 0xFF) << 0;
}
return UShort.valueOf(number);
}
public static
UShort from(final byte b0, final byte b1) {
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
byte[] toBytes(final UShort x) {
int num = x.intValue();
return new byte[] {(byte) (num & 0x00FF >> 0), (byte) ((num & 0xFF00) >> 8)};
}
public static
void toBytes(final UShort x, final byte[] bytes, final int offset) {
int num = x.intValue();
bytes[offset + 1] = (byte) ((num & 0xFF00) >> 8);
bytes[offset + 0] = (byte) (num & 0x00FF >> 0);
}
public static
void toBytes(final UShort x, final byte[] bytes) {
int num = x.intValue();
bytes[1] = (byte) ((num & 0xFF00) >> 8);
bytes[0] = (byte) (num & 0x00FF >> 0);
}
private
UShort_() {
}
}
/**
* INT to and from bytes
*/
public static final
class Int_ {
@SuppressWarnings("fallthrough")
public static
int from(final byte[] bytes, final int offset, final int bytenum) {
int number = 0;
switch (bytenum) {
case 4:
number |= (bytes[offset + 3] & 0xFF) << 24;
case 3:
number |= (bytes[offset + 2] & 0xFF) << 16;
case 2:
number |= (bytes[offset + 1] & 0xFF) << 8;
case 1:
number |= (bytes[offset + 0] & 0xFF) << 0;
}
return number;
}
@SuppressWarnings("fallthrough")
public static
int from(final byte[] bytes) {
int number = 0;
switch (bytes.length) {
default:
case 4:
number |= (bytes[3] & 0xFF) << 24;
case 3:
number |= (bytes[2] & 0xFF) << 16;
case 2:
number |= (bytes[1] & 0xFF) << 8;
case 1:
number |= (bytes[0] & 0xFF) << 0;
}
return number;
}
public static
int from(final byte b0, final byte b1, final byte b2, final byte b3) {
return (b3 & 0xFF) << 24 |
(b2 & 0xFF) << 16 |
(b1 & 0xFF) << 8 |
(b0 & 0xFF) << 0;
}
public static
int from(final ByteBuffer buff) {
return from(buff.get(), buff.get(), buff.get(), buff.get());
}
public static
int 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 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
Int_() {
}
}
/**
* UNSIGNED INT to and from bytes
*/
public static final
class UInt_ {
@SuppressWarnings("fallthrough")
public static
UInteger from(final byte[] bytes, final int offset, final int bytenum) {
int number = 0;
switch (bytenum) {
case 4:
number |= (bytes[offset + 3] & 0xFF) << 24;
case 3:
number |= (bytes[offset + 2] & 0xFF) << 16;
case 2:
number |= (bytes[offset + 1] & 0xFF) << 8;
case 1:
number |= (bytes[offset + 0] & 0xFF) << 0;
}
return UInteger.valueOf(number);
}
@SuppressWarnings("fallthrough")
public static
UInteger from(final byte[] bytes) {
int number = 0;
switch (bytes.length) {
default:
case 4:
number |= (bytes[3] & 0xFF) << 24;
case 3:
number |= (bytes[2] & 0xFF) << 16;
case 2:
number |= (bytes[1] & 0xFF) << 8;
case 1:
number |= (bytes[0] & 0xFF) << 0;
}
return UInteger.valueOf(number);
}
public static
UInteger from(final byte b0, final byte b1, final byte b2, final byte b3) {
int number = (b3 & 0xFF) << 24 |
(b2 & 0xFF) << 16 |
(b1 & 0xFF) << 8 |
(b0 & 0xFF) << 0;
return UInteger.valueOf(number);
}
public static
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();
return new byte[] {(byte) (num & 0x000000FFL >> 0), (byte) ((num & 0x0000FF00L) >> 8), (byte) ((num & 0x00FF0000L) >> 16),
(byte) ((num & 0xFF000000L) >> 24)};
}
public static
void toBytes(final UInteger x, final byte[] bytes, final int offset) {
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
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
UInt_() {
}
}
/**
* LONG to and from bytes
*/
public static final
class Long_ {
@SuppressWarnings("fallthrough")
public static
long from(final byte[] bytes, final int offset, final int bytenum) {
long number = 0;
switch (bytenum) {
case 8:
number |= (long) (bytes[offset + 7] & 0xFF) << 56;
case 7:
number |= (long) (bytes[offset + 6] & 0xFF) << 48;
case 6:
number |= (long) (bytes[offset + 5] & 0xFF) << 40;
case 5:
number |= (long) (bytes[offset + 4] & 0xFF) << 32;
case 4:
number |= (long) (bytes[offset + 3] & 0xFF) << 24;
case 3:
number |= (long) (bytes[offset + 2] & 0xFF) << 16;
case 2:
number |= (long) (bytes[offset + 1] & 0xFF) << 8;
case 1:
number |= (long) (bytes[offset + 0] & 0xFF) << 0;
}
return number;
}
@SuppressWarnings("fallthrough")
public static
long from(final byte[] bytes) {
long number = 0L;
switch (bytes.length) {
default:
case 8:
number |= (long) (bytes[7] & 0xFF) << 56;
case 7:
number |= (long) (bytes[6] & 0xFF) << 48;
case 6:
number |= (long) (bytes[5] & 0xFF) << 40;
case 5:
number |= (long) (bytes[4] & 0xFF) << 32;
case 4:
number |= (long) (bytes[3] & 0xFF) << 24;
case 3:
number |= (long) (bytes[2] & 0xFF) << 16;
case 2:
number |= (long) (bytes[1] & 0xFF) << 8;
case 1:
number |= (long) (bytes[0] & 0xFF) << 0;
}
return number;
}
public static
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 |
(long) (b6 & 0xFF) << 48 |
(long) (b5 & 0xFF) << 40 |
(long) (b4 & 0xFF) << 32 |
(long) (b3 & 0xFF) << 24 |
(long) (b2 & 0xFF) << 16 |
(long) (b1 & 0xFF) << 8 |
(long) (b0 & 0xFF) << 0;
}
public static
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),
(byte) (x >> 48), (byte) (x >> 56),};
}
public static
void toBytes(final long x, final byte[] bytes, final int offset) {
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
void toBytes(final long x, final byte[] bytes) {
bytes[7] = (byte) (x >> 56);
bytes[6] = (byte) (x >> 48);
bytes[5] = (byte) (x >> 40);
bytes[4] = (byte) (x >> 32);
bytes[3] = (byte) (x >> 24);
bytes[2] = (byte) (x >> 16);
bytes[1] = (byte) (x >> 8);
bytes[0] = (byte) (x >> 0);
}
private
Long_() {
}
}
/**
* UNSIGNED LONG to and from bytes
*/
public static final
class ULong_ {
@SuppressWarnings("fallthrough")
public static
ULong from(final byte[] bytes, final int offset, final int bytenum) {
long number = 0;
switch (bytenum) {
case 8:
number |= (long) (bytes[offset + 7] & 0xFF) << 56;
case 7:
number |= (long) (bytes[offset + 6] & 0xFF) << 48;
case 6:
number |= (long) (bytes[offset + 5] & 0xFF) << 40;
case 5:
number |= (long) (bytes[offset + 4] & 0xFF) << 32;
case 4:
number |= (long) (bytes[offset + 3] & 0xFF) << 24;
case 3:
number |= (long) (bytes[offset + 2] & 0xFF) << 16;
case 2:
number |= (long) (bytes[offset + 1] & 0xFF) << 8;
case 1:
number |= (long) (bytes[offset + 0] & 0xFF) << 0;
}
return ULong.valueOf(number);
}
public static
ULong from(final byte[] bytes) {
BigInteger ulong = new BigInteger(1, bytes);
return ULong.valueOf(ulong);
}
public static
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};
BigInteger ulong = new BigInteger(1, bytes);
return ULong.valueOf(ulong);
}
public static
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];
int offset = 0;
byte temp_byte[] = x.toBigInteger()
.toByteArray();
int array_count = temp_byte.length - 1;
for (int i = 7; i >= 0; i--) {
if (array_count >= 0) {
bytes[offset] = temp_byte[array_count];
}
else {
bytes[offset] = (byte) 00;
}
offset++;
array_count--;
}
return bytes;
}
public static
void toBytes(final ULong x, final byte[] bytes, final int offset) {
final byte[] bytes1 = toBytes(x);
int length = bytes.length;
int pos = 8;
while (length > 0) {
bytes[pos--] = bytes1[offset + length--];
}
}
public static
void toBytes(final ULong x, final byte[] bytes) {
final byte[] bytes1 = toBytes(x);
int length = bytes.length;
int pos = 8;
while (length > 0) {
bytes[pos--] = bytes1[length--];
}
}
private
ULong_() {
}
}
}

View File

@ -1,477 +0,0 @@
/*
* Copyright 2014 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (c) 2008, Nathan Sweet
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
* - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package dorkbox.util.bytes;
@SuppressWarnings({"Duplicates", "NumericCastThatLosesPrecision", "UnusedAssignment", "IntegerMultiplicationImplicitCastToLong", "unused"})
public
class OptimizeUtilsByteArray {
/**
* Returns the number of bytes that would be written with {@link #writeInt(byte[], int, boolean)}.
*
* @param optimizePositive
* true if you want to optimize the number of bytes needed to write the length value
*/
public static
int intLength(int value, boolean optimizePositive) {
return OptimizeUtilsByteBuf.intLength(value, optimizePositive);
}
// int
/**
* look at buffer, and see if we can read the length of the int off of it. (from the reader index)
*
* @return 0 if we could not read anything, >0 for the number of bytes for the int on the buffer
*/
@SuppressWarnings("SimplifiableIfStatement")
public static
boolean canReadInt(byte[] buffer) {
int position = 0;
return canReadInt(buffer, position);
}
/**
* FROM KRYO
* <p>
* look at buffer, and see if we can read the length of the int off of it. (from the reader index)
*
* @param position where in the buffer to start reading
* @return 0 if we could not read anything, >0 for the number of bytes for the int on the buffer
*/
@SuppressWarnings("SimplifiableIfStatement")
public static
boolean canReadInt(final byte[] buffer, int position) {
int length = buffer.length;
if (length >= 5) {
return true;
}
if ((buffer[position++] & 0x80) == 0) {
return true;
}
if (position == length) {
return false;
}
if ((buffer[position++] & 0x80) == 0) {
return true;
}
if (position == length) {
return false;
}
if ((buffer[position++] & 0x80) == 0) {
return true;
}
if (position == length) {
return false;
}
if ((buffer[position++] & 0x80) == 0) {
return true;
}
return position != length;
}
/**
* Reads an int from the buffer that was optimized at position 0
*
* @param optimizePositive
* If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (5
* bytes). This ultimately means that it will use fewer bytes for positive numbers.
*/
public static
int readInt(byte[] buffer, boolean optimizePositive) {
int position = 0;
return readInt(buffer, optimizePositive, position);
}
/**
* FROM KRYO
* <p>
* Reads an int from the buffer that was optimized.
*
* @param position where in the buffer to start reading
* @param optimizePositive
* If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (5
* bytes). This ultimately means that it will use fewer bytes for positive numbers.
*/
public static
int readInt(final byte[] buffer, final boolean optimizePositive, int position) {
int b = buffer[position++];
int result = b & 0x7F;
if ((b & 0x80) != 0) {
b = buffer[position++];
result |= (b & 0x7F) << 7;
if ((b & 0x80) != 0) {
b = buffer[position++];
result |= (b & 0x7F) << 14;
if ((b & 0x80) != 0) {
b = buffer[position++];
result |= (b & 0x7F) << 21;
if ((b & 0x80) != 0) {
b = buffer[position++];
result |= (b & 0x7F) << 28;
}
}
}
}
return optimizePositive ? result : result >>> 1 ^ -(result & 1);
}
/**
* Writes the specified int to the buffer using 1 to 5 bytes, depending on the size of the number.
*
* @param optimizePositive
* If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (5
* bytes). This ultimately means that it will use fewer bytes for positive numbers.
*
* @return the number of bytes written.
*/
public static
int writeInt(byte[] buffer, int value, boolean optimizePositive) {
int position = 0;
return writeInt(buffer, value, optimizePositive, position);
}
/**
* FROM KRYO
* <p>
* Writes the specified int to the buffer using 1 to 5 bytes, depending on the size of the number.
*
* @param position where in the buffer to start writing
* @param optimizePositive
* If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (5
* bytes). This ultimately means that it will use fewer bytes for positive numbers.
*
* @return the number of bytes written.
*/
public static
int writeInt(final byte[] buffer, int value, final boolean optimizePositive, int position) {
if (!optimizePositive) {
value = value << 1 ^ value >> 31;
}
if (value >>> 7 == 0) {
buffer[position++] = (byte) value;
return 1;
}
if (value >>> 14 == 0) {
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7);
return 2;
}
if (value >>> 21 == 0) {
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7 | 0x80);
buffer[position++] = (byte) (value >>> 14);
return 3;
}
if (value >>> 28 == 0) {
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7 | 0x80);
buffer[position++] = (byte) (value >>> 14 | 0x80);
buffer[position++] = (byte) (value >>> 21);
return 4;
}
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7 | 0x80);
buffer[position++] = (byte) (value >>> 14 | 0x80);
buffer[position++] = (byte) (value >>> 21 | 0x80);
buffer[position++] = (byte) (value >>> 28);
return 5;
}
/**
* Returns 1-9 bytes that would be written with {@link #writeLong(byte[], long, boolean)}.
*
* @param optimizePositive
* If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9
* bytes). This ultimately means that it will use fewer bytes for positive numbers.
*/
public static
int longLength(long value, boolean optimizePositive) {
return OptimizeUtilsByteBuf.longLength(value, optimizePositive);
}
// long
/**
* Reads a 1-9 byte long.
*
* @param optimizePositive
* If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9
* bytes). This ultimately means that it will use fewer bytes for positive numbers.
*/
public static
long readLong(byte[] buffer, boolean optimizePositive) {
int position = 0;
return readLong(buffer, optimizePositive, position);
}
/**
* FROM KRYO
* <p>
* Reads a 1-9 byte long.
*
* @param position where in the buffer to start reading
* @param optimizePositive
* If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9
* bytes). This ultimately means that it will use fewer bytes for positive numbers.
*/
public static
long readLong(final byte[] buffer, final boolean optimizePositive, int position) {
int b = buffer[position++];
long result = b & 0x7F;
if ((b & 0x80) != 0) {
b = buffer[position++];
result |= (b & 0x7F) << 7;
if ((b & 0x80) != 0) {
b = buffer[position++];
result |= (b & 0x7F) << 14;
if ((b & 0x80) != 0) {
b = buffer[position++];
result |= (b & 0x7F) << 21;
if ((b & 0x80) != 0) {
b = buffer[position++];
result |= (long) (b & 0x7F) << 28;
if ((b & 0x80) != 0) {
b = buffer[position++];
result |= (long) (b & 0x7F) << 35;
if ((b & 0x80) != 0) {
b = buffer[position++];
result |= (long) (b & 0x7F) << 42;
if ((b & 0x80) != 0) {
b = buffer[position++];
result |= (long) (b & 0x7F) << 49;
if ((b & 0x80) != 0) {
b = buffer[position++];
result |= (long) b << 56;
}
}
}
}
}
}
}
}
if (!optimizePositive) {
result = result >>> 1 ^ -(result & 1);
}
return result;
}
/**
* Writes a 1-9 byte long.
*
* @param optimizePositive
* If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9
* bytes).
*
* @return the number of bytes written.
*/
public static
int writeLong(byte[] buffer, long value, boolean optimizePositive) {
int position = 0;
return writeLong(buffer, value, optimizePositive, position);
}
/**
* FROM KRYO
* <p>
* Writes a 1-9 byte long.
*
* @param position where in the buffer to start writing
* @param optimizePositive
* If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9
* bytes).
*
* @return the number of bytes written.
*/
public static
int writeLong(final byte[] buffer, long value, final boolean optimizePositive, int position) {
if (!optimizePositive) {
value = value << 1 ^ value >> 63;
}
if (value >>> 7 == 0) {
buffer[position++] = (byte) value;
return 1;
}
if (value >>> 14 == 0) {
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7);
return 2;
}
if (value >>> 21 == 0) {
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7 | 0x80);
buffer[position++] = (byte) (value >>> 14);
return 3;
}
if (value >>> 28 == 0) {
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7 | 0x80);
buffer[position++] = (byte) (value >>> 14 | 0x80);
buffer[position++] = (byte) (value >>> 21);
return 4;
}
if (value >>> 35 == 0) {
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7 | 0x80);
buffer[position++] = (byte) (value >>> 14 | 0x80);
buffer[position++] = (byte) (value >>> 21 | 0x80);
buffer[position++] = (byte) (value >>> 28);
return 5;
}
if (value >>> 42 == 0) {
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7 | 0x80);
buffer[position++] = (byte) (value >>> 14 | 0x80);
buffer[position++] = (byte) (value >>> 21 | 0x80);
buffer[position++] = (byte) (value >>> 28 | 0x80);
buffer[position++] = (byte) (value >>> 35);
return 6;
}
if (value >>> 49 == 0) {
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7 | 0x80);
buffer[position++] = (byte) (value >>> 14 | 0x80);
buffer[position++] = (byte) (value >>> 21 | 0x80);
buffer[position++] = (byte) (value >>> 28 | 0x80);
buffer[position++] = (byte) (value >>> 35 | 0x80);
buffer[position++] = (byte) (value >>> 42);
return 7;
}
if (value >>> 56 == 0) {
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7 | 0x80);
buffer[position++] = (byte) (value >>> 14 | 0x80);
buffer[position++] = (byte) (value >>> 21 | 0x80);
buffer[position++] = (byte) (value >>> 28 | 0x80);
buffer[position++] = (byte) (value >>> 35 | 0x80);
buffer[position++] = (byte) (value >>> 42 | 0x80);
buffer[position++] = (byte) (value >>> 49);
return 8;
}
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7 | 0x80);
buffer[position++] = (byte) (value >>> 14 | 0x80);
buffer[position++] = (byte) (value >>> 21 | 0x80);
buffer[position++] = (byte) (value >>> 28 | 0x80);
buffer[position++] = (byte) (value >>> 35 | 0x80);
buffer[position++] = (byte) (value >>> 42 | 0x80);
buffer[position++] = (byte) (value >>> 49 | 0x80);
buffer[position++] = (byte) (value >>> 56);
return 9;
}
/**
* look at buffer, and see if we can read the length of the long off of it (from the reader index).
*
* @return 0 if we could not read anything, >0 for the number of bytes for the long on the buffer
*/
public static
boolean canReadLong(byte[] buffer) {
int position = 0;
return canReadLong(buffer, position);
}
/**
* FROM KRYO
* <p>
* look at buffer, and see if we can read the length of the long off of it (from the reader index).
*
* @param position where in the buffer to start reading
* @return 0 if we could not read anything, >0 for the number of bytes for the long on the buffer
*/
private static
boolean canReadLong(final byte[] buffer, int position) {
int limit = buffer.length;
if (limit >= 9) {
return true;
}
if ((buffer[position++] & 0x80) == 0) {
return true;
}
if (position == limit) {
return false;
}
if ((buffer[position++] & 0x80) == 0) {
return true;
}
if (position == limit) {
return false;
}
if ((buffer[position++] & 0x80) == 0) {
return true;
}
if (position == limit) {
return false;
}
if ((buffer[position++] & 0x80) == 0) {
return true;
}
if (position == limit) {
return false;
}
if ((buffer[position++] & 0x80) == 0) {
return true;
}
if (position == limit) {
return false;
}
if ((buffer[position++] & 0x80) == 0) {
return true;
}
if (position == limit) {
return false;
}
if ((buffer[position++] & 0x80) == 0) {
return true;
}
if (position == limit) {
return false;
}
//noinspection SimplifiableIfStatement
if ((buffer[position++] & 0x80) == 0) {
return true;
}
return position != limit;
}
private
OptimizeUtilsByteArray() {
}
}

View File

@ -1,381 +0,0 @@
/*
* Copyright 2014 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (c) 2008, Nathan Sweet
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
* - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package dorkbox.util.bytes;
import io.netty.buffer.ByteBuf;
@SuppressWarnings({"Duplicates", "NumericCastThatLosesPrecision", "UnusedAssignment", "IntegerMultiplicationImplicitCastToLong", "unused"})
public
class OptimizeUtilsByteBuf {
// int
/**
* FROM KRYO
* <p>
* Returns the number of bytes that would be written with {@link #writeInt(ByteBuf, int, boolean)}.
*
* @param optimizePositive
* If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (5
* bytes). This ultimately means that it will use fewer bytes for positive numbers.
*/
public static
int intLength(int value, boolean optimizePositive) {
if (!optimizePositive) {
value = value << 1 ^ value >> 31;
}
if (value >>> 7 == 0) {
return 1;
}
if (value >>> 14 == 0) {
return 2;
}
if (value >>> 21 == 0) {
return 3;
}
if (value >>> 28 == 0) {
return 4;
}
return 5;
}
/**
* FROM KRYO
* <p>
* look at buffer, and see if we can read the length of the int off of it. (from the reader index)
*
* @return 0 if we could not read anything, >0 for the number of bytes for the int on the buffer
*/
public static
int canReadInt(ByteBuf buffer) {
int startIndex = buffer.readerIndex();
try {
int remaining = buffer.readableBytes();
for (int offset = 0, count = 1; offset < 32 && remaining > 0; offset += 7, remaining--, count++) {
int b = buffer.readByte();
if ((b & 0x80) == 0) {
return count;
}
}
return 0;
} finally {
buffer.readerIndex(startIndex);
}
}
/**
* FROM KRYO
* <p>
* Reads an int from the buffer that was optimized.
*
* @param optimizePositive
* If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (5
* bytes). This ultimately means that it will use fewer bytes for positive numbers.
*
* @return the number of bytes written.
*/
public static
int readInt(ByteBuf buffer, boolean optimizePositive) {
int b = buffer.readByte();
int result = b & 0x7F;
if ((b & 0x80) != 0) {
b = buffer.readByte();
result |= (b & 0x7F) << 7;
if ((b & 0x80) != 0) {
b = buffer.readByte();
result |= (b & 0x7F) << 14;
if ((b & 0x80) != 0) {
b = buffer.readByte();
result |= (b & 0x7F) << 21;
if ((b & 0x80) != 0) {
b = buffer.readByte();
result |= (b & 0x7F) << 28;
}
}
}
}
return optimizePositive ? result : result >>> 1 ^ -(result & 1);
}
/**
* FROM KRYO
* <p>
* Writes the specified int to the buffer using 1 to 5 bytes, depending on the size of the number.
*
* @param optimizePositive
* If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (5
* bytes). This ultimately means that it will use fewer bytes for positive numbers.
*
* @return the number of bytes written.
*/
public static
int writeInt(ByteBuf buffer, int value, boolean optimizePositive) {
if (!optimizePositive) {
value = value << 1 ^ value >> 31;
}
if (value >>> 7 == 0) {
buffer.writeByte((byte) value);
return 1;
}
if (value >>> 14 == 0) {
buffer.writeByte((byte) (value & 0x7F | 0x80));
buffer.writeByte((byte) (value >>> 7));
return 2;
}
if (value >>> 21 == 0) {
buffer.writeByte((byte) (value & 0x7F | 0x80));
buffer.writeByte((byte) (value >>> 7 | 0x80));
buffer.writeByte((byte) (value >>> 14));
return 3;
}
if (value >>> 28 == 0) {
buffer.writeByte((byte) (value & 0x7F | 0x80));
buffer.writeByte((byte) (value >>> 7 | 0x80));
buffer.writeByte((byte) (value >>> 14 | 0x80));
buffer.writeByte((byte) (value >>> 21));
return 4;
}
buffer.writeByte((byte) (value & 0x7F | 0x80));
buffer.writeByte((byte) (value >>> 7 | 0x80));
buffer.writeByte((byte) (value >>> 14 | 0x80));
buffer.writeByte((byte) (value >>> 21 | 0x80));
buffer.writeByte((byte) (value >>> 28));
return 5;
}
// long
/**
* Returns the 1-9 bytes that would be written with {@link #writeLong(ByteBuf, long, boolean)}.
*
* @param optimizePositive
* If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9
* bytes). This ultimately means that it will use fewer bytes for positive numbers.
*/
public static
int longLength(long value, boolean optimizePositive) {
if (!optimizePositive) {
value = value << 1 ^ value >> 63;
}
if (value >>> 7 == 0) {
return 1;
}
if (value >>> 14 == 0) {
return 2;
}
if (value >>> 21 == 0) {
return 3;
}
if (value >>> 28 == 0) {
return 4;
}
if (value >>> 35 == 0) {
return 5;
}
if (value >>> 42 == 0) {
return 6;
}
if (value >>> 49 == 0) {
return 7;
}
if (value >>> 56 == 0) {
return 8;
}
return 9;
}
/**
* FROM KRYO
* <p>
* Reads a 1-9 byte long.
*
* @param optimizePositive
* If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9
* bytes). This ultimately means that it will use fewer bytes for positive numbers.
*/
public static
long readLong(ByteBuf buffer, boolean optimizePositive) {
int b = buffer.readByte();
long result = b & 0x7F;
if ((b & 0x80) != 0) {
b = buffer.readByte();
result |= (b & 0x7F) << 7;
if ((b & 0x80) != 0) {
b = buffer.readByte();
result |= (b & 0x7F) << 14;
if ((b & 0x80) != 0) {
b = buffer.readByte();
result |= (b & 0x7F) << 21;
if ((b & 0x80) != 0) {
b = buffer.readByte();
result |= (long) (b & 0x7F) << 28;
if ((b & 0x80) != 0) {
b = buffer.readByte();
result |= (long) (b & 0x7F) << 35;
if ((b & 0x80) != 0) {
b = buffer.readByte();
result |= (long) (b & 0x7F) << 42;
if ((b & 0x80) != 0) {
b = buffer.readByte();
result |= (long) (b & 0x7F) << 49;
if ((b & 0x80) != 0) {
b = buffer.readByte();
result |= (long) b << 56;
}
}
}
}
}
}
}
}
if (!optimizePositive) {
result = result >>> 1 ^ -(result & 1);
}
return result;
}
/**
* FROM KRYO
* <p>
* Writes a 1-9 byte long.
*
* @param optimizePositive
* If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9
* bytes). This ultimately means that it will use fewer bytes for positive numbers.
*
* @return the number of bytes written.
*/
public static
int writeLong(ByteBuf buffer, long value, boolean optimizePositive) {
if (!optimizePositive) {
value = value << 1 ^ value >> 63;
}
if (value >>> 7 == 0) {
buffer.writeByte((byte) value);
return 1;
}
if (value >>> 14 == 0) {
buffer.writeByte((byte) (value & 0x7F | 0x80));
buffer.writeByte((byte) (value >>> 7));
return 2;
}
if (value >>> 21 == 0) {
buffer.writeByte((byte) (value & 0x7F | 0x80));
buffer.writeByte((byte) (value >>> 7 | 0x80));
buffer.writeByte((byte) (value >>> 14));
return 3;
}
if (value >>> 28 == 0) {
buffer.writeByte((byte) (value & 0x7F | 0x80));
buffer.writeByte((byte) (value >>> 7 | 0x80));
buffer.writeByte((byte) (value >>> 14 | 0x80));
buffer.writeByte((byte) (value >>> 21));
return 4;
}
if (value >>> 35 == 0) {
buffer.writeByte((byte) (value & 0x7F | 0x80));
buffer.writeByte((byte) (value >>> 7 | 0x80));
buffer.writeByte((byte) (value >>> 14 | 0x80));
buffer.writeByte((byte) (value >>> 21 | 0x80));
buffer.writeByte((byte) (value >>> 28));
return 5;
}
if (value >>> 42 == 0) {
buffer.writeByte((byte) (value & 0x7F | 0x80));
buffer.writeByte((byte) (value >>> 7 | 0x80));
buffer.writeByte((byte) (value >>> 14 | 0x80));
buffer.writeByte((byte) (value >>> 21 | 0x80));
buffer.writeByte((byte) (value >>> 28 | 0x80));
buffer.writeByte((byte) (value >>> 35));
return 6;
}
if (value >>> 49 == 0) {
buffer.writeByte((byte) (value & 0x7F | 0x80));
buffer.writeByte((byte) (value >>> 7 | 0x80));
buffer.writeByte((byte) (value >>> 14 | 0x80));
buffer.writeByte((byte) (value >>> 21 | 0x80));
buffer.writeByte((byte) (value >>> 28 | 0x80));
buffer.writeByte((byte) (value >>> 35 | 0x80));
buffer.writeByte((byte) (value >>> 42));
return 7;
}
if (value >>> 56 == 0) {
buffer.writeByte((byte) (value & 0x7F | 0x80));
buffer.writeByte((byte) (value >>> 7 | 0x80));
buffer.writeByte((byte) (value >>> 14 | 0x80));
buffer.writeByte((byte) (value >>> 21 | 0x80));
buffer.writeByte((byte) (value >>> 28 | 0x80));
buffer.writeByte((byte) (value >>> 35 | 0x80));
buffer.writeByte((byte) (value >>> 42 | 0x80));
buffer.writeByte((byte) (value >>> 49));
return 8;
}
buffer.writeByte((byte) (value & 0x7F | 0x80));
buffer.writeByte((byte) (value >>> 7 | 0x80));
buffer.writeByte((byte) (value >>> 14 | 0x80));
buffer.writeByte((byte) (value >>> 21 | 0x80));
buffer.writeByte((byte) (value >>> 28 | 0x80));
buffer.writeByte((byte) (value >>> 35 | 0x80));
buffer.writeByte((byte) (value >>> 42 | 0x80));
buffer.writeByte((byte) (value >>> 49 | 0x80));
buffer.writeByte((byte) (value >>> 56));
return 9;
}
/**
* FROM KRYO
* <p>
* look at buffer, and see if we can read the length of the long off of it (from the reader index).
*
* @return 0 if we could not read anything, >0 for the number of bytes for the long on the buffer
*/
public static
int canReadLong(ByteBuf buffer) {
int position = buffer.readerIndex();
try {
int remaining = buffer.readableBytes();
for (int offset = 0, count = 1; offset < 64 && remaining > 0; offset += 7, remaining--, count++) {
int b = buffer.readByte();
if ((b & 0x80) == 0) {
return count;
}
}
return 0;
} finally {
buffer.readerIndex(position);
}
}
}

View File

@ -1,311 +0,0 @@
/*
* Copyright (c) 2011-2017, Data Geekery GmbH (http://www.datageekery.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util.bytes;
import java.io.ObjectStreamException;
import java.math.BigInteger;
/**
* The <code>unsigned byte</code> type
*
* @author Lukas Eder
* @author Ed Schaller
* @author Jens Nerche
*/
public final class UByte extends UNumber implements Comparable<UByte> {
/**
* Generated UID
*/
private static final long serialVersionUID = -6821055240959745390L;
/**
* Cached values
*/
private static final UByte[] VALUES = mkValues();
/**
* A constant holding the minimum value an <code>unsigned byte</code> can
* have, 0.
*/
public static final short MIN_VALUE = 0x00;
/**
* A constant holding the maximum value an <code>unsigned byte</code> can
* have, 2<sup>8</sup>-1.
*/
public static final short MAX_VALUE = 0xff;
/**
* A constant holding the minimum value an <code>unsigned byte</code> can
* have as UByte, 0.
*/
public static final UByte MIN = valueOf(0x00);
/**
* A constant holding the maximum value an <code>unsigned byte</code> can
* have as UByte, 2<sup>8</sup>-1.
*/
public static final UByte MAX = valueOf(0xff);
/**
* The value modelling the content of this <code>unsigned byte</code>
*/
private final short value;
/**
* Generate a cached value for each byte value.
*
* @return Array of cached values for UByte.
*/
private static final UByte[] mkValues() {
UByte[] ret = new UByte[256];
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++)
ret[i & MAX_VALUE] = new UByte((byte) i);
return ret;
}
/**
* Get an instance of an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned byte</code>.
*/
public static UByte valueOf(String value) throws NumberFormatException {
return valueOfUnchecked(rangeCheck(Short.parseShort(value)));
}
/**
* Get an instance of an <code>unsigned byte</code> by masking it with
* <code>0xFF</code> i.e. <code>(byte) -1</code> becomes
* <code>(ubyte) 255</code>
*/
public static UByte valueOf(byte value) {
return valueOfUnchecked((short) (value & MAX_VALUE));
}
/**
* Get the value of a short without checking the value.
*/
private static UByte valueOfUnchecked(short value) throws NumberFormatException {
return VALUES[value & MAX_VALUE];
}
/**
* Get an instance of an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned byte</code>
*/
public static UByte valueOf(short value) throws NumberFormatException {
return valueOfUnchecked(rangeCheck(value));
}
/**
* Get an instance of an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned byte</code>
*/
public static UByte valueOf(int value) throws NumberFormatException {
return valueOfUnchecked(rangeCheck(value));
}
/**
* Get an instance of an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned byte</code>
*/
public static UByte valueOf(long value) throws NumberFormatException {
return valueOfUnchecked(rangeCheck(value));
}
/**
* Create an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned byte</code>
*/
private UByte(long value) throws NumberFormatException {
this.value = rangeCheck(value);
}
/**
* Create an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned byte</code>
*/
private UByte(int value) throws NumberFormatException {
this.value = rangeCheck(value);
}
/**
* Create an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned byte</code>
*/
private UByte(short value) throws NumberFormatException {
this.value = rangeCheck(value);
}
/**
* Create an <code>unsigned byte</code> by masking it with <code>0xFF</code>
* i.e. <code>(byte) -1</code> becomes <code>(ubyte) 255</code>
*/
private UByte(byte value) {
this.value = (short) (value & MAX_VALUE);
}
/**
* Create an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned byte</code>.
*/
private UByte(String value) throws NumberFormatException {
this.value = rangeCheck(Short.parseShort(value));
}
/**
* Throw exception if value out of range (short version)
*
* @param value Value to check
* @return value if it is in range
* @throws NumberFormatException if value is out of range
*/
private static short rangeCheck(short value) throws NumberFormatException {
if (value < MIN_VALUE || value > MAX_VALUE)
throw new NumberFormatException("Value is out of range : " + value);
return value;
}
/**
* Throw exception if value out of range (int version)
*
* @param value Value to check
* @return value if it is in range
* @throws NumberFormatException if value is out of range
*/
private static short rangeCheck(int value) throws NumberFormatException {
if (value < MIN_VALUE || value > MAX_VALUE)
throw new NumberFormatException("Value is out of range : " + value);
return (short) value;
}
/**
* Throw exception if value out of range (long version)
*
* @param value Value to check
* @return value if it is in range
* @throws NumberFormatException if value is out of range
*/
private static short rangeCheck(long value) throws NumberFormatException {
if (value < MIN_VALUE || value > MAX_VALUE)
throw new NumberFormatException("Value is out of range : " + value);
return (short) value;
}
/**
* Replace version read through deserialization with cached version. Note
* that this does not use the {@link #valueOfUnchecked(short)} as we have no
* guarantee that the value from the stream is valid.
*
* @return cached instance of this object's value
* @throws ObjectStreamException
*/
private Object readResolve() throws ObjectStreamException {
return valueOf(value);
}
@Override
public int intValue() {
return value;
}
@Override
public long longValue() {
return value;
}
@Override
public float floatValue() {
return value;
}
@Override
public double doubleValue() {
return value;
}
@Override
public int hashCode() {
return Short.valueOf(value).hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj instanceof UByte)
return value == ((UByte) obj).value;
return false;
}
@Override
public String toString() {
return Short.valueOf(value).toString();
}
@Override
public String toHexString() {
return Integer.toHexString(this.value);
}
@Override
public int compareTo(UByte o) {
return (value < o.value ? -1 : (value == o.value ? 0 : 1));
}
@Override
public BigInteger toBigInteger() {
return BigInteger.valueOf(value);
}
public UByte add(UByte val) throws NumberFormatException {
return valueOf(value + val.value);
}
public UByte add(int val) throws NumberFormatException {
return valueOf(value + val);
}
public UByte subtract(final UByte val) {
return valueOf(value - val.value);
}
public UByte subtract(final int val) {
return valueOf(value - val);
}
}

View File

@ -1,349 +0,0 @@
/*
* Copyright (c) 2011-2017, Data Geekery GmbH (http://www.datageekery.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util.bytes;
import java.io.ObjectStreamException;
import java.math.BigInteger;
/**
* The <code>unsigned int</code> type
*
* @author Lukas Eder
* @author Ed Schaller
* @author Jens Nerche
*/
public final class UInteger extends UNumber implements Comparable<UInteger> {
private static final Class<UInteger> CLASS = UInteger.class;
private static final String CLASS_NAME = CLASS.getName();
/**
* System property name for the property to set the size of the pre-cache.
*/
private static final String PRECACHE_PROPERTY = CLASS_NAME + ".precacheSize";
/**
* Default size for the value cache.
*/
private static final int DEFAULT_PRECACHE_SIZE = 256;
/**
* Generated UID
*/
private static final long serialVersionUID = -6821055240959745390L;
/**
* Cached values
*/
private static final UInteger[] VALUES = mkValues();
/**
* A constant holding the minimum value an <code>unsigned int</code> can
* have, 0.
*/
public static final long MIN_VALUE = 0x00000000;
/**
* A constant holding the maximum value an <code>unsigned int</code> can
* have, 2<sup>32</sup>-1.
*/
public static final long MAX_VALUE = 0xffffffffL;
/**
* A constant holding the minimum value an <code>unsigned int</code> can
* have as UInteger, 0.
*/
public static final UInteger MIN = valueOf(MIN_VALUE);
/**
* A constant holding the maximum value an <code>unsigned int</code> can
* have as UInteger, 2<sup>32</sup>-1.
*/
public static final UInteger MAX = valueOf(MAX_VALUE);
/**
* The value modelling the content of this <code>unsigned int</code>
*/
private final long value;
/**
* Figure out the size of the precache.
*
* @return The parsed value of the system property
* {@link #PRECACHE_PROPERTY} or {@link #DEFAULT_PRECACHE_SIZE} if
* the property is not set, not a number or retrieving results in a
* {@link SecurityException}. If the parsed value is zero or
* negative no cache will be created. If the value is larger than
* {@link Integer#MAX_VALUE} then Integer#MAX_VALUE will be used.
*/
private static final int getPrecacheSize() {
String prop = null;
long propParsed;
try {
prop = System.getProperty(PRECACHE_PROPERTY);
}
catch (SecurityException e) {
// security manager stopped us so use default
// FIXME: should we log this somewhere?
return DEFAULT_PRECACHE_SIZE;
}
if (prop == null)
return DEFAULT_PRECACHE_SIZE;
// empty value
// FIXME: should we log this somewhere?
if (prop.length() <= 0)
return DEFAULT_PRECACHE_SIZE;
try {
propParsed = Long.parseLong(prop);
}
catch (NumberFormatException e) {
// not a valid number
// FIXME: should we log this somewhere?
return DEFAULT_PRECACHE_SIZE;
}
// treat negative value as no cache...
if (propParsed < 0)
return 0;
// FIXME: should we log this somewhere?
if (propParsed > Integer.MAX_VALUE)
return Integer.MAX_VALUE;
return (int) propParsed;
}
/**
* Generate a cached value for initial unsigned integer values.
*
* @return Array of cached values for UInteger
*/
private static final UInteger[] mkValues() {
int precacheSize = getPrecacheSize();
UInteger[] ret;
if (precacheSize <= 0)
return null;
ret = new UInteger[precacheSize];
for (int i = 0; i < precacheSize; i++)
ret[i] = new UInteger(i);
return ret;
}
/**
* Unchecked internal constructor. This serves two purposes: first it allows
* {@link #UInteger(long)} to stay deprecated without warnings and second
* constructor without unnecessary value checks.
*
* @param value The value to wrap
* @param unused Unused parameter to distinguish between this and the
* deprecated public constructor.
*/
private UInteger(long value, boolean unused) {
this.value = value;
}
/**
* Retrieve a cached value.
*
* @param value Cached value to retrieve
* @return Cached value if one exists. Null otherwise.
*/
private static UInteger getCached(long value) {
if (VALUES != null && value < VALUES.length)
return VALUES[(int) value];
return null;
}
/**
* Get the value of a long without checking the value.
*/
private static UInteger valueOfUnchecked(long value) {
UInteger cached;
if ((cached = getCached(value)) != null)
return cached;
return new UInteger(value, true);
}
/**
* Create an <code>unsigned int</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned int</code>.
*/
public static UInteger valueOf(String value) throws NumberFormatException {
return valueOfUnchecked(rangeCheck(Long.parseLong(value)));
}
/**
* Create an <code>unsigned int</code> by masking it with
* <code>0xFFFFFFFF</code> i.e. <code>(int) -1</code> becomes
* <code>(uint) 4294967295</code>
*/
public static UInteger valueOf(int value) {
return valueOfUnchecked(value & MAX_VALUE);
}
/**
* Create an <code>unsigned int</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned byte</code>
*/
public static UInteger valueOf(long value) throws NumberFormatException {
return valueOfUnchecked(rangeCheck(value));
}
/**
* Create an <code>unsigned int</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned int</code>
*/
private UInteger(long value) throws NumberFormatException {
this.value = rangeCheck(value);
}
/**
* Create an <code>unsigned int</code> by masking it with
* <code>0xFFFFFFFF</code> i.e. <code>(int) -1</code> becomes
* <code>(uint) 4294967295</code>
*/
private UInteger(int value) {
this.value = value & MAX_VALUE;
}
/**
* Create an <code>unsigned int</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned int</code>.
*/
private UInteger(String value) throws NumberFormatException {
this.value = rangeCheck(Long.parseLong(value));
}
/**
* Throw exception if value out of range (long version)
*
* @param value Value to check
* @return value if it is in range
* @throws NumberFormatException if value is out of range
*/
private static long rangeCheck(long value) throws NumberFormatException {
if (value < MIN_VALUE || value > MAX_VALUE)
throw new NumberFormatException("Value is out of range : " + value);
return value;
}
/**
* Replace version read through deserialization with cached version.
*
* @return cached instance of this object's value if one exists, otherwise
* this object
* @throws ObjectStreamException
*/
private Object readResolve() throws ObjectStreamException {
UInteger cached;
// the value read could be invalid so check it
rangeCheck(value);
if ((cached = getCached(value)) != null)
return cached;
return this;
}
@Override
public int intValue() {
return (int) value;
}
@Override
public long longValue() {
return value;
}
@Override
public float floatValue() {
return value;
}
@Override
public double doubleValue() {
return value;
}
@Override
public BigInteger toBigInteger() {
return BigInteger.valueOf(value);
}
@Override
public int hashCode() {
return Long.valueOf(value).hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj instanceof UInteger)
return value == ((UInteger) obj).value;
return false;
}
@Override
public String toString() {
return Long.valueOf(value).toString();
}
@Override
public String toHexString() {
return Long.toHexString(this.value);
}
@Override
public int compareTo(UInteger o) {
return (value < o.value ? -1 : (value == o.value ? 0 : 1));
}
public UInteger add(final UInteger val) {
return valueOf(value + val.value);
}
public UInteger add(final int val) {
return valueOf(value + val);
}
public UInteger subtract(final UInteger val) {
return valueOf(value - val.value);
}
public UInteger subtract(final int val) {
return valueOf(value - val);
}
}

View File

@ -1,273 +0,0 @@
/*
* Copyright (c) 2011-2017, Data Geekery GmbH (http://www.datageekery.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util.bytes;
import java.math.BigInteger;
/**
* The <code>unsigned long</code> type
*
* @author Lukas Eder
* @author Jens Nerche
* @author Ivan Sokolov
*/
public final class ULong extends UNumber implements Comparable<ULong> {
/**
* Generated UID
*/
private static final long serialVersionUID = -6821055240959745390L;
/**
* A constant holding the minimum value an <code>unsigned long</code> can
* have, 0.
*/
public static final BigInteger MIN_VALUE = BigInteger.ZERO;
/**
* A constant holding the maximum value an <code>unsigned long</code> can
* have, 2<sup>64</sup>-1.
*/
public static final BigInteger MAX_VALUE = new BigInteger("18446744073709551615");
/**
* A constant holding the maximum value + 1 an <code>signed long</code> can
* have, 2<sup>63</sup>.
*/
public static final BigInteger MAX_VALUE_LONG = new BigInteger("9223372036854775808");
/**
* A constant holding the minimum value an <code>unsigned long</code> can
* have as ULong, 0.
*/
public static final ULong MIN = valueOf(MIN_VALUE.longValue());
/**
* A constant holding the maximum value + 1 an <code>signed long</code> can
* have as ULong, 2<sup>63</sup>.
*/
public static final ULong MAX = valueOf(MAX_VALUE);
/**
* The value modelling the content of this <code>unsigned long</code>
*/
private final long value;
/**
* Create an <code>unsigned long</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned long</code>.
*/
public static ULong valueOf(String value) throws NumberFormatException {
return new ULong(value);
}
/**
* Create an <code>unsigned long</code> by masking it with
* <code>0xFFFFFFFFFFFFFFFF</code> i.e. <code>(long) -1</code> becomes
* <code>(uint) 18446744073709551615</code>
*/
public static ULong valueOf(long value) {
return new ULong(value);
}
/**
* Create an <code>unsigned long</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned long</code>
*/
public static ULong valueOf(BigInteger value) throws NumberFormatException {
return new ULong(value);
}
public static int compare(long x, long y) {
x += Long.MIN_VALUE;
y += Long.MIN_VALUE;
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
/**
* Create an <code>unsigned long</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned long</code>
*/
private ULong(BigInteger value) throws NumberFormatException {
if (value.compareTo(MIN_VALUE) < 0 || value.compareTo(MAX_VALUE) > 0)
throw new NumberFormatException();
else
this.value = value.longValue();
}
/**
* Create an <code>unsigned long</code> by masking it with
* <code>0xFFFFFFFFFFFFFFFF</code> i.e. <code>(long) -1</code> becomes
* <code>(uint) 18446744073709551615</code>
*/
private ULong(long value) {
this.value = value;
}
/**
* Create an <code>unsigned long</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned long</code>.
*/
private ULong(String value) throws NumberFormatException {
if (value == null)
throw new NumberFormatException("null");
int length = value.length();
if (length == 0)
throw new NumberFormatException("Empty input string");
if (value.charAt(0) == '-')
throw new NumberFormatException(
String.format("Illegal leading minus sign on unsigned string %s", value));
if (length <= 18) {
this.value = Long.parseLong(value, 10);
return;
}
final long first = Long.parseLong(value.substring(0, length - 1), 10);
final int second = Character.digit(value.charAt(length - 1), 10);
if (second < 0)
throw new NumberFormatException("Bad digit at end of " + value);
long result = first * 10 + second;
if (compare(result, first) < 0)
throw new NumberFormatException(
String.format("String value %s exceeds range of unsigned long", value));
this.value = result;
}
@Override
public int intValue() {
return (int) value;
}
@Override
public long longValue() {
return value;
}
@Override
public float floatValue() {
if (value < 0)
return ((float) (value & Long.MAX_VALUE)) + Long.MAX_VALUE;
else
return value;
}
@Override
public double doubleValue() {
if (value < 0)
return ((double) (value & Long.MAX_VALUE)) + Long.MAX_VALUE;
else
return value;
}
@Override
public int hashCode() {
return Long.valueOf(value).hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof ULong)
return value == ((ULong) obj).value;
return false;
}
@Override
public String toString() {
if (value >= 0)
return Long.toString(value);
else
return BigInteger.valueOf(value & Long.MAX_VALUE).add(MAX_VALUE_LONG).toString();
}
@Override
public String toHexString() {
return Long.toHexString(this.value);
}
@Override
public int compareTo(ULong o) {
return compare(value, o.value);
}
public ULong add(ULong val) throws NumberFormatException {
if (value < 0 && val.value < 0)
throw new NumberFormatException();
final long result = value + val.value;
if ((value < 0 || val.value < 0) && result >= 0)
throw new NumberFormatException();
return valueOf(result);
}
public ULong add(int val) throws NumberFormatException {
return add((long) val);
}
public ULong add(long val) throws NumberFormatException {
if (val < 0)
return subtract(Math.abs(val));
final long result = value + val;
if (value < 0 && result >= 0)
throw new NumberFormatException();
return valueOf(result);
}
public ULong subtract(final ULong val) {
if (this.compareTo(val) < 0)
throw new NumberFormatException();
final long result = value - val.value;
if (value < 0 && result >= 0)
throw new NumberFormatException();
return valueOf(result);
}
public ULong subtract(final int val) {
return subtract((long) val);
}
public ULong subtract(final long val) {
if (val < 0)
return add(-val);
if (compare(value, val) < 0)
throw new NumberFormatException();
final long result = value - val;
if (value < 0 && result >= 0)
throw new NumberFormatException();
return valueOf(result);
}
}

View File

@ -1,44 +0,0 @@
/*
* Copyright (c) 2011-2017, Data Geekery GmbH (http://www.datageekery.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util.bytes;
import java.math.BigInteger;
/**
* A base type for unsigned numbers.
*
* @author Lukas Eder
*/
public abstract class UNumber extends Number {
/**
* Generated UID
*/
private static final long serialVersionUID = -7666221938815339843L;
/**
* Get this number as a {@link BigInteger}. This is a convenience method for
* calling <code>new BigInteger(toString())</code>
*/
public BigInteger toBigInteger() {
return new BigInteger(toString());
}
/**
* Converts this number to a hex string representation
*/
public abstract String toHexString();
}

View File

@ -1,195 +0,0 @@
/*
* Copyright (c) 2011-2017, Data Geekery GmbH (http://www.datageekery.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util.bytes;
import java.math.BigInteger;
/**
* The <code>unsigned short</code> type
*
* @author Lukas Eder
* @author Jens Nerche
*/
public final class UShort extends UNumber implements Comparable<UShort> {
/**
* Generated UID
*/
private static final long serialVersionUID = -6821055240959745390L;
/**
* A constant holding the minimum value an <code>unsigned short</code> can
* have, 0.
*/
public static final int MIN_VALUE = 0x0000;
/**
* A constant holding the maximum value an <code>unsigned short</code> can
* have, 2<sup>16</sup>-1.
*/
public static final int MAX_VALUE = 0xffff;
/**
* A constant holding the minimum value an <code>unsigned short</code> can
* have as UShort, 0.
*/
public static final UShort MIN = valueOf(MIN_VALUE);
/**
* A constant holding the maximum value an <code>unsigned short</code> can
* have as UShort, 2<sup>16</sup>-1.
*/
public static final UShort MAX = valueOf(MAX_VALUE);
/**
* The value modelling the content of this <code>unsigned short</code>
*/
private final int value;
/**
* Create an <code>unsigned short</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned short</code>.
*/
public static UShort valueOf(String value) throws NumberFormatException {
return new UShort(value);
}
/**
* Create an <code>unsigned short</code> by masking it with
* <code>0xFFFF</code> i.e. <code>(short) -1</code> becomes
* <code>(ushort) 65535</code>
*/
public static UShort valueOf(short value) {
return new UShort(value);
}
/**
* Create an <code>unsigned short</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned short</code>
*/
public static UShort valueOf(int value) throws NumberFormatException {
return new UShort(value);
}
/**
* Create an <code>unsigned short</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned short</code>
*/
private UShort(int value) throws NumberFormatException {
this.value = value;
rangeCheck();
}
/**
* Create an <code>unsigned short</code> by masking it with
* <code>0xFFFF</code> i.e. <code>(short) -1</code> becomes
* <code>(ushort) 65535</code>
*/
private UShort(short value) {
this.value = value & MAX_VALUE;
}
/**
* Create an <code>unsigned short</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned short</code>.
*/
private UShort(String value) throws NumberFormatException {
this.value = Integer.parseInt(value);
rangeCheck();
}
private void rangeCheck() throws NumberFormatException {
if (value < MIN_VALUE || value > MAX_VALUE)
throw new NumberFormatException("Value is out of range : " + value);
}
@Override
public int intValue() {
return value;
}
@Override
public long longValue() {
return value;
}
@Override
public float floatValue() {
return value;
}
@Override
public double doubleValue() {
return value;
}
@Override
public BigInteger toBigInteger() {
return BigInteger.valueOf(value);
}
@Override
public int hashCode() {
return Integer.valueOf(value).hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof UShort)
return value == ((UShort) obj).value;
return false;
}
@Override
public String toString() {
return Integer.valueOf(value).toString();
}
@Override
public String toHexString() {
return Integer.toHexString(this.value);
}
@Override
public int compareTo(UShort o) {
return (value < o.value ? -1 : (value == o.value ? 0 : 1));
}
public UShort add(UShort val) throws NumberFormatException {
return valueOf(value + val.value);
}
public UShort add(int val) throws NumberFormatException {
return valueOf(value + val);
}
public UShort subtract(final UShort val) {
return valueOf(value - val.value);
}
public UShort subtract(final int val) {
return valueOf(value - val);
}
}

View File

@ -1,192 +0,0 @@
/*
* Copyright (c) 2011-2017, Data Geekery GmbH (http://www.datageekery.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util.bytes;
import java.math.BigInteger;
/**
* A utility class for static access to unsigned number functionality.
* <p>
* It essentially contains factory methods for unsigned number wrappers. In
* future versions, it will also contain some arithmetic methods, handling
* regular arithmetic and bitwise operations
*
* @author Lukas Eder
*/
public final class Unsigned {
/**
* Create an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned byte</code>.
* @see UByte.valueOf(String)
*/
public static
UByte ubyte(String value) throws NumberFormatException {
return value == null ? null : UByte.valueOf(value);
}
/**
* Create an <code>unsigned byte</code> by masking it with <code>0xFF</code>
* i.e. <code>(byte) -1</code> becomes <code>(ubyte) 255</code>
*
* @see UByte#valueOf(byte)
*/
public static UByte ubyte(byte value) {
return UByte.valueOf(value);
}
/**
* Create an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned byte</code>
* @see UByte#valueOf(short)
*/
public static UByte ubyte(short value) throws NumberFormatException {
return UByte.valueOf(value);
}
/**
* Create an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned byte</code>
* @see UByte#valueOf(short)
*/
public static UByte ubyte(int value) throws NumberFormatException {
return UByte.valueOf(value);
}
/**
* Create an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned byte</code>
* @see UByte#valueOf(short)
*/
public static UByte ubyte(long value) throws NumberFormatException {
return UByte.valueOf(value);
}
/**
* Create an <code>unsigned short</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned short</code>.
* @see UShort#valueOf(String)
*/
public static
UShort ushort(String value) throws NumberFormatException {
return value == null ? null : UShort.valueOf(value);
}
/**
* Create an <code>unsigned short</code> by masking it with
* <code>0xFFFF</code> i.e. <code>(short) -1</code> becomes
* <code>(ushort) 65535</code>
*
* @see UShort#valueOf(short)
*/
public static UShort ushort(short value) {
return UShort.valueOf(value);
}
/**
* Create an <code>unsigned short</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned short</code>
* @see UShort#valueOf(int)
*/
public static UShort ushort(int value) throws NumberFormatException {
return UShort.valueOf(value);
}
/**
* Create an <code>unsigned int</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned int</code>.
* @see UInteger#valueOf(String)
*/
public static
UInteger uint(String value) throws NumberFormatException {
return value == null ? null : UInteger.valueOf(value);
}
/**
* Create an <code>unsigned int</code> by masking it with
* <code>0xFFFFFFFF</code> i.e. <code>(int) -1</code> becomes
* <code>(uint) 4294967295</code>
*
* @see UInteger#valueOf(int)
*/
public static UInteger uint(int value) {
return UInteger.valueOf(value);
}
/**
* Create an <code>unsigned int</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned int</code>
* @see UInteger#valueOf(long)
*/
public static UInteger uint(long value) throws NumberFormatException {
return UInteger.valueOf(value);
}
/**
* Create an <code>unsigned long</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned long</code>.
* @see ULong#valueOf(String)
*/
public static
ULong ulong(String value) throws NumberFormatException {
return value == null ? null : ULong.valueOf(value);
}
/**
* Create an <code>unsigned long</code> by masking it with
* <code>0xFFFFFFFFFFFFFFFF</code> i.e. <code>(long) -1</code> becomes
* <code>(uint) 18446744073709551615</code>
*
* @see ULong#valueOf(long)
*/
public static ULong ulong(long value) {
return ULong.valueOf(value);
}
/**
* Create an <code>unsigned long</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned long</code>
* @see ULong#valueOf(BigInteger)
*/
public static ULong ulong(BigInteger value) throws NumberFormatException {
return ULong.valueOf(value);
}
/**
* No instances
*/
private Unsigned() {}
}

View File

@ -42,7 +42,6 @@ import org.lwjgl.util.xxhash.XXHash;
import org.slf4j.Logger;
import dorkbox.os.OS;
import dorkbox.util.bytes.LittleEndian;
/**
* http://en.wikipedia.org/wiki/NSA_Suite_B http://www.nsa.gov/ia/programs/suiteb_cryptography/
@ -262,6 +261,25 @@ class Crypto {
}
}
static
int toInt(final byte[] bytes) {
int number = 0;
switch (bytes.length) {
default:
case 4:
number |= (bytes[3] & 0xFF) << 24;
case 3:
number |= (bytes[2] & 0xFF) << 16;
case 2:
number |= (bytes[1] & 0xFF) << 8;
case 1:
number |= (bytes[0] & 0xFF) << 0;
}
return number;
}
/**
* Specifically, to return the hash of the ALL files/directories inside the jar, minus the action specified (LGPL) files.
*/
@ -309,7 +327,7 @@ class Crypto {
hasAction = true;
// we have an ACTION describing how it was compressed, etc
int fileAction = LittleEndian.Int_.from(new byte[] {extraData[5], extraData[6], extraData[7], extraData[8]});
int fileAction = toInt(new byte[] {extraData[5], extraData[6], extraData[7], extraData[8]});
if ((fileAction & action) != action) {
okToHash = true;

View File

@ -1,211 +0,0 @@
/*
* Copyright 2016 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util.javaFx;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import org.slf4j.LoggerFactory;
import dorkbox.os.OS;
import dorkbox.util.swt.Swt;
/**
* Utility methods for JavaFX.
* <p>
* We use reflection for these methods so that we can compile everything under a version of Java that might not have JavaFX.
*/
public
class JavaFX {
public final static boolean isLoaded;
public final static boolean isGtk3;
// Methods are cached for performance
private static final Method dispatchMethod;
private static final Method isEventThreadMethod;
private static final Object isEventThreadObject;
static {
// There is a silly amount of redirection, simply because we have to be able to access JavaFX, but only if it's in use.
// Since this class is the place other code interacts with, we can use JavaFX stuff if necessary without loading/linking
// the JavaFX classes by accident
// We cannot use getToolkit(), because if JavaFX is not being used, calling getToolkit() will initialize it...
// see: https://bugs.openjdk.java.net/browse/JDK-8090933
Class<?> javaFxLoggerClass = AccessController.doPrivileged(new PrivilegedAction<Class<?>>() {
@Override
public
Class<?> run() {
try {
return Class.forName("com.sun.javafx.logging.PlatformLogger", true, ClassLoader.getSystemClassLoader());
} catch (Exception ignored) {
}
try {
return Class.forName("com.sun.javafx.logging.PlatformLogger", true, Thread.currentThread().getContextClassLoader());
} catch (Exception ignored) {
}
return null;
}
});
boolean isJavaFxLoaded_ = false;
try {
if (javaFxLoggerClass != null) {
// this is important to use reflection, because if JavaFX is not being used, calling getToolkit() will initialize it...
java.lang.reflect.Method m = ClassLoader.class.getDeclaredMethod("findLoadedClass", String.class);
m.setAccessible(true);
ClassLoader cl = ClassLoader.getSystemClassLoader();
// JavaFX Java7,8 is GTK2 only. Java9 can have it be GTK3 if -Djdk.gtk.version=3 is specified
// see http://mail.openjdk.java.net/pipermail/openjfx-dev/2016-May/019100.html
isJavaFxLoaded_ = (null != m.invoke(cl, "com.sun.javafx.tk.Toolkit")) || (null != m.invoke(cl, "javafx.application.Application"));
}
} catch (Throwable e) {
LoggerFactory.getLogger(JavaFX.class).debug("Error detecting if JavaFX is loaded", e);
}
boolean isJavaFxGtk3_ = false;
if (isJavaFxLoaded_) {
// JavaFX Java7,8 is GTK2 only. Java9 can MAYBE have it be GTK3 if `-Djdk.gtk.version=3` is specified
// see
// http://mail.openjdk.java.net/pipermail/openjfx-dev/2016-May/019100.html
// https://docs.oracle.com/javafx/2/system_requirements_2-2-3/jfxpub-system_requirements_2-2-3.htm
// from the page: JavaFX 2.2.3 for Linux requires gtk2 2.18+.
if (OS.javaVersion >= 9) {
// HILARIOUSLY enough, you can use JavaFX + SWT..... And the javaFX GTK version info SHOULD
// be based on what SWT has loaded
// https://github.com/teamfx/openjfx-9-dev-rt/blob/master/modules/javafx.graphics/src/main/java/com/sun/glass/ui/gtk/GtkApplication.java
if (Swt.isLoaded && !Swt.isGtk3) {
isJavaFxGtk3_ = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
@Override
public
Boolean run() {
String version = System.getProperty("jdk.gtk.version", "2");
return "3".equals(version) || version.startsWith("3.");
}
});
}
}
}
isLoaded = isJavaFxLoaded_;
isGtk3 = isJavaFxGtk3_;
Method _isEventThreadMethod = null;
Method _dispatchMethod = null;
Object _isEventThreadObject = null;
if (isJavaFxLoaded_) {
try {
Class<?> clazz = Class.forName("javafx.application.Platform");
_dispatchMethod = clazz.getMethod("runLater", Runnable.class);
// JAVA 7
// javafx.application.Platform.isFxApplicationThread();
// JAVA 8
// com.sun.javafx.tk.Toolkit.getToolkit().isFxUserThread();
if (OS.javaVersion <= 7) {
clazz = Class.forName("javafx.application.Platform");
_isEventThreadMethod = clazz.getMethod("isFxApplicationThread");
_isEventThreadObject = null;
} else {
clazz = Class.forName("com.sun.javafx.tk.Toolkit");
_isEventThreadMethod = clazz.getMethod("getToolkit");
_isEventThreadObject = _isEventThreadMethod.invoke(null);
_isEventThreadMethod = _isEventThreadObject.getClass()
.getMethod("isFxUserThread", (java.lang.Class<?>[])null);
}
} catch (Throwable e) {
LoggerFactory.getLogger(JavaFX.class).error("Cannot initialize JavaFX", e);
}
}
dispatchMethod = _dispatchMethod;
isEventThreadMethod = _isEventThreadMethod;
isEventThreadObject = _isEventThreadObject;
}
public static
void dispatch(final Runnable runnable) {
// javafx.application.Platform.runLater(runnable);
try {
dispatchMethod.invoke(null, runnable);
} catch (Throwable e) {
LoggerFactory.getLogger(JavaFX.class)
.error("Unable to execute JavaFX runLater(). Please create an issue with your OS and Java " +
"version so we may further investigate this issue.");
}
}
public static
boolean isEventThread() {
// JAVA 7
// javafx.application.Platform.isFxApplicationThread();
// JAVA 8
// com.sun.javafx.tk.Toolkit.getToolkit().isFxUserThread();
try {
if (OS.javaVersion <= 7) {
return (Boolean) isEventThreadMethod.invoke(null);
} else {
Class<?>[] args = null;
//noinspection ConstantConditions
return (Boolean) isEventThreadMethod.invoke(isEventThreadObject, (Object) args);
}
} catch (Throwable e) {
LoggerFactory.getLogger(JavaFX.class)
.error("Unable to check if JavaFX is in the event thread. Please create an issue with your OS and Java " +
"version so we may further investigate this issue.");
}
return false;
}
public static
void onShutdown(final Runnable runnable) {
// com.sun.javafx.tk.Toolkit.getToolkit()
// .addShutdownHook(runnable);
try {
Class<?> clazz = Class.forName("com.sun.javafx.tk.Toolkit");
Method method = clazz.getMethod("getToolkit");
Object o = method.invoke(null);
Method m = o.getClass()
.getMethod("addShutdownHook", Runnable.class);
m.invoke(o, runnable);
} catch (Throwable e) {
LoggerFactory.getLogger(JavaFX.class)
.error("Unable to insert shutdown hook into JavaFX. Please create an issue with your OS and Java " +
"version so we may further investigate this issue.");
}
}
}

View File

@ -1,89 +0,0 @@
/*
* Copyright 2016 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util.swt;
import java.security.AccessController;
import java.security.PrivilegedAction;
/**
* Utility methods for SWT. SWT is always available for compiling, so it is not necessary to use reflection to compile it.
* <p>
* SWT system tray types are GtkStatusIcon trays (so we don't want to use them)
*/
public
class Swt {
public final static boolean isLoaded;
public final static boolean isGtk3;
private static final int version;
// NOTE: This class cannot have SWT **ANYTHING** in it
static {
// There is a silly amount of redirection, simply because we have to be able to access SWT, but only if it's in use.
// Since this class is the place other code interacts with, we can use SWT stuff if necessary without loading/linking
// the SWT classes by accident
Class<?> swtErrorClass = AccessController.doPrivileged(new PrivilegedAction<Class<?>>() {
@Override
public
Class<?> run() {
try {
return Class.forName("org.eclipse.swt.SWTError", true, ClassLoader.getSystemClassLoader());
} catch (Exception ignored) {
}
try {
return Class.forName("org.eclipse.swt.SWTError", true, Thread.currentThread().getContextClassLoader());
} catch (Exception ignored) {
}
return null;
}
});
if (swtErrorClass != null) {
// this means that SWT is available in the system at runtime. We use the error class because that DOES NOT intitialize anything
boolean isSwtLoadable_ = SwtAccess.isLoadable();
version = SwtAccess.getVersion();
isLoaded = isSwtLoadable_;
isGtk3 = isSwtLoadable_ && SwtAccess.isGtk3();
SwtAccess.init();
} else {
version = 0;
isLoaded = false;
isGtk3 = false;
}
}
public static
int getVersion() {
return version;
}
public static
void dispatch(final Runnable runnable) {
SwtAccess.dispatch(runnable);
}
public static
boolean isEventThread() {
return SwtAccess.isEventThread();
}
public static
void onShutdown(final Runnable runnable) {
SwtAccess.onShutdown(runnable);
}
}

View File

@ -1,135 +0,0 @@
package dorkbox.util.swt;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import dorkbox.os.OS;
public
class SwtAccess {
private static Display currentDisplay = null;
private static Thread currentDisplayThread = null;
public static
void init() {
// we MUST save this on init, otherwise it is "null" when methods are run from the swing EDT.
currentDisplay = org.eclipse.swt.widgets.Display.getCurrent();
currentDisplayThread = currentDisplay.getThread();
}
static
boolean isLoadable() {
return org.eclipse.swt.SWT.isLoadable();
}
static
void onShutdown(final org.eclipse.swt.widgets.Display currentDisplay, final Runnable runnable) {
// currentDisplay.getShells() must only be called inside the event thread!
org.eclipse.swt.widgets.Shell shell = currentDisplay.getShells()[0];
shell.addListener(org.eclipse.swt.SWT.Close, new org.eclipse.swt.widgets.Listener() {
@Override
public
void handleEvent(final org.eclipse.swt.widgets.Event event) {
runnable.run();
}
});
}
static
int getVersion() {
return SWT.getVersion();
}
/**
* This is only necessary for linux.
*
* @return true if SWT is GTK3. False if SWT is GTK2. If for some reason we DO NOT KNOW, then we return false (GTK2).
*/
static boolean isGtk3() {
if (!OS.isLinux()) {
return false;
}
// required to use reflection, because this is an internal class!
final String SWT_INTERNAL_CLASS = "org.eclipse.swt.internal.gtk.OS";
Class<?> osClass = AccessController.doPrivileged(new PrivilegedAction<Class<?>>() {
@Override
public
Class<?> run() {
try {
return Class.forName(SWT_INTERNAL_CLASS, true, ClassLoader.getSystemClassLoader());
} catch (Exception ignored) {
}
try {
return Class.forName(SWT_INTERNAL_CLASS, true, Thread.currentThread().getContextClassLoader());
} catch (Exception ignored) {
}
return null;
}
});
if (osClass == null) {
return false;
}
final Class<?> clazz = osClass;
Method method = AccessController.doPrivileged(new PrivilegedAction<Method>() {
@Override
public
Method run() {
try {
return clazz.getMethod("gtk_major_version");
} catch (Exception e) {
return null;
}
}
});
if (method == null) {
return false;
}
int version = 0;
try {
version = ((Number)method.invoke(osClass)).intValue();
} catch (Exception ignored) {
// this method doesn't exist.
}
return version == 3;
}
static
void dispatch(final Runnable runnable) {
currentDisplay.syncExec(runnable);
}
static
boolean isEventThread() {
return Thread.currentThread() == currentDisplayThread;
}
static
void onShutdown(final Runnable runnable) {
// currentDisplay.getShells() must only be called inside the event thread!
if (isEventThread()) {
SwtAccess.onShutdown(currentDisplay, runnable);
} else {
dispatch(new Runnable() {
@Override
public
void run() {
SwtAccess.onShutdown(currentDisplay, runnable);
}
});
}
}
}