Search in sources :

Example 91 with ByteOrder

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;
}
Also used : ByteOrder(java.nio.ByteOrder) ByteBuffer(java.nio.ByteBuffer)

Example 92 with ByteOrder

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);
    }
}
Also used : EventAnnouncement(org.apache.flink.runtime.io.network.api.EventAnnouncement) CancelCheckpointMarker(org.apache.flink.runtime.io.network.api.CancelCheckpointMarker) IOException(java.io.IOException) ByteOrder(java.nio.ByteOrder) AbstractEvent(org.apache.flink.runtime.event.AbstractEvent) IOException(java.io.IOException) EndOfData(org.apache.flink.runtime.io.network.api.EndOfData) SubtaskConnectionDescriptor(org.apache.flink.runtime.io.network.api.SubtaskConnectionDescriptor) DataInputDeserializer(org.apache.flink.core.memory.DataInputDeserializer)

Example 93 with ByteOrder

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;
}
Also used : DefaultHttpChunk(org.jboss.netty.handler.codec.http.DefaultHttpChunk) ByteOrder(java.nio.ByteOrder) ByteBuffer(java.nio.ByteBuffer)

Example 94 with ByteOrder

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.
        }
    }
}
Also used : ByteOrder(java.nio.ByteOrder) ByteBuffer(java.nio.ByteBuffer) Test(org.testng.annotations.Test)

Example 95 with ByteOrder

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;
}
Also used : ByteOrder(java.nio.ByteOrder) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteOrder (java.nio.ByteOrder)111 ByteBuffer (java.nio.ByteBuffer)39 IOException (java.io.IOException)8 Test (org.junit.Test)7 QuickTest (com.hazelcast.test.annotation.QuickTest)5 DataInputStream (java.io.DataInputStream)5 FileInputStream (java.io.FileInputStream)5 UUID (java.util.UUID)5 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)4 HKL (ffx.crystal.HKL)3 DataOutputStream (java.io.DataOutputStream)3 EOFException (java.io.EOFException)3 FileOutputStream (java.io.FileOutputStream)3 ShortBuffer (java.nio.ShortBuffer)3 ArrayList (java.util.ArrayList)3 Element (org.jdom.Element)3 InvalidDumpFormatException (com.ibm.j9ddr.corereaders.InvalidDumpFormatException)2 Point (com.revolsys.geometry.model.Point)2 Crystal (ffx.crystal.Crystal)2 BigInteger (java.math.BigInteger)2