use of java.nio.ByteOrder in project cachecloud by sohutv.
the class MurmurHash method hash64A.
public static long hash64A(ByteBuffer buf, int seed) {
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 hs4j by killme2008.
the class AbstractIoBuffer method compact.
/**
* {@inheritDoc}
*/
@Override
public final IoBuffer compact() {
int remaining = remaining();
int capacity = capacity();
if (capacity == 0) {
return this;
}
if (isAutoShrink() && remaining <= capacity >>> 2 && capacity > minimumCapacity) {
int newCapacity = capacity;
int minCapacity = Math.max(minimumCapacity, remaining << 1);
for (; ; ) {
if (newCapacity >>> 1 < minCapacity) {
break;
}
newCapacity >>>= 1;
}
newCapacity = Math.max(minCapacity, newCapacity);
if (newCapacity == capacity) {
return this;
}
// Shrink and compact:
// // Save the state.
ByteOrder bo = order();
// // Sanity check.
if (remaining > newCapacity) {
throw new IllegalStateException("The amount of the remaining bytes is greater than " + "the new capacity.");
}
// // Reallocate.
ByteBuffer oldBuf = buf();
ByteBuffer newBuf = getAllocator().allocateNioBuffer(newCapacity, isDirect());
newBuf.put(oldBuf);
buf(newBuf);
// // Restore the state.
buf().order(bo);
} else {
buf().compact();
}
mark = -1;
return this;
}
use of java.nio.ByteOrder in project hs4j by killme2008.
the class AbstractIoBuffer method shrink.
/**
* {@inheritDoc}
*/
@Override
public final IoBuffer shrink() {
if (!recapacityAllowed) {
throw new IllegalStateException("Derived buffers and their parent can't be expanded.");
}
int position = position();
int capacity = capacity();
int limit = limit();
if (capacity == limit) {
return this;
}
int newCapacity = capacity;
int minCapacity = Math.max(minimumCapacity, limit);
for (; ; ) {
if (newCapacity >>> 1 < minCapacity) {
break;
}
newCapacity >>>= 1;
}
newCapacity = Math.max(minCapacity, newCapacity);
if (newCapacity == capacity) {
return this;
}
// Shrink and compact:
// // Save the state.
ByteOrder bo = order();
// // Reallocate.
ByteBuffer oldBuf = buf();
ByteBuffer newBuf = getAllocator().allocateNioBuffer(newCapacity, isDirect());
oldBuf.position(0);
oldBuf.limit(limit);
newBuf.put(oldBuf);
buf(newBuf);
// // Restore the state.
buf().position(position);
buf().limit(limit);
buf().order(bo);
mark = -1;
return this;
}
use of java.nio.ByteOrder in project Mycat-Server by MyCATApache.
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 bytecode-viewer by Konloch.
the class ValuesPanel method doubleTextFieldKeyReleased.
// GEN-LAST:event_floatTextFieldKeyReleased
private void doubleTextFieldKeyReleased(java.awt.event.KeyEvent evt) {
// GEN-FIRST:event_doubleTextFieldKeyReleased
if (evt.getKeyCode() == KeyEvent.VK_ENTER && isEditable()) {
try {
ByteOrder byteOrder = getByteOrder();
double doubleValue = Double.parseDouble(doubleTextField.getText());
byteBuffer.rewind();
if (byteBuffer.order() != byteOrder) {
byteBuffer.order(byteOrder);
}
byteBuffer.putDouble(doubleValue);
modifyValues(8);
updateValues();
} catch (NumberFormatException ex) {
showException(ex);
}
}
}
Aggregations