package io.ebeaninternal.server.type;
import io.ebeaninternal.server.core.BasicTypeConverter;
import java.sql.SQLException;
import java.sql.Types;
import java.util.UUID;
public class ScalarTypeUUIDBinary extends ScalarTypeUUIDBase {
private final boolean optimized;
protected ScalarTypeUUIDBinary(boolean optimized) {
super(false, Types.BINARY);
this.optimized = optimized;
}
@Override
public int getLength() {
return 16;
}
@Override
public Object toJdbcType(Object value) {
return convertToBytes(value, optimized);
}
@Override
public UUID toBeanType(Object value) {
if (value instanceof byte[]) {
return convertFromBytes((byte[]) value, optimized);
} else {
return BasicTypeConverter.toUUID(value, optimized);
}
}
@Override
public void bind(DataBind b, UUID value) throws SQLException {
if (value == null) {
b.setNull(Types.BINARY);
} else {
b.setBytes(convertToBytes(value, optimized));
}
}
@Override
public UUID read(DataReader dataReader) throws SQLException {
byte[] bytes = dataReader.getBytes();
if (bytes == null) {
return null;
} else {
return convertFromBytes(bytes, optimized);
}
}
public static UUID convertFromBytes(byte[] bytes, boolean optimized) {
int usableBytes = Math.min(bytes.length, 16);
byte[] barr = new byte[16];
for (int i = 15, j = usableBytes - 1; j >= 0; i--, j--) {
barr[i] = bytes[j];
}
long msb;
if (optimized) {
msb = ((long)barr[4] << 56) +
((long)(barr[5] & 255) << 48) +
((long)(barr[6] & 255) << 40) +
((long)(barr[7] & 255) << 32) +
((long)(barr[2] & 255) << 24) +
((barr[3] & 255) << 16) +
((barr[0] & 255) << 8) +
((barr[1] & 255) << 0);
} else {
msb = ((long)barr[0] << 56) +
((long)(barr[1] & 255) << 48) +
((long)(barr[2] & 255) << 40) +
((long)(barr[3] & 255) << 32) +
((long)(barr[4] & 255) << 24) +
((barr[5] & 255) << 16) +
((barr[6] & 255) << 8) +
((barr[7] & 255) << 0);
}
long lsb = ((long)barr[8] << 56) +
((long)(barr[9] & 255) << 48) +
((long)(barr[10] & 255) << 40) +
((long)(barr[11] & 255) << 32) +
((long)(barr[12] & 255) << 24) +
((barr[13] & 255) << 16) +
((barr[14] & 255) << 8) +
((barr[15] & 255) << 0);
return new UUID(msb, lsb);
}
public static byte[] convertToBytes(Object value, boolean optimized) {
UUID uuid = (UUID) value;
byte[] ret = new byte[16];
long l = uuid.getMostSignificantBits();
if (optimized) {
ret[0] = (byte) (l >>> 8);
ret[1] = (byte) (l >>> 0);
ret[2] = (byte) (l >>> 24);
ret[3] = (byte) (l >>> 16);
ret[4] = (byte) (l >>> 56);
ret[5] = (byte) (l >>> 48);
ret[6] = (byte) (l >>> 40);
ret[7] = (byte) (l >>> 32);
} else {
ret[0] = (byte) (l >>> 56);
ret[1] = (byte) (l >>> 48);
ret[2] = (byte) (l >>> 40);
ret[3] = (byte) (l >>> 32);
ret[4] = (byte) (l >>> 24);
ret[5] = (byte) (l >>> 16);
ret[6] = (byte) (l >>> 8);
ret[7] = (byte) (l >>> 0);
}
l = uuid.getLeastSignificantBits();
ret[8] = (byte) (l >>> 56);
ret[9] = (byte) (l >>> 48);
ret[10] = (byte) (l >>> 40);
ret[11] = (byte) (l >>> 32);
ret[12] = (byte) (l >>> 24);
ret[13] = (byte) (l >>> 16);
ret[14] = (byte) (l >>> 8);
ret[15] = (byte) (l >>> 0);
return ret;
}
}