use of java.nio.ByteOrder in project new-cloud by xie-summer.
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 flink by apache.
the class EventSerializer method fromSerializedEvent.
public static AbstractEvent fromSerializedEvent(ByteBuffer buffer, ClassLoader classLoader) throws IOException {
if (buffer.remaining() < 4) {
throw new IOException("Incomplete event");
}
final ByteOrder bufferOrder = buffer.order();
buffer.order(ByteOrder.BIG_ENDIAN);
try {
final int type = buffer.getInt();
if (type == END_OF_PARTITION_EVENT) {
return EndOfPartitionEvent.INSTANCE;
} else if (type == CHECKPOINT_BARRIER_EVENT) {
return deserializeCheckpointBarrier(buffer);
} else if (type == END_OF_SUPERSTEP_EVENT) {
return EndOfSuperstepEvent.INSTANCE;
} else if (type == END_OF_CHANNEL_STATE_EVENT) {
return EndOfChannelStateEvent.INSTANCE;
} else if (type == END_OF_USER_RECORDS_EVENT) {
return new EndOfData(StopMode.values()[buffer.get()]);
} else if (type == CANCEL_CHECKPOINT_MARKER_EVENT) {
long id = buffer.getLong();
return new CancelCheckpointMarker(id);
} else if (type == ANNOUNCEMENT_EVENT) {
int sequenceNumber = buffer.getInt();
AbstractEvent announcedEvent = fromSerializedEvent(buffer, classLoader);
return new EventAnnouncement(announcedEvent, sequenceNumber);
} else if (type == VIRTUAL_CHANNEL_SELECTOR_EVENT) {
return new SubtaskConnectionDescriptor(buffer.getInt(), buffer.getInt());
} else if (type == OTHER_EVENT) {
try {
final DataInputDeserializer deserializer = new DataInputDeserializer(buffer);
final String className = deserializer.readUTF();
final Class<? extends AbstractEvent> clazz;
try {
clazz = classLoader.loadClass(className).asSubclass(AbstractEvent.class);
} catch (ClassNotFoundException e) {
throw new IOException("Could not load event class '" + className + "'.", e);
} catch (ClassCastException e) {
throw new IOException("The class '" + className + "' is not a valid subclass of '" + AbstractEvent.class.getName() + "'.", e);
}
final AbstractEvent event = InstantiationUtil.instantiate(clazz, AbstractEvent.class);
event.read(deserializer);
return event;
} catch (Exception e) {
throw new IOException("Error while deserializing or instantiating event.", e);
}
} else {
throw new IOException("Corrupt byte stream for event");
}
} finally {
buffer.order(bufferOrder);
}
}
use of java.nio.ByteOrder in project databus by linkedin.
the class ChunkedBodyWritableByteChannel method write.
@Override
public int write(ByteBuffer buffer) throws IOException {
if (null != _response) {
_response.setHeader(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
_response.setChunked(true);
writeToChannel(_response);
_response = null;
}
// We need to lie to netty that the buffer is BIG_ENDIAN event if it is LITTLE_ENDIAN (see DDS-1212)
ByteOrder bufferOrder = buffer.order();
ByteBuffer realBuffer = bufferOrder == ByteOrder.BIG_ENDIAN ? buffer : buffer.slice().order(ByteOrder.BIG_ENDIAN);
if (null == _chunkReuse) {
_chunkReuse = new DefaultHttpChunk(ChannelBuffers.wrappedBuffer(realBuffer));
} else {
_chunkReuse.setContent(ChannelBuffers.wrappedBuffer(realBuffer));
}
int bytesWritten = realBuffer.remaining();
// We assume this either writes the whole buffer or else throws (no partial writes)
writeToChannel(_chunkReuse);
// DDSDBUS-1363: WritableByteChannel.write() contract requires both (1) return of bytes written and (2) update of
// position. Without the latter, NIO loops forever.
realBuffer.position(realBuffer.limit());
// DDS-1212
if (bufferOrder == ByteOrder.LITTLE_ENDIAN)
buffer.position(realBuffer.position());
return bytesWritten;
}
use of java.nio.ByteOrder in project databus by linkedin.
the class TestDbusEventV2 method testEopEvent.
@Test
public void testEopEvent() throws Exception {
long seq = 9832465L;
short partitionId = 33;
ByteOrder[] byteOrders = { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN };
int[] startPositions = { 0, 23 };
long now = TimeUtils.currentNanoTime();
for (ByteOrder byteOrder : byteOrders) {
DbusEventFactory eventFactory = new DbusEventV2Factory(byteOrder);
for (int startPos : startPositions) {
LOG.info(("Trying byteOrder " + byteOrder + ", startPosition " + startPos));
ByteBuffer bb = ByteBuffer.allocate(1000).order(byteOrder);
for (byte i = 0; i < startPos; i++) {
bb.put(i);
}
DbusEventInfo eventInfo = new DbusEventInfo(null, seq, partitionId, partitionId, now, DbusEventInternalWritable.EOPMarkerSrcId, DbusEventInternalWritable.emptyMd5, DbusEventInternalWritable.EOPMarkerValue, // enable tracing
false, // autocommit
true, DbusEventFactory.DBUS_EVENT_V2, // payload schema version, 0 since there is no payload
(short) 0, // DbusEventPart for metadataBytes
null);
final int evtLen = eventFactory.serializeLongKeyEndOfPeriodMarker(bb, eventInfo);
DbusEvent e = eventFactory.createReadOnlyDbusEventFromBuffer(bb, startPos);
Assert.assertTrue(e.isEndOfPeriodMarker());
Assert.assertNull(e.getOpcode());
Assert.assertEquals(seq, e.sequence());
Assert.assertEquals(partitionId, e.getPartitionId());
Assert.assertTrue(now <= e.timestampInNanos());
Assert.assertNull(e.getPayloadPart());
Assert.assertEquals(evtLen, e.size());
Assert.assertEquals(DbusEventFactory.computeEventLength(DbusEventInternalWritable.EOPMarkerKey, eventInfo), evtLen, "Mismatch between computed length and serialized length");
// TODO Check the ext repl bit after DDSDBUS-2296 is fixed.
}
}
}
use of java.nio.ByteOrder in project jedis by xetorthio.
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;
}
Aggregations