use of java.nio.ByteOrder in project xian by happyyangyuan.
the class Shard method hash.
/**
* MurMurHash算法,是非加密HASH算法,性能很高,
* 比传统的CRC32,MD5,SHA-1(这两个算法都是加密HASH算法,复杂度本身就很高,带来的性能上的损害也不可避免)
* 等HASH算法要快很多,而且据说这个算法的碰撞率很低.
* http://murmurhash.googlepages.com/
*/
private Long hash(String key) {
ByteBuffer buf = ByteBuffer.wrap(key.getBytes());
int seed = 0x1234ABCD;
ByteOrder byteOrder = buf.order();
buf.order(ByteOrder.LITTLE_ENDIAN);
long m = 0xc6a4a7935bd1e995L;
int r = 47;
long h = seed ^ (buf.remaining() * m);
long k;
while (buf.remaining() >= 8) {
k = buf.getLong();
k *= m;
k ^= k >>> r;
k *= m;
h ^= k;
h *= m;
}
if (buf.remaining() > 0) {
ByteBuffer finish = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN);
// for big-endian version, do this first:
// finish.position(8-buf.remaining());
finish.put(buf).rewind();
h ^= finish.getLong();
h *= m;
}
h ^= h >>> r;
h *= m;
h ^= h >>> r;
buf.order(byteOrder);
return h;
}
use of java.nio.ByteOrder in project sis by apache.
the class ChannelImageInputStream method readUTF.
/**
* Reads in a string that has been encoded using a UTF-8 string.
*
* @return the string reads from the stream.
* @throws IOException if an error (including EOF) occurred while reading the stream.
*/
@Override
public final String readUTF() throws IOException {
final ByteOrder oldOrder = buffer.order();
buffer.order(ByteOrder.BIG_ENDIAN);
try {
return DataInputStream.readUTF(this);
} finally {
buffer.order(oldOrder);
}
}
use of java.nio.ByteOrder in project sis by apache.
the class ChannelImageInputStreamTest method testWithRandomData.
/**
* Fills a buffer with random data and compares the result with a standard image input stream.
* We will allocate a small buffer for the {@code ChannelImageInputStream} in order to force
* frequent interactions between the buffer and the channel.
*
* @throws IOException should never happen since we read and write in memory only.
*/
@Test
public void testWithRandomData() throws IOException {
final ByteBuffer buffer = ByteBuffer.allocate(BUFFER_MAX_CAPACITY);
final ByteOrder byteOrder = random.nextBoolean() ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
final byte[] data = createRandomArray(STREAM_LENGTH);
referenceStream = ImageIO.createImageInputStream(new ByteArrayInputStream(data));
referenceStream.setByteOrder(byteOrder);
testedStream = new ChannelImageInputStream("testWithRandomData", Channels.newChannel(new ByteArrayInputStream(data)), buffer, false);
testedStream.setByteOrder(byteOrder);
transferRandomData(testedStream, data.length - ARRAY_MAX_LENGTH, 24);
}
use of java.nio.ByteOrder in project sis by apache.
the class GeoTiffStoreProvider method probeContent.
/**
* Returns {@link ProbeResult#SUPPORTED} if the given storage appears to be supported by {@link GeoTiffStore}.
* Returning {@code SUPPORTED} from this method does not guarantee that reading or writing will succeed,
* only that there appears to be a reasonable chance of success based on a brief inspection of the
* {@linkplain StorageConnector#getStorage() storage object} or contents.
*
* @param connector information about the storage (URL, stream, <i>etc</i>).
* @return {@code SUPPORTED} if the given storage seems to be usable by {@code GeoTiffStore} instances.
* @throws DataStoreException if an I/O error occurred.
*/
@Override
public ProbeResult probeContent(StorageConnector connector) throws DataStoreException {
final ByteBuffer buffer = connector.getStorageAs(ByteBuffer.class);
if (buffer != null) {
if (buffer.remaining() < 2 * Short.BYTES) {
return ProbeResult.INSUFFICIENT_BYTES;
}
final int p = buffer.position();
final short order = buffer.getShort(p);
final boolean isBigEndian = (order == GeoTIFF.BIG_ENDIAN);
if (isBigEndian || order == GeoTIFF.LITTLE_ENDIAN) {
final ByteOrder old = buffer.order();
try {
buffer.order(isBigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);
switch(buffer.getShort(p + Short.BYTES)) {
case GeoTIFF.CLASSIC:
case GeoTIFF.BIG_TIFF:
return new ProbeResult(true, MIME_TYPE, VERSION);
}
} finally {
buffer.order(old);
}
}
}
return ProbeResult.UNSUPPORTED_STORAGE;
}
use of java.nio.ByteOrder in project weiui by kuaifan.
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;
}
Aggregations