use of java.nio.ByteOrder in project Gadgetbridge by Freeyourgadget.
the class PebbleProtocol method getUUID.
private UUID getUUID(ByteBuffer buf) {
ByteOrder byteOrder = buf.order();
buf.order(ByteOrder.BIG_ENDIAN);
long uuid_high = buf.getLong();
long uuid_low = buf.getLong();
buf.order(byteOrder);
return new UUID(uuid_high, uuid_low);
}
use of java.nio.ByteOrder in project cachecloud by sohutv.
the class MurmurHash method hash.
/**
* Hashes the bytes in a buffer from the current position to the limit.
* @param buf The bytes to hash.
* @param seed The seed for the hash.
* @return The 32 bit murmur hash of the bytes in the buffer.
*/
public static int hash(ByteBuffer buf, int seed) {
// save byte order for later restoration
ByteOrder byteOrder = buf.order();
buf.order(ByteOrder.LITTLE_ENDIAN);
int m = 0x5bd1e995;
int r = 24;
int h = seed ^ buf.remaining();
int k;
while (buf.remaining() >= 4) {
k = buf.getInt();
k *= m;
k ^= k >>> r;
k *= m;
h *= m;
h ^= k;
}
if (buf.remaining() > 0) {
ByteBuffer finish = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN);
// for big-endian version, use this first:
// finish.position(4-buf.remaining());
finish.put(buf).rewind();
h ^= finish.getInt();
h *= m;
}
h ^= h >>> 13;
h *= m;
h ^= h >>> 15;
buf.order(byteOrder);
return h;
}
use of java.nio.ByteOrder in project new-cloud by xie-summer.
the class MurmurHash method hash.
/**
* Hashes the bytes in a buffer from the current position to the limit.
* @param buf The bytes to hash.
* @param seed The seed for the hash.
* @return The 32 bit murmur hash of the bytes in the buffer.
*/
public static int hash(ByteBuffer buf, int seed) {
// save byte order for later restoration
ByteOrder byteOrder = buf.order();
buf.order(ByteOrder.LITTLE_ENDIAN);
int m = 0x5bd1e995;
int r = 24;
int h = seed ^ buf.remaining();
int k;
while (buf.remaining() >= 4) {
k = buf.getInt();
k *= m;
k ^= k >>> r;
k *= m;
h *= m;
h ^= k;
}
if (buf.remaining() > 0) {
ByteBuffer finish = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN);
// for big-endian version, use this first:
// finish.position(4-buf.remaining());
finish.put(buf).rewind();
h ^= finish.getInt();
h *= m;
}
h ^= h >>> 13;
h *= m;
h ^= h >>> 15;
buf.order(byteOrder);
return h;
}
use of java.nio.ByteOrder in project glide by bumptech.
the class DefaultImageHeaderParser method parseExifSegment.
private static int parseExifSegment(RandomAccessReader segmentData) {
final int headerOffsetSize = JPEG_EXIF_SEGMENT_PREAMBLE.length();
short byteOrderIdentifier = segmentData.getInt16(headerOffsetSize);
final ByteOrder byteOrder;
switch(byteOrderIdentifier) {
case MOTOROLA_TIFF_MAGIC_NUMBER:
byteOrder = ByteOrder.BIG_ENDIAN;
break;
case INTEL_TIFF_MAGIC_NUMBER:
byteOrder = ByteOrder.LITTLE_ENDIAN;
break;
default:
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Unknown endianness = " + byteOrderIdentifier);
}
byteOrder = ByteOrder.BIG_ENDIAN;
break;
}
segmentData.order(byteOrder);
int firstIfdOffset = segmentData.getInt32(headerOffsetSize + 4) + headerOffsetSize;
int tagCount = segmentData.getInt16(firstIfdOffset);
for (int i = 0; i < tagCount; i++) {
final int tagOffset = calcTagOffset(firstIfdOffset, i);
final int tagType = segmentData.getInt16(tagOffset);
// We only want orientation.
if (tagType != ORIENTATION_TAG_TYPE) {
continue;
}
final int formatCode = segmentData.getInt16(tagOffset + 2);
// 12 is max format code.
if (formatCode < 1 || formatCode > 12) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Got invalid format code = " + formatCode);
}
continue;
}
final int componentCount = segmentData.getInt32(tagOffset + 4);
if (componentCount < 0) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Negative tiff component count");
}
continue;
}
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Got tagIndex=" + i + " tagType=" + tagType + " formatCode=" + formatCode + " componentCount=" + componentCount);
}
final int byteCount = componentCount + BYTES_PER_FORMAT[formatCode];
if (byteCount > 4) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Got byte count > 4, not orientation, continuing, formatCode=" + formatCode);
}
continue;
}
final int tagValueOffset = tagOffset + 8;
if (tagValueOffset < 0 || tagValueOffset > segmentData.length()) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Illegal tagValueOffset=" + tagValueOffset + " tagType=" + tagType);
}
continue;
}
if (byteCount < 0 || tagValueOffset + byteCount > segmentData.length()) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Illegal number of bytes for TI tag data tagType=" + tagType);
}
continue;
}
// assume componentCount == 1 && fmtCode == 3
return segmentData.getInt16(tagValueOffset);
}
return -1;
}
use of java.nio.ByteOrder in project vcell by virtualcell.
the class VisMeshUtils method readPointDataFromVtuXml.
private static double[] readPointDataFromVtuXml(Document meshFileXmlDocument, String dataName) {
Element vtkFileElement = meshFileXmlDocument.getRootElement();
ByteOrder byteOrder;
if (vtkFileElement.getAttributeValue("byte_order").equals("LittleEndian")) {
byteOrder = ByteOrder.LITTLE_ENDIAN;
} else {
byteOrder = ByteOrder.BIG_ENDIAN;
}
// vtkFileElement.setAttribute("header_type", "UInt32");
Element unstructuredGridElement = vtkFileElement.getChild("UnstructuredGrid");
@SuppressWarnings("unchecked") List<Element> pieceElements = unstructuredGridElement.getChildren("Piece");
if (pieceElements.size() != 1) {
throw new RuntimeException("Expecting exactly one mesh piece, found " + pieceElements.size());
}
// int numberOfCells = Integer.parseInt(pieceElements.get(0).getAttributeValue("NumberOfCells"));
int numberOfPoints = Integer.parseInt(pieceElements.get(0).getAttributeValue("NumberOfPoints"));
Element pointData = pieceElements.get(0).getChild("PointData");
List<Element> dataArrayElements = pointData.getChildren("DataArray");
for (Element dataArrayElement : dataArrayElements) {
if (dataArrayElement.getAttribute("Name").equals(dataName)) {
if (!dataArrayElement.getAttribute("type").equals("Float64")) {
throw new RuntimeException("expecting type Float64");
}
if (!dataArrayElement.getAttribute("format").equals("binary")) {
throw new RuntimeException("expecting format binary");
}
}
String base64 = dataArrayElement.getText().trim();
byte[] bytes = new byte[numberOfPoints * 8 + 4];
ByteBuffer b = ByteBuffer.wrap(DatatypeConverter.parseBase64Binary(base64));
b.order(byteOrder);
double[] data = new double[numberOfPoints];
int numPointsEncoded = b.getInt() / 8;
for (int i = 0; i < numPointsEncoded; i++) {
data[i] = b.getDouble();
}
return data;
}
throw new RuntimeException("point data " + dataName + " not found");
}
Aggregations