use of java.nio.ByteOrder in project jeesuite-libs by vakinge.
the class HashUtils method hash.
/**
* MurMurHash算法,是非加密HASH算法,性能很高,
* 比传统的CRC32,MD5,SHA-1(这两个算法都是加密HASH算法,复杂度本身就很高,带来的性能上的损害也不可避免)
* 等HASH算法要快很多,而且据说这个算法的碰撞率很低. http://murmurhash.googlepages.com/
*/
public static Long hash(String key) {
if (key == null)
return 0L;
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);
finish.put(buf).rewind();
h ^= finish.getLong();
h *= m;
}
h ^= h >>> r;
h *= m;
h ^= h >>> r;
buf.order(byteOrder);
return Math.abs(h);
}
use of java.nio.ByteOrder in project vespa by vespa-engine.
the class DynamicDocumentList method serializeInternal.
private void serializeInternal(DocumentSerializer buf) {
ByteOrder originalOrder = buf.getBuf().order();
buf.getBuf().order(ByteOrder.LITTLE_ENDIAN);
// save the position before the size
int posAtBeginning = buf.getBuf().position();
// write the number of entries
buf.putInt(null, entries.size());
// create a list of metaentries, one for each entry
List<MetaEntry> metaEntries = new ArrayList<MetaEntry>(entries.size());
// jump past the meta block, we will serialize this afterwards when we know sizes and positions
byte[] bogusEntry = new byte[entries.size() * MetaEntry.SIZE];
buf.put(null, bogusEntry);
for (Entry entry : entries) {
MetaEntry metaEntry = new MetaEntry();
metaEntries.add(metaEntry);
// is this a remove? in that case, set this flag
if (entry.isRemoveEntry())
metaEntry.flags |= MetaEntry.REMOVE_ENTRY;
// is the body stripped? in that case, set this flag
if (entry.isBodyStripped())
metaEntry.flags |= MetaEntry.BODY_STRIPPED;
// is this an update? in that case, set this flag
if (entry.getDocumentOperation() instanceof DocumentUpdate)
metaEntry.flags |= MetaEntry.UPDATE_ENTRY;
// is this a document? in that case, try to set the timestamp
if (entry.getDocumentOperation() instanceof DocumentPut) {
Document doc = ((DocumentPut) entry.getDocumentOperation()).getDocument();
Long lastModified = doc.getLastModified();
if (lastModified != null) {
metaEntry.timestamp = lastModified;
}
if (doc.getDataType().getHeaderType().getCompressionConfig() != null && doc.getDataType().getHeaderType().getCompressionConfig().type != CompressionType.NONE) {
metaEntry.flags |= MetaEntry.COMPRESSED;
}
if (doc.getDataType().getBodyType().getCompressionConfig() != null && doc.getDataType().getBodyType().getCompressionConfig().type != CompressionType.NONE) {
metaEntry.flags |= MetaEntry.COMPRESSED;
}
}
metaEntry.headerPos = buf.getBuf().position() - posAtBeginning;
buf.getBuf().order(ByteOrder.BIG_ENDIAN);
if (entry.getDocumentOperation() instanceof DocumentPut) {
Document doc = ((DocumentPut) entry.getDocumentOperation()).getDocument();
// serialize document and save length:
doc.serializeHeader(buf);
} else if (entry.getDocumentOperation() instanceof DocumentUpdate) {
DocumentUpdate docUp = (DocumentUpdate) entry.getDocumentOperation();
docUp.serialize(buf);
} else if (entry.getDocumentOperation() instanceof DocumentRemove) {
new Document(DataType.DOCUMENT, entry.getDocumentOperation().getId()).serialize(buf);
} else {
throw new IllegalArgumentException("Can not handle class " + entry.getDocumentOperation().getClass().getName());
}
metaEntry.headerLen = buf.getBuf().position() - metaEntry.headerPos - posAtBeginning;
if (entry.getDocumentOperation() instanceof DocumentPut) {
metaEntry.bodyPos = buf.getBuf().position() - posAtBeginning;
Document doc = ((DocumentPut) entry.getDocumentOperation()).getDocument();
doc.serializeBody(buf);
metaEntry.bodyLen = buf.getBuf().position() - metaEntry.bodyPos - posAtBeginning;
} else {
metaEntry.bodyPos = 0;
metaEntry.bodyLen = 0;
}
buf.getBuf().order(ByteOrder.LITTLE_ENDIAN);
}
// save position after payload:
int posAfterEntries = buf.getBuf().position();
// go to beginning (after length) to serialize metaentries:
buf.getBuf().position(posAtBeginning + 4);
// serialize metaentries
for (MetaEntry metaEntry : metaEntries) {
metaEntry.serialize(buf.getBuf());
}
// set position to after payload:
buf.getBuf().position(posAfterEntries);
buf.getBuf().order(originalOrder);
}
use of java.nio.ByteOrder in project vespa by vespa-engine.
the class MetaEntry method serialize.
public void serialize(GrowableByteBuffer buf) {
ByteOrder originalOrder = buf.order();
buf.order(ByteOrder.LITTLE_ENDIAN);
// 8
buf.putLong(timestamp);
// 12
buf.putInt(headerPos);
// 16
buf.putInt(headerLen);
// 20
buf.putInt(bodyPos);
// 24
buf.putInt(bodyLen);
// 28 (written as little-endian int, this is on purpose)
buf.putInt(flags);
// 32
buf.putInt(0);
buf.order(originalOrder);
}
use of java.nio.ByteOrder in project Bytecoder by mirkosertic.
the class ImageStream method ensure.
public void ensure(int needs) {
if (needs < 0) {
throw new IndexOutOfBoundsException("Bad value: " + needs);
}
if (needs > buffer.remaining()) {
byte[] bytes = buffer.array();
ByteOrder byteOrder = buffer.order();
int position = buffer.position();
int newSize = needs <= bytes.length ? bytes.length << 1 : position + needs;
buffer = ByteBuffer.allocate(newSize);
buffer.order(byteOrder);
buffer.put(bytes, 0, position);
}
}
use of java.nio.ByteOrder in project Bytecoder by mirkosertic.
the class Trie2 method createFromSerialized.
/**
* Create a Trie2 from its serialized form. Inverse of utrie2_serialize().
*
* Reads from the current position and leaves the buffer after the end of the trie.
*
* The serialized format is identical between ICU4C and ICU4J, so this function
* will work with serialized Trie2s from either.
*
* The actual type of the returned Trie2 will be either Trie2_16 or Trie2_32, depending
* on the width of the data.
*
* To obtain the width of the Trie2, check the actual class type of the returned Trie2.
* Or use the createFromSerialized() function of Trie2_16 or Trie2_32, which will
* return only Tries of their specific type/size.
*
* The serialized Trie2 on the stream may be in either little or big endian byte order.
* This allows using serialized Tries from ICU4C without needing to consider the
* byte order of the system that created them.
*
* @param bytes a byte buffer to the serialized form of a UTrie2.
* @return An unserialized Trie2, ready for use.
* @throws IllegalArgumentException if the stream does not contain a serialized Trie2.
* @throws IOException if a read error occurs in the buffer.
*/
public static Trie2 createFromSerialized(ByteBuffer bytes) throws IOException {
// From ICU4C utrie2_impl.h
// * Trie2 data structure in serialized form:
// *
// * UTrie2Header header;
// * uint16_t index[header.index2Length];
// * uint16_t data[header.shiftedDataLength<<2]; -- or uint32_t data[...]
// * @internal
// */
// typedef struct UTrie2Header {
// /** "Tri2" in big-endian US-ASCII (0x54726932) */
// uint32_t signature;
// /**
// * options bit field:
// * 15.. 4 reserved (0)
// * 3.. 0 UTrie2ValueBits valueBits
// */
// uint16_t options;
//
// /** UTRIE2_INDEX_1_OFFSET..UTRIE2_MAX_INDEX_LENGTH */
// uint16_t indexLength;
//
// /** (UTRIE2_DATA_START_OFFSET..UTRIE2_MAX_DATA_LENGTH)>>UTRIE2_INDEX_SHIFT */
// uint16_t shiftedDataLength;
//
// /** Null index and data blocks, not shifted. */
// uint16_t index2NullOffset, dataNullOffset;
//
// /**
// * First code point of the single-value range ending with U+10ffff,
// * rounded up and then shifted right by UTRIE2_SHIFT_1.
// */
// uint16_t shiftedHighStart;
// } UTrie2Header;
ByteOrder outerByteOrder = bytes.order();
try {
UTrie2Header header = new UTrie2Header();
/* check the signature */
header.signature = bytes.getInt();
switch(header.signature) {
case 0x54726932:
// The buffer is already set to the trie data byte order.
break;
case 0x32697254:
// Temporarily reverse the byte order.
boolean isBigEndian = outerByteOrder == ByteOrder.BIG_ENDIAN;
bytes.order(isBigEndian ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
header.signature = 0x54726932;
break;
default:
throw new IllegalArgumentException("Buffer does not contain a serialized UTrie2");
}
header.options = bytes.getChar();
header.indexLength = bytes.getChar();
header.shiftedDataLength = bytes.getChar();
header.index2NullOffset = bytes.getChar();
header.dataNullOffset = bytes.getChar();
header.shiftedHighStart = bytes.getChar();
if ((header.options & UTRIE2_OPTIONS_VALUE_BITS_MASK) != 0) {
throw new IllegalArgumentException("UTrie2 serialized format error.");
}
Trie2 This;
This = new Trie2_16();
This.header = header;
/* get the length values and offsets */
This.indexLength = header.indexLength;
This.dataLength = header.shiftedDataLength << UTRIE2_INDEX_SHIFT;
This.index2NullOffset = header.index2NullOffset;
This.dataNullOffset = header.dataNullOffset;
This.highStart = header.shiftedHighStart << UTRIE2_SHIFT_1;
This.highValueIndex = This.dataLength - UTRIE2_DATA_GRANULARITY;
This.highValueIndex += This.indexLength;
// Allocate the Trie2 index array. If the data width is 16 bits, the array also
// includes the space for the data.
int indexArraySize = This.indexLength;
indexArraySize += This.dataLength;
This.index = new char[indexArraySize];
/* Read in the index */
int i;
for (i = 0; i < This.indexLength; i++) {
This.index[i] = bytes.getChar();
}
/* Read in the data. 16 bit data goes in the same array as the index.
* 32 bit data goes in its own separate data array.
*/
This.data16 = This.indexLength;
for (i = 0; i < This.dataLength; i++) {
This.index[This.data16 + i] = bytes.getChar();
}
This.data32 = null;
This.initialValue = This.index[This.dataNullOffset];
This.errorValue = This.index[This.data16 + UTRIE2_BAD_UTF8_DATA_OFFSET];
return This;
} finally {
bytes.order(outerByteOrder);
}
}
Aggregations