java.nio.ByteBuffer

Here are the examples of the java api class java.nio.ByteBuffer taken from open source projects.

1. ClusterMetricsSnapshot#serialize()

Project: ignite
Source File: ClusterMetricsSnapshot.java
View license
/**
     * Serializes node metrics into byte array.
     *
     * @param data Byte array.
     * @param off Offset into byte array.
     * @param metrics Node metrics to serialize.
     * @return New offset.
     */
public static int serialize(byte[] data, int off, ClusterMetrics metrics) {
    ByteBuffer buf = ByteBuffer.wrap(data, off, METRICS_SIZE);
    buf.putInt(metrics.getMaximumActiveJobs());
    buf.putInt(metrics.getCurrentActiveJobs());
    buf.putFloat(metrics.getAverageActiveJobs());
    buf.putInt(metrics.getMaximumWaitingJobs());
    buf.putInt(metrics.getCurrentWaitingJobs());
    buf.putFloat(metrics.getAverageWaitingJobs());
    buf.putInt(metrics.getMaximumRejectedJobs());
    buf.putInt(metrics.getCurrentRejectedJobs());
    buf.putFloat(metrics.getAverageRejectedJobs());
    buf.putInt(metrics.getMaximumCancelledJobs());
    buf.putInt(metrics.getCurrentCancelledJobs());
    buf.putFloat(metrics.getAverageCancelledJobs());
    buf.putInt(metrics.getTotalRejectedJobs());
    buf.putInt(metrics.getTotalCancelledJobs());
    buf.putInt(metrics.getTotalExecutedJobs());
    buf.putLong(metrics.getMaximumJobWaitTime());
    buf.putLong(metrics.getCurrentJobWaitTime());
    buf.putDouble(metrics.getAverageJobWaitTime());
    buf.putLong(metrics.getMaximumJobExecuteTime());
    buf.putLong(metrics.getCurrentJobExecuteTime());
    buf.putDouble(metrics.getAverageJobExecuteTime());
    buf.putInt(metrics.getTotalExecutedTasks());
    buf.putLong(metrics.getCurrentIdleTime());
    buf.putLong(metrics.getTotalIdleTime());
    buf.putInt(metrics.getTotalCpus());
    buf.putDouble(metrics.getCurrentCpuLoad());
    buf.putDouble(metrics.getAverageCpuLoad());
    buf.putDouble(metrics.getCurrentGcCpuLoad());
    buf.putLong(metrics.getHeapMemoryInitialized());
    buf.putLong(metrics.getHeapMemoryUsed());
    buf.putLong(metrics.getHeapMemoryCommitted());
    buf.putLong(metrics.getHeapMemoryMaximum());
    buf.putLong(metrics.getHeapMemoryTotal());
    buf.putLong(metrics.getNonHeapMemoryInitialized());
    buf.putLong(metrics.getNonHeapMemoryUsed());
    buf.putLong(metrics.getNonHeapMemoryCommitted());
    buf.putLong(metrics.getNonHeapMemoryMaximum());
    buf.putLong(metrics.getNonHeapMemoryTotal());
    buf.putLong(metrics.getStartTime());
    buf.putLong(metrics.getNodeStartTime());
    buf.putLong(metrics.getUpTime());
    buf.putInt(metrics.getCurrentThreadCount());
    buf.putInt(metrics.getMaximumThreadCount());
    buf.putLong(metrics.getTotalStartedThreadCount());
    buf.putInt(metrics.getCurrentDaemonThreadCount());
    buf.putLong(metrics.getLastDataVersion());
    buf.putInt(metrics.getSentMessagesCount());
    buf.putLong(metrics.getSentBytesCount());
    buf.putInt(metrics.getReceivedMessagesCount());
    buf.putLong(metrics.getReceivedBytesCount());
    buf.putInt(metrics.getOutboundMessagesQueueSize());
    buf.putInt(metrics.getTotalNodes());
    assert !buf.hasRemaining() : "Invalid metrics size [expected=" + METRICS_SIZE + ", actual=" + (buf.position() - off) + ']';
    return buf.position();
}

2. PebbleProtocol#encodeActionResponse2x()

Project: Gadgetbridge
Source File: PebbleProtocol.java
View license
public byte[] encodeActionResponse2x(int id, byte actionId, int iconId, String caption) {
    short length = (short) (18 + caption.getBytes().length);
    ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + length);
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putShort(length);
    buf.putShort(ENDPOINT_EXTENSIBLENOTIFS);
    buf.order(ByteOrder.LITTLE_ENDIAN);
    buf.put(NOTIFICATIONACTION_RESPONSE);
    buf.putInt(id);
    buf.put(actionId);
    buf.put(NOTIFICATIONACTION_ACK);
    //nr of attributes
    buf.put((byte) 2);
    // icon
    buf.put((byte) 6);
    // length
    buf.putShort((short) 4);
    buf.putInt(iconId);
    // title
    buf.put((byte) 2);
    buf.putShort((short) caption.getBytes().length);
    buf.put(caption.getBytes());
    return buf.array();
}

3. PebbleProtocol#encodeActionResponse()

Project: Gadgetbridge
Source File: PebbleProtocol.java
View license
public byte[] encodeActionResponse(UUID uuid, int iconId, String caption) {
    short length = (short) (29 + caption.getBytes().length);
    ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + length);
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putShort(length);
    buf.putShort(ENDPOINT_NOTIFICATIONACTION);
    buf.put(NOTIFICATIONACTION_RESPONSE);
    buf.putLong(uuid.getMostSignificantBits());
    buf.putLong(uuid.getLeastSignificantBits());
    buf.order(ByteOrder.LITTLE_ENDIAN);
    buf.put(NOTIFICATIONACTION_ACK);
    //nr of attributes
    buf.put((byte) 2);
    // icon
    buf.put((byte) 6);
    // length
    buf.putShort((short) 4);
    buf.putInt(0x80000000 | iconId);
    // title
    buf.put((byte) 2);
    buf.putShort((short) caption.getBytes().length);
    buf.put(caption.getBytes());
    return buf.array();
}

4. TestImportExport#testImportExportWkbGeometryCollection()

View license
@Test
public static void testImportExportWkbGeometryCollection() {
    OperatorImportFromWkb importerWKB = (OperatorImportFromWkb) OperatorFactoryLocal.getInstance().getOperator(Operator.Type.ImportFromWkb);
    int offset = 0;
    ByteBuffer wkbBuffer = ByteBuffer.allocate(600).order(ByteOrder.nativeOrder());
    wkbBuffer.put(offset, (byte) WkbByteOrder.wkbNDR);
    // byte order
    offset += 1;
    wkbBuffer.putInt(offset, WkbGeometryType.wkbGeometryCollection);
    // type
    offset += 4;
    // 3 geometries
    wkbBuffer.putInt(offset, 3);
    offset += 4;
    wkbBuffer.put(offset, (byte) WkbByteOrder.wkbNDR);
    offset += 1;
    wkbBuffer.putInt(offset, WkbGeometryType.wkbPoint);
    offset += 4;
    wkbBuffer.putDouble(offset, 0);
    offset += 8;
    wkbBuffer.putDouble(offset, 0);
    offset += 8;
    wkbBuffer.put(offset, (byte) WkbByteOrder.wkbNDR);
    // byte order
    offset += 1;
    wkbBuffer.putInt(offset, WkbGeometryType.wkbGeometryCollection);
    // type
    offset += 4;
    // 7 empty geometries
    wkbBuffer.putInt(offset, 7);
    offset += 4;
    wkbBuffer.put(offset, (byte) WkbByteOrder.wkbNDR);
    offset += 1;
    wkbBuffer.putInt(offset, WkbGeometryType.wkbLineString);
    offset += 4;
    // 0 points, for empty linestring
    wkbBuffer.putInt(offset, 0);
    offset += 4;
    wkbBuffer.put(offset, (byte) WkbByteOrder.wkbNDR);
    offset += 1;
    wkbBuffer.putInt(offset, WkbGeometryType.wkbPolygon);
    offset += 4;
    // 0 points, for empty polygon
    wkbBuffer.putInt(offset, 0);
    offset += 4;
    wkbBuffer.put(offset, (byte) WkbByteOrder.wkbNDR);
    offset += 1;
    wkbBuffer.putInt(offset, WkbGeometryType.wkbMultiPolygon);
    offset += 4;
    // 0 points, for empty multipolygon
    wkbBuffer.putInt(offset, 0);
    offset += 4;
    wkbBuffer.put(offset, (byte) WkbByteOrder.wkbNDR);
    offset += 1;
    wkbBuffer.putInt(offset, WkbGeometryType.wkbMultiLineString);
    offset += 4;
    // 0 points, for empty multilinestring
    wkbBuffer.putInt(offset, 0);
    offset += 4;
    wkbBuffer.put(offset, (byte) WkbByteOrder.wkbNDR);
    offset += 1;
    wkbBuffer.putInt(offset, WkbGeometryType.wkbGeometryCollection);
    offset += 4;
    // 0 geometries, for empty
    wkbBuffer.putInt(offset, 0);
    // geometrycollection
    offset += 4;
    wkbBuffer.put(offset, (byte) WkbByteOrder.wkbNDR);
    offset += 1;
    wkbBuffer.putInt(offset, WkbGeometryType.wkbMultiPoint);
    offset += 4;
    // 0 points, for empty multipoint
    wkbBuffer.putInt(offset, 0);
    offset += 4;
    wkbBuffer.put(offset, (byte) WkbByteOrder.wkbNDR);
    offset += 1;
    wkbBuffer.putInt(offset, WkbGeometryType.wkbPoint);
    offset += 4;
    wkbBuffer.putDouble(offset, 66);
    offset += 8;
    wkbBuffer.putDouble(offset, 88);
    offset += 8;
    wkbBuffer.put(offset, (byte) WkbByteOrder.wkbNDR);
    offset += 1;
    wkbBuffer.putInt(offset, WkbGeometryType.wkbPoint);
    offset += 4;
    wkbBuffer.putDouble(offset, 13);
    offset += 8;
    wkbBuffer.putDouble(offset, 17);
    offset += 8;
    // "GeometryCollection( Point (0 0),  GeometryCollection( LineString empty, Polygon empty, MultiPolygon empty, MultiLineString empty, MultiPoint empty ), Point (13 17) )";
    OGCStructure structure = importerWKB.executeOGC(0, wkbBuffer, null).m_structures.get(0);
    assertTrue(structure.m_type == 7);
    assertTrue(structure.m_structures.get(0).m_type == 1);
    assertTrue(structure.m_structures.get(1).m_type == 7);
    assertTrue(structure.m_structures.get(2).m_type == 1);
    assertTrue(structure.m_structures.get(1).m_structures.get(0).m_type == 2);
    assertTrue(structure.m_structures.get(1).m_structures.get(1).m_type == 3);
    assertTrue(structure.m_structures.get(1).m_structures.get(2).m_type == 6);
    assertTrue(structure.m_structures.get(1).m_structures.get(3).m_type == 5);
    assertTrue(structure.m_structures.get(1).m_structures.get(4).m_type == 7);
    assertTrue(structure.m_structures.get(1).m_structures.get(5).m_type == 4);
    assertTrue(structure.m_structures.get(1).m_structures.get(6).m_type == 1);
    Point p = (Point) structure.m_structures.get(1).m_structures.get(6).m_geometry;
    assertTrue(p.getX() == 66);
    assertTrue(p.getY() == 88);
    p = (Point) structure.m_structures.get(2).m_geometry;
    assertTrue(p.getX() == 13);
    assertTrue(p.getY() == 17);
}

5. TestClientPortChannel#runBadLoginMessages()

Project: voltdb
Source File: TestClientPortChannel.java
View license
public void runBadLoginMessages(int port) throws Exception {
    //Just connect and disconnect
    PortConnector channel = new PortConnector("localhost", port);
    System.out.println("Testing Connect and Close");
    for (int i = 0; i < 100; i++) {
        channel.connect();
        channel.close();
    }
    //Bad +ve length
    System.out.println("Testing bad login message");
    channel.connect();
    ByteBuffer buf = ByteBuffer.allocate(Integer.SIZE);
    buf.putInt(10);
    buf.position(0);
    channel.write(buf);
    channel.close();
    //Bad -ve length.
    System.out.println("Testing negative length of message");
    channel.connect();
    buf = ByteBuffer.allocate(Integer.SIZE);
    buf.putInt(-1);
    buf.position(0);
    channel.write(buf);
    channel.close();
    //Bad 0 length.
    System.out.println("Testing zero length of message");
    channel.connect();
    buf = ByteBuffer.allocate(Integer.SIZE);
    buf.putInt(0);
    buf.position(0);
    channel.write(buf);
    channel.close();
    //too big length
    System.out.println("Testing too big length of message");
    channel.connect();
    buf = ByteBuffer.allocate(Integer.SIZE);
    buf.putInt(Integer.MAX_VALUE);
    buf.position(0);
    channel.write(buf);
    channel.close();
    //login message with bad version
    System.out.println("Testing bad service name");
    channel.connect();
    buf = ByteBuffer.allocate(42);
    buf.putInt(38);
    buf.put((byte) '0');
    buf.put((byte) ClientAuthScheme.HASH_SHA1.getValue());
    buf.putInt(8);
    buf.put("dataCase".getBytes("UTF-8"));
    buf.putInt(0);
    buf.put("".getBytes("UTF-8"));
    buf.put(ConnectionUtil.getHashedPassword(ClientAuthScheme.HASH_SHA1, ""));
    buf.flip();
    channel.write(buf);
    //Now this will fail because bad version will be read.
    try {
        ByteBuffer resp = ByteBuffer.allocate(6);
        channel.read(resp, 6);
        resp.flip();
        resp.getInt();
        resp.get();
        byte code = resp.get();
        assertEquals(5, code);
    } catch (Exception ioex) {
        fail();
    }
    //login message with bad version
    System.out.println("Testing bad scheme name");
    channel.connect();
    buf = ByteBuffer.allocate(42);
    buf.putInt(38);
    buf.put((byte) 1);
    buf.put((byte) 3);
    buf.putInt(8);
    buf.put("database".getBytes("UTF-8"));
    buf.putInt(0);
    buf.put("".getBytes("UTF-8"));
    buf.put(ConnectionUtil.getHashedPassword(ClientAuthScheme.HASH_SHA1, ""));
    buf.flip();
    channel.write(buf);
    //Now this will fail because bad version will be read.
    try {
        ByteBuffer resp = ByteBuffer.allocate(6);
        channel.read(resp, 6);
        resp.flip();
        resp.getInt();
        resp.get();
        byte code = resp.get();
        assertEquals(3, code);
    } catch (Exception ioex) {
        fail();
    }
    //login message with bad service name length.
    System.out.println("Testing service name with invalid length");
    channel.connect();
    buf = ByteBuffer.allocate(42);
    buf.putInt(38);
    buf.put((byte) '0');
    buf.put((byte) ClientAuthScheme.HASH_SHA1.getValue());
    buf.putInt(Integer.MAX_VALUE);
    buf.put("database".getBytes("UTF-8"));
    buf.putInt(0);
    buf.put("".getBytes("UTF-8"));
    buf.put(ConnectionUtil.getHashedPassword(ClientAuthScheme.HASH_SHA1, ""));
    buf.flip();
    channel.write(buf);
    boolean mustfail = false;
    try {
        ByteBuffer resp = ByteBuffer.allocate(6);
        channel.read(resp, 6);
        resp.flip();
    } catch (Exception ioex) {
        mustfail = true;
    }
    assertTrue(mustfail);
    channel.close();
    //Make sure server is up and we can login/connect after all the beating it took.
    doLoginAndClose(port);
    doLoginSha2AndClose(port);
}

6. ZipCombinerTest#zipWithPartialEntry()

Project: bazel
Source File: ZipCombinerTest.java
View license
// Create a ZIP with a partial entry.
private InputStream zipWithPartialEntry() {
    ByteBuffer out = ByteBuffer.wrap(new byte[32]).order(ByteOrder.LITTLE_ENDIAN);
    out.clear();
    // file header
    // file header signature
    out.putInt(LOCAL_FILE_HEADER_MARKER);
    // version to extract
    out.putShort((short) 6);
    // general purpose bit flag
    out.putShort((short) 0);
    // compression method
    out.putShort((short) ZipOutputStream.STORED);
    // mtime (00:00:00)
    out.putShort((short) 0);
    // mdate (1.1.1980)
    out.putShort((short) 0x21);
    // crc32
    out.putInt(0);
    // compressed size
    out.putInt(10);
    // uncompressed size
    out.putInt(10);
    // file name length
    out.putShort((short) 1);
    // extra field length
    out.putShort((short) 0);
    // file name
    out.put((byte) 'a');
    // file contents
    out.put((byte) 0x01);
    return new ByteArrayInputStream(out.array());
}

7. PebbleProtocol#encodePhoneVersion3x()

Project: Gadgetbridge
Source File: PebbleProtocol.java
View license
private byte[] encodePhoneVersion3x(byte os) {
    final short LENGTH_PHONEVERSION3X = 25;
    ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + LENGTH_PHONEVERSION3X);
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putShort(LENGTH_PHONEVERSION3X);
    buf.putShort(ENDPOINT_PHONEVERSION);
    buf.put((byte) 0x01);
    //0xffffffff
    buf.putInt(-1);
    buf.putInt(0);
    buf.putInt(os);
    buf.put(PHONEVERSION_APPVERSION_MAGIC);
    // major
    buf.put((byte) 3);
    // minor
    buf.put((byte) 12);
    // patch
    buf.put((byte) 0);
    buf.order(ByteOrder.LITTLE_ENDIAN);
    //flags
    buf.putLong(0x00000000000001af);
    return buf.array();
}

8. QueryRegenerateEvent#getShortQuery()

Project: Nukkit
Source File: QueryRegenerateEvent.java
View license
public byte[] getShortQuery() {
    ByteBuffer query = ByteBuffer.allocate(65536);
    query.put(this.serverName.getBytes(StandardCharsets.UTF_8));
    query.put((byte) 0x00);
    query.put(this.gameType.getBytes(StandardCharsets.UTF_8));
    query.put((byte) 0x00);
    query.put(this.map.getBytes(StandardCharsets.UTF_8));
    query.put((byte) 0x00);
    query.put(String.valueOf(this.numPlayers).getBytes(StandardCharsets.UTF_8));
    query.put((byte) 0x00);
    query.put(String.valueOf(this.maxPlayers).getBytes(StandardCharsets.UTF_8));
    query.put((byte) 0x00);
    query.put(Binary.writeLShort(this.port));
    query.put(this.ip.getBytes(StandardCharsets.UTF_8));
    query.put((byte) 0x00);
    return Arrays.copyOf(query.array(), query.position());
}

9. BinaryEncDecTest#testUserClientSync()

Project: kaa
Source File: BinaryEncDecTest.java
View license
@Test
public void testUserClientSync() throws PlatformEncDecException {
    ByteBuffer buf = ByteBuffer.wrap(new byte[4 + 4 + 4 + 8 + 8 + 4 + 4 + 8 + 4 + 4 + SHA_1_LENGTH]);
    // user assign request
    buf.put((byte) 0);
    buf.put((byte) 4);
    buf.put((byte) 0);
    buf.put((byte) 5);
    buf.put((byte) 0);
    buf.put((byte) 8);
    buf.put((byte) 0);
    buf.put((byte) 0);
    buf.put("user".getBytes(Charset.forName("UTF-8")));
    buf.put("token".getBytes(Charset.forName("UTF-8")));
    buf.put((byte) 0);
    buf.put((byte) 0);
    buf.put((byte) 0);
    buf.put("verifier".getBytes(Charset.forName("UTF-8")));
    // attach requests
    buf.put((byte) 1);
    buf.put((byte) 0);
    buf.put((byte) 0);
    buf.put((byte) 1);
    buf.putShort(BIG_MAGIC_NUMBER);
    buf.put((byte) 0);
    buf.put((byte) 6);
    buf.put("token2".getBytes(Charset.forName("UTF-8")));
    buf.put((byte) 0);
    buf.put((byte) 0);
    // detach requests
    buf.put((byte) 2);
    buf.put((byte) 0);
    buf.put((byte) 0);
    buf.put((byte) 1);
    buf.putShort((short) (BIG_MAGIC_NUMBER + 1));
    buf.put((byte) 0);
    buf.put((byte) 0);
    byte[] keyHash = new byte[SHA_1_LENGTH];
    keyHash[MAGIC_INDEX] = MAGIC_NUMBER;
    buf.put(keyHash);
    ClientSync sync = encDec.decode(concat(buildHeader(Constants.KAA_PLATFORM_PROTOCOL_BINARY_ID, 1, 2), getValidMetaData(), buildExtensionHeader(BinaryEncDec.USER_EXTENSION_ID, 0, 0, buf.array().length), buf.array()));
    Assert.assertNotNull(sync);
    Assert.assertNotNull(sync.getClientSyncMetaData());
    Assert.assertNotNull(sync.getUserSync());
    UserClientSync uSync = sync.getUserSync();
    Assert.assertNotNull(uSync.getUserAttachRequest());
    Assert.assertEquals("user", uSync.getUserAttachRequest().getUserExternalId());
    Assert.assertEquals("token", uSync.getUserAttachRequest().getUserAccessToken());
    Assert.assertNotNull(uSync.getEndpointAttachRequests());
    Assert.assertEquals(1, uSync.getEndpointAttachRequests().size());
    Assert.assertEquals(BIG_MAGIC_NUMBER, uSync.getEndpointAttachRequests().get(0).getRequestId());
    Assert.assertEquals("token2", uSync.getEndpointAttachRequests().get(0).getEndpointAccessToken());
    Assert.assertNotNull(uSync.getEndpointDetachRequests());
    Assert.assertEquals(1, uSync.getEndpointDetachRequests().size());
    Assert.assertEquals(BIG_MAGIC_NUMBER + 1, uSync.getEndpointDetachRequests().get(0).getRequestId());
    Assert.assertEquals(Base64Util.encode(keyHash), uSync.getEndpointDetachRequests().get(0).getEndpointKeyHash());
}

10. ProxyLoginHandler#createSocks4aMessage()

View license
private ByteBuffer[] createSocks4aMessage() {
    //TODO convert to direct?
    ByteBuffer handshake = ByteBuffer.allocate(256 + mapped_ip.length());
    // socks 4(a)
    handshake.put((byte) 4);
    // command = CONNECT
    handshake.put((byte) 1);
    // port
    handshake.putShort((short) remote_address.getPort());
    handshake.put((byte) 0);
    handshake.put((byte) 0);
    handshake.put((byte) 0);
    // indicates socks 4a
    handshake.put((byte) 1);
    if (socks_user.length() > 0) {
        handshake.put(socks_user.getBytes());
    }
    handshake.put((byte) 0);
    handshake.put(mapped_ip.getBytes());
    handshake.put((byte) 0);
    handshake.flip();
    //TODO convert to direct?
    return new ByteBuffer[] { handshake, ByteBuffer.allocate(8) };
}

11. ConditionalReplaceOperation#encode()

View license
@Override
public ByteBuffer encode(final Serializer<K> keySerializer, final Serializer<V> valueSerializer) {
    ByteBuffer keyBuf = keySerializer.serialize(key);
    ByteBuffer oldValueBuf = valueSerializer.serialize(oldValue);
    ByteBuffer valueBuf = valueSerializer.serialize(newValue);
    ByteBuffer buffer = ByteBuffer.allocate(// Operation type
    BYTE_SIZE_BYTES + // Size of the key payload
    INT_SIZE_BYTES + // Size of expiration time stamp
    LONG_SIZE_BYTES + // the key payload itself
    keyBuf.remaining() + // Size of the old value payload
    INT_SIZE_BYTES + // The old value payload itself
    oldValueBuf.remaining() + // The value payload itself
    valueBuf.remaining());
    buffer.put(getOpCode().getValue());
    buffer.putLong(this.timeStamp);
    buffer.putInt(keyBuf.remaining());
    buffer.put(keyBuf);
    buffer.putInt(oldValueBuf.remaining());
    buffer.put(oldValueBuf);
    buffer.put(valueBuf);
    buffer.flip();
    return buffer;
}

12. PebbleProtocol#encodeInstallMetadata()

Project: Gadgetbridge
Source File: PebbleProtocol.java
View license
public byte[] encodeInstallMetadata(UUID uuid, String appName, short appVersion, short sdkVersion, int flags, int iconId) {
    final short METADATA_LENGTH = 126;
    byte[] name_buf = new byte[96];
    System.arraycopy(appName.getBytes(), 0, name_buf, 0, appName.getBytes().length);
    ByteBuffer buf = ByteBuffer.allocate(METADATA_LENGTH);
    buf.order(ByteOrder.BIG_ENDIAN);
    // watchapp uuid
    buf.putLong(uuid.getMostSignificantBits());
    buf.putLong(uuid.getLeastSignificantBits());
    buf.order(ByteOrder.LITTLE_ENDIAN);
    buf.putInt(flags);
    buf.putInt(iconId);
    buf.putShort(appVersion);
    buf.putShort(sdkVersion);
    // app_face_bgcolor
    buf.put((byte) 0);
    // app_face_template_id
    buf.put((byte) 0);
    // 96 bytes
    buf.put(name_buf);
    return encodeBlobdb(uuid, BLOBDB_INSERT, BLOBDB_APP, buf.array());
}

13. PebbleProtocol#encodePhoneVersion2x()

Project: Gadgetbridge
Source File: PebbleProtocol.java
View license
private byte[] encodePhoneVersion2x(byte os) {
    ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + LENGTH_PHONEVERSION);
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putShort(LENGTH_PHONEVERSION);
    buf.putShort(ENDPOINT_PHONEVERSION);
    buf.put((byte) 0x01);
    //0xffffffff
    buf.putInt(-1);
    if (os == PHONEVERSION_REMOTE_OS_ANDROID) {
        buf.putInt(PHONEVERSION_SESSION_CAPS_GAMMARAY);
    } else {
        buf.putInt(0);
    }
    buf.putInt(PHONEVERSION_REMOTE_CAPS_SMS | PHONEVERSION_REMOTE_CAPS_TELEPHONY | os);
    buf.put(PHONEVERSION_APPVERSION_MAGIC);
    buf.put(PHONEVERSION_APPVERSION_MAJOR);
    buf.put(PHONEVERSION_APPVERSION_MINOR);
    buf.put(PHONEVERSION_APPVERSION_PATCH);
    return buf.array();
}

14. IPv4Test#setUp()

Project: onos
Source File: IPv4Test.java
View license
@Before
public void setUp() throws Exception {
    deserializer = IPv4.deserializer();
    ByteBuffer bb = ByteBuffer.allocate(headerLength * 4);
    bb.put((byte) ((version & 0xf) << 4 | headerLength & 0xf));
    bb.put(diffServ);
    bb.putShort(totalLength);
    bb.putShort(identification);
    bb.putShort((short) ((flags & 0x7) << 13 | fragmentOffset & 0x1fff));
    bb.put(ttl);
    bb.put(protocol);
    bb.putShort(checksum);
    bb.putInt(sourceAddress);
    bb.putInt(destinationAddress);
    bb.put(options);
    headerBytes = bb.array();
}

15. BaseBinaryCommandUnitTest#constructResponse()

View license
public ByteBuffer constructResponse(byte opCode, short keyLength, byte extraLength, byte dataType, short status, int totalBodyLength, int opaque, long cas, byte[] extras, byte[] keyBytes, byte[] valueBytes) {
    ByteBuffer result = ByteBuffer.allocate(24 + totalBodyLength);
    result.put((byte) 0x81);
    result.put(opCode);
    result.putShort(keyLength);
    result.put(extraLength);
    result.put(dataType);
    result.putShort(status);
    result.putInt(totalBodyLength);
    result.putInt(opaque);
    result.putLong(cas);
    if (extras != null)
        result.put(extras);
    if (keyBytes != null)
        result.put(keyBytes);
    if (valueBytes != null)
        result.put(valueBytes);
    result.flip();
    return result;
}

16. ARP#serialize()

Project: onos
Source File: ARP.java
View license
@Override
public byte[] serialize() {
    final int length = 8 + 2 * (0xff & this.hardwareAddressLength) + 2 * (0xff & this.protocolAddressLength);
    final byte[] data = new byte[length];
    final ByteBuffer bb = ByteBuffer.wrap(data);
    bb.putShort(this.hardwareType);
    bb.putShort(this.protocolType);
    bb.put(this.hardwareAddressLength);
    bb.put(this.protocolAddressLength);
    bb.putShort(this.opCode);
    bb.put(this.senderHardwareAddress, 0, 0xff & this.hardwareAddressLength);
    bb.put(this.senderProtocolAddress, 0, 0xff & this.protocolAddressLength);
    bb.put(this.targetHardwareAddress, 0, 0xff & this.hardwareAddressLength);
    bb.put(this.targetProtocolAddress, 0, 0xff & this.protocolAddressLength);
    return data;
}

17. ArpTest#setUp()

Project: onos
Source File: ArpTest.java
View license
@Before
public void setUp() {
    ByteBuffer bb = ByteBuffer.allocate(ARP.INITIAL_HEADER_LENGTH + 2 * hwAddressLength + 2 * protoAddressLength);
    bb.putShort(ARP.HW_TYPE_ETHERNET);
    bb.putShort(ARP.PROTO_TYPE_IP);
    bb.put(hwAddressLength);
    bb.put(protoAddressLength);
    bb.putShort(ARP.OP_REPLY);
    bb.put(srcMac.toBytes());
    bb.put(srcIp.toOctets());
    bb.put(targetMac.toBytes());
    bb.put(targetIp.toOctets());
    byteHeader = bb.array();
}

18. IntroductionProcessor#createIntroductionBuffer()

Project: Orchid
Source File: IntroductionProcessor.java
View license
private ByteBuffer createIntroductionBuffer(int timestamp, Router rr, byte[] cookie, byte[] dhPublic) {
    final ByteBuffer buffer = ByteBuffer.allocate(Cell.CELL_LEN);
    final byte[] rpAddress = rr.getAddress().getAddressDataBytes();
    final short rpPort = (short) rr.getOnionPort();
    final byte[] rpIdentity = rr.getIdentityHash().getRawBytes();
    final byte[] rpOnionKey = rr.getOnionKey().getRawBytes();
    // VER    Version byte: set to 3.        [1 octet]
    buffer.put((byte) INTRODUCTION_PROTOCOL_VERSION);
    addAuthentication(buffer);
    //buffer.put((byte) 0);                              // AUTHT  The auth type that is used     [1 octet]
    // TS     A timestamp                   [4 octets]
    buffer.putInt(timestamp);
    // IP     Rendezvous point's address    [4 octets]
    buffer.put(rpAddress);
    // PORT   Rendezvous point's OR port    [2 octets]
    buffer.putShort(rpPort);
    // ID     Rendezvous point identity ID [20 octets]
    buffer.put(rpIdentity);
    // KLEN   Length of onion key           [2 octets]
    buffer.putShort((short) rpOnionKey.length);
    // KEY    Rendezvous point onion key [KLEN octets]
    buffer.put(rpOnionKey);
    // RC     Rendezvous cookie            [20 octets]
    buffer.put(cookie);
    // g^x    Diffie-Hellman data, part 1 [128 octets]
    buffer.put(dhPublic);
    return buffer;
}

19. FastLeaderElection#buildMsg()

Project: zookeeper
Source File: FastLeaderElection.java
View license
static ByteBuffer buildMsg(int state, long leader, long zxid, long electionEpoch, long epoch, byte[] configData) {
    byte requestBytes[] = new byte[44 + configData.length];
    ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes);
    /*
         * Building notification packet to send
         */
    requestBuffer.clear();
    requestBuffer.putInt(state);
    requestBuffer.putLong(leader);
    requestBuffer.putLong(zxid);
    requestBuffer.putLong(electionEpoch);
    requestBuffer.putLong(epoch);
    requestBuffer.putInt(Notification.CURRENTVERSION);
    requestBuffer.putInt(configData.length);
    requestBuffer.put(configData);
    return requestBuffer;
}

20. MongoDBOutputOperator#queryFunction1()

View license
/**
   * 8B windowId | 1B opratorId | 3B tupleId
   */
public void queryFunction1(ByteBuffer bb, StringBuilder high, StringBuilder low) {
    bb.putLong(windowId);
    byte opId = (byte) (operatorId);
    bb.put(opId);
    ByteBuffer lowbb = bb;
    lowbb.put((byte) 0);
    lowbb.put((byte) 0);
    lowbb.put((byte) 0);
    //    String str = Hex.encodeHexString(lowbb.array());
    for (byte b : lowbb.array()) {
        low.append(String.format("02x", b & 0xff));
    }
    ByteBuffer highbb = bb;
    highbb.put((byte) 0xff);
    highbb.put((byte) 0xff);
    highbb.put((byte) 0xff);
    for (byte b : highbb.array()) {
        high.append(String.format("02x", b & 0xff));
    }
}

21. CassandraKeyValueServices#makeCompositeBuffer()

View license
static ByteBuffer makeCompositeBuffer(byte[] colName, long ts) {
    assert colName.length <= 1 << 16 : "Cannot use column names larger than 64KiB, was " + colName.length;
    ByteBuffer buffer = ByteBuffer.allocate(6 + /* misc */
    8 + /* timestamp */
    colName.length).order(ByteOrder.BIG_ENDIAN);
    buffer.put((byte) ((colName.length >> 8) & 0xFF));
    buffer.put((byte) (colName.length & 0xFF));
    buffer.put(colName);
    buffer.put((byte) 0);
    buffer.put((byte) 0);
    buffer.put((byte) (8 & 0xFF));
    buffer.putLong(~ts);
    buffer.put((byte) 0);
    buffer.flip();
    return buffer;
}

22. IntroductionProcessor#createIntroductionBuffer()

Project: bitcoinj
Source File: IntroductionProcessor.java
View license
private ByteBuffer createIntroductionBuffer(int timestamp, Router rr, byte[] cookie, byte[] dhPublic) {
    final ByteBuffer buffer = ByteBuffer.allocate(Cell.CELL_LEN);
    final byte[] rpAddress = rr.getAddress().getAddressDataBytes();
    final short rpPort = (short) rr.getOnionPort();
    final byte[] rpIdentity = rr.getIdentityHash().getRawBytes();
    final byte[] rpOnionKey = rr.getOnionKey().getRawBytes();
    // VER    Version byte: set to 3.        [1 octet]
    buffer.put((byte) INTRODUCTION_PROTOCOL_VERSION);
    addAuthentication(buffer);
    //buffer.put((byte) 0);                              // AUTHT  The auth type that is used     [1 octet]
    // TS     A timestamp                   [4 octets]
    buffer.putInt(timestamp);
    // IP     Rendezvous point's address    [4 octets]
    buffer.put(rpAddress);
    // PORT   Rendezvous point's OR port    [2 octets]
    buffer.putShort(rpPort);
    // ID     Rendezvous point identity ID [20 octets]
    buffer.put(rpIdentity);
    // KLEN   Length of onion key           [2 octets]
    buffer.putShort((short) rpOnionKey.length);
    // KEY    Rendezvous point onion key [KLEN octets]
    buffer.put(rpOnionKey);
    // RC     Rendezvous cookie            [20 octets]
    buffer.put(cookie);
    // g^x    Diffie-Hellman data, part 1 [128 octets]
    buffer.put(dhPublic);
    return buffer;
}

23. DigestManager#computeDigestAndPackageForSending()

Project: bookkeeper
Source File: DigestManager.java
View license
/**
     * Computes the digest for an entry and put bytes together for sending.
     *
     * @param entryId
     * @param lastAddConfirmed
     * @param length
     * @param data
     * @return
     */
public ChannelBuffer computeDigestAndPackageForSending(long entryId, long lastAddConfirmed, long length, byte[] data, int doffset, int dlength) {
    byte[] bufferArray = new byte[METADATA_LENGTH + macCodeLength];
    ByteBuffer buffer = ByteBuffer.wrap(bufferArray);
    buffer.putLong(ledgerId);
    buffer.putLong(entryId);
    buffer.putLong(lastAddConfirmed);
    buffer.putLong(length);
    buffer.flip();
    update(buffer.array(), 0, METADATA_LENGTH);
    update(data, doffset, dlength);
    byte[] digest = getValueAndReset();
    buffer.limit(buffer.capacity());
    buffer.position(METADATA_LENGTH);
    buffer.put(digest);
    buffer.flip();
    return ChannelBuffers.wrappedBuffer(ChannelBuffers.wrappedBuffer(buffer), ChannelBuffers.wrappedBuffer(data, doffset, dlength));
}

24. BTreeBlockFinderTest#getFileMatadataWithOnlyNoDictionaryKey()

View license
private DataFileFooter getFileMatadataWithOnlyNoDictionaryKey(byte[] startKey, byte[] endKey, byte[] noDictionaryStartKey, byte[] noDictionaryEndKey) {
    DataFileFooter footer = new DataFileFooter();
    BlockletIndex index = new BlockletIndex();
    BlockletBTreeIndex btreeIndex = new BlockletBTreeIndex();
    ByteBuffer buffer = ByteBuffer.allocate(4 + 0 + 4 + noDictionaryStartKey.length);
    buffer.putInt(0);
    buffer.putInt(noDictionaryStartKey.length);
    buffer.put(noDictionaryStartKey);
    buffer.rewind();
    btreeIndex.setStartKey(buffer.array());
    ByteBuffer buffer1 = ByteBuffer.allocate(4 + 0 + 4 + noDictionaryEndKey.length);
    buffer1.putInt(0);
    buffer1.putInt(noDictionaryEndKey.length);
    buffer1.put(noDictionaryEndKey);
    buffer1.rewind();
    btreeIndex.setEndKey(buffer1.array());
    BlockletMinMaxIndex minMax = new BlockletMinMaxIndex();
    minMax.setMaxValues(new byte[][] { endKey, noDictionaryEndKey });
    minMax.setMinValues(new byte[][] { startKey, noDictionaryStartKey });
    index.setBtreeIndex(btreeIndex);
    index.setMinMaxIndex(minMax);
    footer.setBlockletIndex(index);
    return footer;
}

25. BTreeBlockFinderTest#getFileFooterWithOnlyDictionaryKey()

View license
private DataFileFooter getFileFooterWithOnlyDictionaryKey(byte[] startKey, byte[] endKey, byte[] noDictionaryStartKey, byte[] noDictionaryEndKey) {
    DataFileFooter footer = new DataFileFooter();
    BlockletIndex index = new BlockletIndex();
    BlockletBTreeIndex btreeIndex = new BlockletBTreeIndex();
    ByteBuffer buffer = ByteBuffer.allocate(4 + startKey.length + 4 + 0);
    buffer.putInt(startKey.length);
    buffer.putInt(0);
    buffer.put(startKey);
    buffer.rewind();
    btreeIndex.setStartKey(buffer.array());
    ByteBuffer buffer1 = ByteBuffer.allocate(4 + 0 + 4 + noDictionaryEndKey.length);
    buffer1.putInt(endKey.length);
    buffer1.putInt(0);
    buffer1.put(endKey);
    buffer1.rewind();
    btreeIndex.setEndKey(buffer1.array());
    BlockletMinMaxIndex minMax = new BlockletMinMaxIndex();
    minMax.setMaxValues(new byte[][] { endKey });
    minMax.setMinValues(new byte[][] { startKey });
    index.setBtreeIndex(btreeIndex);
    index.setMinMaxIndex(minMax);
    footer.setBlockletIndex(index);
    return footer;
}

26. ProxyLoginHandler#createSocks4Message()

View license
private ByteBuffer[] createSocks4Message() throws Exception {
    //TODO convert to direct?
    ByteBuffer handshake = ByteBuffer.allocate(256 + mapped_ip.length());
    // socks 4(a)
    handshake.put((byte) 4);
    // command = CONNECT
    handshake.put((byte) 1);
    handshake.putShort((short) remote_address.getPort());
    byte[] ip_bytes = HostNameToIPResolver.syncResolve(remote_address.getAddress().getHostAddress()).getAddress();
    handshake.put(ip_bytes[0]);
    handshake.put(ip_bytes[1]);
    handshake.put(ip_bytes[2]);
    handshake.put(ip_bytes[3]);
    if (socks_user.length() > 0) {
        handshake.put(socks_user.getBytes());
    }
    handshake.put((byte) 0);
    handshake.flip();
    //TODO convert to direct?
    return new ByteBuffer[] { handshake, ByteBuffer.allocate(8) };
}

27. CRCedChunk#getChunkPayload()

View license
public byte[] getChunkPayload() {
    byte[] contentPayload = getContentPayload();
    int length = contentPayload.length;
    ByteBuffer buffer = ByteBuffer.allocate(length + 12);
    buffer.putInt(length);
    buffer.put(type);
    buffer.put(contentPayload);
    buffer.position(4);
    buffer.limit(length + 8);
    long crc = crc(buffer);
    buffer.limit(length + 12);
    buffer.putInt((int) crc);
    buffer.position(0);
    return buffer.array();
}

28. HyperLogLogCollectorTest#makeCollectorBuffer()

Project: druid
Source File: HyperLogLogCollectorTest.java
View license
private ByteBuffer makeCollectorBuffer(int offset, byte[] initialBytes, int remainingBytes) {
    short numNonZero = 0;
    for (byte initialByte : initialBytes) {
        numNonZero += computeNumNonZero(initialByte);
    }
    final short numNonZeroInRemaining = computeNumNonZero((byte) remainingBytes);
    numNonZero += (short) ((HyperLogLogCollector.NUM_BYTES_FOR_BUCKETS - initialBytes.length) * numNonZeroInRemaining);
    ByteBuffer biggerOffset = ByteBuffer.allocate(HyperLogLogCollector.getLatestNumBytesForDenseStorage());
    biggerOffset.put(HLLCV1.VERSION);
    biggerOffset.put((byte) offset);
    biggerOffset.putShort(numNonZero);
    biggerOffset.put((byte) 0);
    biggerOffset.putShort((short) 0);
    biggerOffset.put(initialBytes);
    while (biggerOffset.hasRemaining()) {
        biggerOffset.put((byte) remainingBytes);
    }
    biggerOffset.clear();
    return biggerOffset.asReadOnlyBuffer();
}

29. ConditionalReplaceOperationTest#testEncode()

View license
@Test
public void testEncode() throws Exception {
    Long key = 1L;
    String newValue = "The one";
    String oldValue = "Another one";
    ConditionalReplaceOperation<Long, String> operation = new ConditionalReplaceOperation<Long, String>(key, oldValue, newValue, TIME_SOURCE.getTimeMillis());
    ByteBuffer byteBuffer = operation.encode(keySerializer, valueSerializer);
    ByteBuffer expected = ByteBuffer.allocate(BYTE_SIZE_BYTES + INT_SIZE_BYTES + 2 * LONG_SIZE_BYTES + INT_SIZE_BYTES + oldValue.length() + newValue.length());
    expected.put(OperationCode.REPLACE_CONDITIONAL.getValue());
    expected.putLong(TIME_SOURCE.getTimeMillis());
    expected.putInt(LONG_SIZE_BYTES);
    expected.putLong(key);
    expected.putInt(oldValue.length());
    expected.put(oldValue.getBytes());
    expected.put(newValue.getBytes());
    expected.flip();
    assertArrayEquals(expected.array(), byteBuffer.array());
}

30. ByteBufferDataTypeTest#testWrite()

Project: es4j
Source File: ByteBufferDataTypeTest.java
View license
@Test
public void testWrite() throws Exception {
    ByteBuffer b = ByteBuffer.allocate(100);
    b.position(1);
    b.limit(50);
    b.putInt(100);
    b.position(1);
    WriteBuffer writeBuffer = new WriteBuffer();
    ByteBufferDataType byteBufferDataType = new ByteBufferDataType();
    byteBufferDataType.write(writeBuffer, b);
    ByteBuffer rb = writeBuffer.getBuffer();
    rb.rewind();
    ByteBuffer b1 = (ByteBuffer) byteBufferDataType.read(rb);
    assertEquals(b1.limit(), 49);
    assertEquals(b1.getInt(), 100);
    rb.rewind();
    ByteBuffer[] byteBuffers = new ByteBuffer[1];
    byteBufferDataType.read(rb, byteBuffers, 1, false);
    assertEquals(byteBuffers[0].getInt(), 100);
}

31. ARP#serialize()

Project: floodlight
Source File: ARP.java
View license
@Override
public byte[] serialize() {
    int length = 8 + (2 * (0xff & this.hardwareAddressLength)) + (2 * (0xff & this.protocolAddressLength));
    byte[] data = new byte[length];
    ByteBuffer bb = ByteBuffer.wrap(data);
    bb.putShort(this.hardwareType);
    bb.putShort(this.protocolType);
    bb.put(this.hardwareAddressLength);
    bb.put(this.protocolAddressLength);
    bb.putShort((short) this.opCode.getOpcode());
    bb.put(this.senderHardwareAddress.getBytes());
    bb.put(this.senderProtocolAddress.getBytes());
    bb.put(this.targetHardwareAddress.getBytes());
    bb.put(this.targetProtocolAddress.getBytes());
    return data;
}

32. SpatialQueryFilter#toBinary()

Project: geowave
Source File: SpatialQueryFilter.java
View license
@Override
public byte[] toBinary() {
    final byte[] geometryBinary = preparedGeometryImage.geometryBinary;
    int geometryFieldIdByteSize = 4;
    for (final ByteArrayId id : geometryFieldIds) {
        geometryFieldIdByteSize += (4 + id.getBytes().length);
    }
    final ByteBuffer geometryFieldIdBuffer = ByteBuffer.allocate(geometryFieldIdByteSize);
    geometryFieldIdBuffer.putInt(geometryFieldIds.size());
    for (final ByteArrayId id : geometryFieldIds) {
        geometryFieldIdBuffer.putInt(id.getBytes().length);
        geometryFieldIdBuffer.put(id.getBytes());
    }
    final byte[] theRest = super.toBinary();
    final ByteBuffer buf = ByteBuffer.allocate(12 + geometryBinary.length + geometryFieldIdByteSize + theRest.length);
    buf.putInt(compareOperation.ordinal());
    buf.putInt(geometryBinary.length);
    buf.putInt(geometryFieldIdByteSize);
    buf.put(geometryBinary);
    buf.put(geometryFieldIdBuffer.array());
    buf.put(theRest);
    return buf.array();
}

33. BTreeBlockFinderTest#getFileMatadataWithOnlyNoDictionaryKey()

View license
private DataFileFooter getFileMatadataWithOnlyNoDictionaryKey(byte[] startKey, byte[] endKey, byte[] noDictionaryStartKey, byte[] noDictionaryEndKey) {
    DataFileFooter footer = new DataFileFooter();
    BlockletIndex index = new BlockletIndex();
    BlockletBTreeIndex btreeIndex = new BlockletBTreeIndex();
    ByteBuffer buffer = ByteBuffer.allocate(4 + 0 + 4 + noDictionaryStartKey.length);
    buffer.putInt(0);
    buffer.putInt(noDictionaryStartKey.length);
    buffer.put(noDictionaryStartKey);
    buffer.rewind();
    btreeIndex.setStartKey(buffer.array());
    ByteBuffer buffer1 = ByteBuffer.allocate(4 + 0 + 4 + noDictionaryEndKey.length);
    buffer1.putInt(0);
    buffer1.putInt(noDictionaryEndKey.length);
    buffer1.put(noDictionaryEndKey);
    buffer1.rewind();
    btreeIndex.setEndKey(buffer1.array());
    BlockletMinMaxIndex minMax = new BlockletMinMaxIndex();
    minMax.setMaxValues(new byte[][] { endKey, noDictionaryEndKey });
    minMax.setMinValues(new byte[][] { startKey, noDictionaryStartKey });
    index.setBtreeIndex(btreeIndex);
    index.setMinMaxIndex(minMax);
    footer.setBlockletIndex(index);
    return footer;
}

34. BTreeBlockFinderTest#getFileFooterWithOnlyDictionaryKey()

View license
private DataFileFooter getFileFooterWithOnlyDictionaryKey(byte[] startKey, byte[] endKey, byte[] noDictionaryStartKey, byte[] noDictionaryEndKey) {
    DataFileFooter footer = new DataFileFooter();
    BlockletIndex index = new BlockletIndex();
    BlockletBTreeIndex btreeIndex = new BlockletBTreeIndex();
    ByteBuffer buffer = ByteBuffer.allocate(4 + startKey.length + 4 + 0);
    buffer.putInt(startKey.length);
    buffer.putInt(0);
    buffer.put(startKey);
    buffer.rewind();
    btreeIndex.setStartKey(buffer.array());
    ByteBuffer buffer1 = ByteBuffer.allocate(4 + 0 + 4 + noDictionaryEndKey.length);
    buffer1.putInt(endKey.length);
    buffer1.putInt(0);
    buffer1.put(endKey);
    buffer1.rewind();
    btreeIndex.setEndKey(buffer1.array());
    BlockletMinMaxIndex minMax = new BlockletMinMaxIndex();
    minMax.setMaxValues(new byte[][] { endKey });
    minMax.setMinValues(new byte[][] { startKey });
    index.setBtreeIndex(btreeIndex);
    index.setMinMaxIndex(minMax);
    footer.setBlockletIndex(index);
    return footer;
}

35. ByteBufferUtilTest#testClone()

Project: cassandra
Source File: ByteBufferUtilTest.java
View license
@Test
public void testClone() {
    ByteBuffer bb = ByteBufferUtil.bytes(s);
    ByteBuffer clone1 = ByteBufferUtil.clone(bb);
    assert bb != clone1;
    assert bb.equals(clone1);
    assert bb.array() != clone1.array();
    bb = fromStringWithPosition(s, 10, false);
    ByteBuffer clone2 = ByteBufferUtil.clone(bb);
    assert bb != clone2;
    assert bb.equals(clone2);
    assert clone1.equals(clone2);
    assert bb.array() != clone2.array();
    bb = fromStringWithPosition(s, 10, true);
    ByteBuffer clone3 = ByteBufferUtil.clone(bb);
    assert bb != clone3;
    assert bb.equals(clone3);
    assert clone1.equals(clone3);
}

36. BaseKeyValueOperation#encode()

Project: ehcache3
Source File: BaseKeyValueOperation.java
View license
/**
   * Here we need to encode two objects of unknown size: the key and the value.
   * Encoding should be done in such a way that the key and value can be read
   * separately while decoding the bytes.
   * So the way it is done here is by writing the size of the payload along with
   * the payload. That is, the size of the key payload is written before the key
   * itself. The value payload is written after that.
   *
   * While decoding, the size is read first and then reading the same number of
   * bytes will get you the key payload. Whatever that is left is the value payload.
   */
@Override
public ByteBuffer encode(final Serializer<K> keySerializer, final Serializer<V> valueSerializer) {
    ByteBuffer keyBuf = keySerializer.serialize(key);
    ByteBuffer valueBuf = valueSerializer.serialize(value);
    int size = // Operation type
    BYTE_SIZE_BYTES + // Size of the key payload
    INT_SIZE_BYTES + // Size of expiration time stamp
    LONG_SIZE_BYTES + // the key payload itself
    keyBuf.remaining() + // the value payload
    valueBuf.remaining();
    ByteBuffer buffer = ByteBuffer.allocate(size);
    buffer.put(getOpCode().getValue());
    buffer.putLong(this.timeStamp);
    buffer.putInt(keyBuf.remaining());
    buffer.put(keyBuf);
    buffer.put(valueBuf);
    buffer.flip();
    return buffer;
}

37. ConditionalReplaceOperationTest#testDecode()

View license
@Test
public void testDecode() throws Exception {
    Long key = 1L;
    String newValue = "The one";
    String oldValue = "Another one";
    ByteBuffer blob = ByteBuffer.allocate(BYTE_SIZE_BYTES + INT_SIZE_BYTES + 2 * LONG_SIZE_BYTES + INT_SIZE_BYTES + oldValue.length() + newValue.length());
    blob.put(OperationCode.REPLACE_CONDITIONAL.getValue());
    blob.putLong(TIME_SOURCE.getTimeMillis());
    blob.putInt(LONG_SIZE_BYTES);
    blob.putLong(key);
    blob.putInt(oldValue.length());
    blob.put(oldValue.getBytes());
    blob.put(newValue.getBytes());
    blob.flip();
    ConditionalReplaceOperation<Long, String> operation = new ConditionalReplaceOperation<Long, String>(blob, keySerializer, valueSerializer);
    assertEquals(key, operation.getKey());
    assertEquals(newValue, operation.getValue());
    assertEquals(oldValue, operation.getOldValue());
}

38. OffHeapValueHolderPortability#encode()

View license
@Override
public ByteBuffer encode(OffHeapValueHolder<V> valueHolder) {
    ByteBuffer serialized;
    if (valueHolder instanceof BinaryValueHolder && ((BinaryValueHolder) valueHolder).isBinaryValueAvailable()) {
        serialized = ((BinaryValueHolder) valueHolder).getBinaryValue();
    } else {
        serialized = serializer.serialize(valueHolder.value());
    }
    ByteBuffer byteBuffer = ByteBuffer.allocate(serialized.remaining() + FIELDS_OVERHEAD);
    byteBuffer.putLong(valueHolder.getId());
    byteBuffer.putLong(valueHolder.creationTime(OffHeapValueHolder.TIME_UNIT));
    byteBuffer.putLong(valueHolder.lastAccessTime(OffHeapValueHolder.TIME_UNIT));
    byteBuffer.putLong(valueHolder.expirationTime(OffHeapValueHolder.TIME_UNIT));
    byteBuffer.putLong(valueHolder.hits());
    byteBuffer.put(serialized);
    byteBuffer.flip();
    return byteBuffer;
}

39. PsshAtomUtil#buildPsshAtom()

Project: ExoPlayer
Source File: PsshAtomUtil.java
View license
/**
   * Builds a PSSH atom for a given {@link UUID} containing the given scheme specific data.
   *
   * @param uuid The UUID of the scheme.
   * @param data The scheme specific data.
   * @return The PSSH atom.
   */
public static byte[] buildPsshAtom(UUID uuid, byte[] data) {
    int psshBoxLength = Atom.FULL_HEADER_SIZE + 16 + /* UUID */
    4 + /* DataSize */
    data.length;
    ByteBuffer psshBox = ByteBuffer.allocate(psshBoxLength);
    psshBox.putInt(psshBoxLength);
    psshBox.putInt(Atom.TYPE_pssh);
    psshBox.putInt(0);
    psshBox.putLong(uuid.getMostSignificantBits());
    psshBox.putLong(uuid.getLeastSignificantBits());
    psshBox.putInt(data.length);
    psshBox.put(data);
    return psshBox.array();
}

40. PebbleProtocol#encodeBlobDBClear()

Project: Gadgetbridge
Source File: PebbleProtocol.java
View license
private byte[] encodeBlobDBClear(byte database) {
    final short LENGTH_BLOBDB_CLEAR = 4;
    ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + LENGTH_BLOBDB_CLEAR);
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putShort(LENGTH_BLOBDB_CLEAR);
    buf.putShort(ENDPOINT_BLOBDB);
    buf.order(ByteOrder.LITTLE_ENDIAN);
    buf.put(BLOBDB_CLEAR);
    // token
    buf.putShort((short) mRandom.nextInt());
    buf.put(database);
    return buf.array();
}

41. PebbleProtocol#encodeApplicationMessageAck()

Project: Gadgetbridge
Source File: PebbleProtocol.java
View license
byte[] encodeApplicationMessageAck(UUID uuid, byte id) {
    // +ACK
    ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + 18);
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putShort((short) 18);
    buf.putShort(ENDPOINT_APPLICATIONMESSAGE);
    buf.put(APPLICATIONMESSAGE_ACK);
    buf.put(id);
    buf.putLong(uuid.getMostSignificantBits());
    buf.putLong(uuid.getMostSignificantBits());
    return buf.array();
}

42. TextRangeFilter#toBinary()

Project: geowave
Source File: TextRangeFilter.java
View license
@Override
public byte[] toBinary() {
    final byte[] startBytes = StringUtils.stringToBinary(start);
    final byte[] endBytes = StringUtils.stringToBinary(end);
    final ByteBuffer bb = ByteBuffer.allocate(4 + 4 + 4 + fieldId.getBytes().length + 4 + startBytes.length + endBytes.length);
    bb.putInt(fieldId.getBytes().length);
    bb.putInt(startBytes.length);
    bb.putInt(endBytes.length);
    bb.put(fieldId.getBytes());
    final int caseSensitiveInt = (caseSensitive) ? 1 : 0;
    bb.putInt(caseSensitiveInt);
    bb.put(startBytes);
    bb.put(endBytes);
    return bb.array();
}

43. ConsistentHashResolverTest#createNodeListWithThreeItems()

Project: kaa
Source File: ConsistentHashResolverTest.java
View license
private List<OperationsNodeInfo> createNodeListWithThreeItems() {
    ByteBuffer buffer1 = ByteBuffer.allocate(8);
    buffer1.put(0, (byte) 98);
    buffer1.put(1, (byte) 98);
    ByteBuffer buffer2 = ByteBuffer.allocate(8);
    buffer2.put(0, (byte) 99);
    buffer2.put(1, (byte) 99);
    ByteBuffer buffer3 = ByteBuffer.allocate(2);
    buffer3.put(0, (byte) 100);
    buffer3.put(1, (byte) 100);
    ConnectionInfo connectionInfo1 = new ConnectionInfo("thrift1", 4241, buffer1);
    ConnectionInfo connectionInfo2 = new ConnectionInfo("thrift2", 4242, buffer2);
    ConnectionInfo connectionInfo3 = new ConnectionInfo("thrift3", 4243, buffer3);
    OperationsNodeInfo operationsNodeInfo1 = new OperationsNodeInfo(connectionInfo1, null, 1231L, null);
    OperationsNodeInfo operationsNodeInfo2 = new OperationsNodeInfo(connectionInfo2, null, 1232L, null);
    OperationsNodeInfo operationsNodeInfo3 = new OperationsNodeInfo(connectionInfo3, null, 1233L, null);
    return Arrays.asList(operationsNodeInfo1, operationsNodeInfo2, operationsNodeInfo3);
}

44. IoBufferTest#testPositionBuffer()

Project: mina
Source File: IoBufferTest.java
View license
/**
     * Test the position method over a buffer
     */
@Test
public void testPositionBuffer() {
    ByteBuffer bb1 = ByteBuffer.allocate(4);
    bb1.put("012".getBytes());
    bb1.flip();
    ByteBuffer bb2 = ByteBuffer.allocate(4);
    bb2.put("3456".getBytes());
    bb2.flip();
    ByteBuffer bb3 = ByteBuffer.allocate(4);
    bb3.put("789".getBytes());
    bb3.flip();
    // The resulting buffer will be seen as "0123456789"
    IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2, bb3);
    // Iterate and check the position
    for (int i = 0; i < ioBuffer.limit(); i++) {
        assertEquals(i, ioBuffer.position());
        ioBuffer.get();
    }
}

45. IoBufferTest#testSetPositionBuffer()

Project: mina
Source File: IoBufferTest.java
View license
/**
     * Test set position method over a buffer
     */
@Test
public void testSetPositionBuffer() {
    ByteBuffer bb1 = ByteBuffer.allocate(4);
    bb1.put("012".getBytes());
    bb1.flip();
    ByteBuffer bb2 = ByteBuffer.allocate(4);
    bb2.put("3456".getBytes());
    bb2.flip();
    ByteBuffer bb3 = ByteBuffer.allocate(4);
    bb3.put("789".getBytes());
    bb3.flip();
    // The resulting buffer will be seen as "0123456789"
    IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2, bb3);
    // Check with random positions
    for (int i : new int[] { 4, 6, 7, 8, 3, 9, 1, 5, 0, 2 }) {
        ioBuffer.position(i);
        assertEquals('0' + i, ioBuffer.get());
    }
}

46. IoBufferTest#testPositionNegativeValue()

Project: mina
Source File: IoBufferTest.java
View license
/**
     * Test the position method with a negative value
     */
@Test(expected = IllegalArgumentException.class)
public void testPositionNegativeValue() {
    ByteBuffer bb1 = ByteBuffer.allocate(4);
    bb1.put("0123".getBytes());
    bb1.flip();
    ByteBuffer bb2 = ByteBuffer.allocate(4);
    bb2.put("4567".getBytes());
    bb2.flip();
    ByteBuffer bb3 = ByteBuffer.allocate(4);
    bb3.put("89".getBytes());
    bb3.flip();
    IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2, bb3);
    ioBuffer.position(-1);
}

47. IoBufferTest#testPositionAboveValue()

Project: mina
Source File: IoBufferTest.java
View license
/**
     * Test the position method with a value above the buffer size
     */
@Test(expected = IllegalArgumentException.class)
public void testPositionAboveValue() {
    ByteBuffer bb1 = ByteBuffer.allocate(4);
    bb1.put("012".getBytes());
    bb1.flip();
    ByteBuffer bb2 = ByteBuffer.allocate(4);
    bb2.put("3456".getBytes());
    bb2.flip();
    ByteBuffer bb3 = ByteBuffer.allocate(4);
    bb3.put("789".getBytes());
    bb3.flip();
    // The resulting buffer will be seen as "0123456789"
    IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2, bb3);
    ioBuffer.position(11);
}

48. RCONPacket#toBuffer()

Project: Nukkit
Source File: RCONPacket.java
View license
public ByteBuffer toBuffer() {
    ByteBuffer buffer = ByteBuffer.allocate(this.payload.length + 14);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.putInt(this.payload.length + 10);
    buffer.putInt(this.id);
    buffer.putInt(this.type);
    buffer.put(this.payload);
    buffer.put((byte) 0);
    buffer.put((byte) 0);
    buffer.flip();
    return buffer;
}

49. CompressedResourceHeader#getBytes()

View license
public byte[] getBytes(ByteOrder order) {
    Objects.requireNonNull(order);
    ByteBuffer buffer = ByteBuffer.allocate(SIZE);
    buffer.order(order);
    buffer.putInt(MAGIC);
    buffer.putLong(compressedSize);
    buffer.putLong(uncompressedSize);
    buffer.putInt(decompressorNameOffset);
    buffer.putInt(contentOffset);
    buffer.put(isTerminal ? (byte) 1 : (byte) 0);
    return buffer.array();
}

50. ByteBufferUtilTest#testClone()

View license
@Test
public void testClone() {
    ByteBuffer bb = ByteBufferUtil.bytes(s);
    ByteBuffer clone1 = ByteBufferUtil.clone(bb);
    assert bb != clone1;
    assert bb.equals(clone1);
    assert bb.array() != clone1.array();
    bb = fromStringWithPosition(s, 10, false);
    ByteBuffer clone2 = ByteBufferUtil.clone(bb);
    assert bb != clone2;
    assert bb.equals(clone2);
    assert clone1.equals(clone2);
    assert bb.array() != clone2.array();
    bb = fromStringWithPosition(s, 10, true);
    ByteBuffer clone3 = ByteBufferUtil.clone(bb);
    assert bb != clone3;
    assert bb.equals(clone3);
    assert clone1.equals(clone3);
}

51. ALPNHackServerHelloExplorer#createNewOutputRecords()

View license
static ByteBuffer createNewOutputRecords(byte[] newFirstMessage, List<ByteBuffer> records) {
    int length = newFirstMessage.length;
    //Framing layer
    length += 5;
    for (int i = 1; i < records.size(); ++i) {
        //the first record is the old server hello, so we start at 1 rather than zero
        ByteBuffer rec = records.get(i);
        length += rec.remaining();
    }
    byte[] newData = new byte[length];
    ByteBuffer ret = ByteBuffer.wrap(newData);
    ByteBuffer oldHello = records.get(0);
    //type
    ret.put(oldHello.get());
    //major
    ret.put(oldHello.get());
    //minor
    ret.put(oldHello.get());
    ret.put((byte) ((newFirstMessage.length >> 8) & 0xFF));
    ret.put((byte) (newFirstMessage.length & 0xFF));
    ret.put(newFirstMessage);
    for (int i = 1; i < records.size(); ++i) {
        ByteBuffer rec = records.get(i);
        ret.put(rec);
    }
    ret.flip();
    return ret;
}

52. GenerateCPPTestFiles#writeServerCallResponse()

Project: voltdb
Source File: GenerateCPPTestFiles.java
View license
private static void writeServerCallResponse(SocketChannel sc, long clientData) throws IOException {
    ByteBuffer message = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE);
    // Message size.
    writeMessageHeader(message);
    // Match the client data.
    message.putLong(clientData);
    // No optional fields
    message.put((byte) 0);
    // Status 1 == OK.
    message.put((byte) 1);
    // App Status 1 == OK.
    message.put((byte) 1);
    // cluster round trip time.
    message.putInt(100);
    // No tables.
    message.putShort((short) 0);
    int size = message.position() - 4;
    message.putInt(0, size);
    message.flip();
    sc.write(message);
}

53. GenerateCPPTestFiles#writeServerAuthenticationResponse()

Project: voltdb
Source File: GenerateCPPTestFiles.java
View license
private static void writeServerAuthenticationResponse(SocketChannel sc, boolean success) throws IOException {
    ByteBuffer message;
    message = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE);
    writeMessageHeader(message);
    // success == 0, failure == 100
    message.put(success ? (byte) 0 : (byte) 100);
    // Server Host ID.
    message.putInt(1);
    // Connection ID.
    message.putLong(1);
    // Timestamp.
    TimestampType tp = new TimestampType();
    message.putLong(tp.getTime());
    // IP Address.  There's no place like home.
    message.putInt(0x7f000001);
    // Empty build string.
    message.putInt(0);
    int size = message.position() - 4;
    message.putInt(0, size);
    message.flip();
    sc.write(message);
}

54. Http2Frame#toBuffer()

Project: mina
Source File: Http2Frame.java
View license
/**
     * Serialize the frame to a buffer.
     * 
     * @return the allocated buffer
     */
public ByteBuffer toBuffer() {
    ByteBuffer buffer = ByteBuffer.allocateDirect(HTTP2_HEADER_LENGTH + getLength());
    buffer.put((byte) (getLength() >> 16));
    buffer.put((byte) (getLength() >> 8));
    buffer.put((byte) (getLength()));
    buffer.put((byte) getType());
    buffer.put(getFlags());
    buffer.putInt(getStreamID());
    writePayload(buffer);
    buffer.flip();
    return buffer;
}

55. Draft_75#createBinaryFrame()

Project: mixpanel-android
Source File: Draft_75.java
View license
@Override
public ByteBuffer createBinaryFrame(Framedata framedata) {
    if (framedata.getOpcode() != Opcode.TEXT) {
        throw new RuntimeException("only text frames supported");
    }
    ByteBuffer pay = framedata.getPayloadData();
    ByteBuffer b = ByteBuffer.allocate(pay.remaining() + 2);
    b.put(START_OF_FRAME);
    pay.mark();
    b.put(pay);
    pay.reset();
    b.put(END_OF_FRAME);
    b.flip();
    return b;
}

56. ID3v2ChapterFrameData#packFrameData()

Project: mp3agic
Source File: ID3v2ChapterFrameData.java
View license
protected byte[] packFrameData() {
    ByteBuffer bb = ByteBuffer.allocate(getLength());
    bb.put(id.getBytes());
    bb.put((byte) 0);
    bb.putInt(startTime);
    bb.putInt(endTime);
    bb.putInt(startOffset);
    bb.putInt(endOffset);
    for (ID3v2Frame frame : subframes) {
        try {
            bb.put(frame.toBytes());
        } catch (NotSupportedException e) {
            e.printStackTrace();
        }
    }
    return bb.array();
}

57. ClusterMessage#getBytes()

Project: onos
Source File: ClusterMessage.java
View license
/**
     * Serializes this instance.
     * @return bytes
     */
public byte[] getBytes() {
    byte[] senderBytes = sender.toString().getBytes(Charsets.UTF_8);
    byte[] subjectBytes = subject.value().getBytes(Charsets.UTF_8);
    int capacity = 12 + senderBytes.length + subjectBytes.length + payload.length;
    ByteBuffer buffer = ByteBuffer.allocate(capacity);
    buffer.putInt(senderBytes.length);
    buffer.put(senderBytes);
    buffer.putInt(subjectBytes.length);
    buffer.put(subjectBytes);
    buffer.putInt(payload.length);
    buffer.put(payload);
    return buffer.array();
}

58. OperaLauncherProtocol#sendRequestHeader()

View license
/**
   * Send the 8 byte header before a Opera Launcher message body (payload).
   *
   * @param type the payload type to be sent after
   * @param size size of the payload following the header
   * @throws IOException if socket send error or protocol parse error
   */
private void sendRequestHeader(MessageType type, int size) throws IOException {
    ByteBuffer buf = ByteBuffer.allocate(8);
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.put((byte) 'L');
    buf.put((byte) '1');
    buf.put(type.getValue());
    // request
    buf.put((byte) 0);
    buf.putInt(size);
    buf.flip();
    logger.finest("SEND: type=" + (0) + ", command=" + ((int) type.getValue()) + ", size=" + size);
    os.write(buf.array());
}

59. RconHandler#assemblePacket()

Project: SimpleServer
Source File: RconHandler.java
View license
private byte[] assemblePacket(byte[] command, int requestID, int responseType) {
    ByteBuffer bbuf;
    int packetSize = INT * 2 + (command.length) + 1 + 1;
    bbuf = ByteBuffer.allocate(packetSize + 4);
    bbuf.order(ByteOrder.LITTLE_ENDIAN);
    bbuf.putInt(packetSize);
    bbuf.putInt(requestID);
    bbuf.putInt(responseType);
    bbuf.put(command);
    bbuf.put((byte) 0);
    bbuf.put((byte) 0);
    byte[] send = bbuf.array();
    return send;
}

60. AcceptMessage#toByteArray()

Project: ThriftyPaxos
Source File: AcceptMessage.java
View license
public byte[] toByteArray() {
    byte[] ret = new byte[4 + 4 + 4 + 8 + 4 + 8 + 8 + 4 + request.length];
    ByteBuffer b = ByteBuffer.wrap(ret);
    b.putInt(getType());
    b.putInt(getSender().hashCode());
    // b.putInt(clientIDHash);
    // b.putLong(clientReqNum);
    b.putInt(round);
    b.putLong(index);
    b.putLong(firstUnstable);
    b.putInt(request.length);
    b.put(request);
    return ret;
}

61. ChannelStatusObject#getData()

Project: thunder
Source File: ChannelStatusObject.java
View license
@Override
public byte[] getData() {
    //TODO: Have some proper summary here..
    ByteBuffer byteBuffer = ByteBuffer.allocate(4 + pubkeyA.length + pubkeyB.length + 4 + 4 + 4);
    byteBuffer.putInt(timestamp);
    byteBuffer.put(pubkeyA);
    byteBuffer.put(pubkeyB);
    byteBuffer.putInt(latency);
    byteBuffer.putInt(feeA);
    byteBuffer.putInt(feeB);
    return byteBuffer.array();
}

62. PubkeyChannelObject#getData()

Project: thunder
Source File: PubkeyChannelObject.java
View license
@Override
public byte[] getData() {
    //TODO: Have some proper summary here..
    ByteBuffer byteBuffer = ByteBuffer.allocate(4 + pubkeyB.length + pubkeyB1.length + pubkeyA.length + pubkeyA1.length + txidAnchor.length);
    byteBuffer.putInt(timestamp);
    byteBuffer.put(pubkeyB);
    byteBuffer.put(pubkeyB1);
    byteBuffer.put(pubkeyA);
    byteBuffer.put(pubkeyA1);
    byteBuffer.put(txidAnchor);
    return byteBuffer.array();
}

63. ForeignHost#sendPoisonPill()

Project: voltdb
Source File: ForeignHost.java
View license
public void sendPoisonPill(String err, int cause) {
    // if this link is "gone silent" for partition tests, just drop the message on the floor
    if (m_linkCutForTest.get()) {
        return;
    }
    byte errBytes[];
    try {
        errBytes = err.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return;
    }
    ByteBuffer message = ByteBuffer.allocate(24 + errBytes.length);
    message.putInt(message.capacity() - 4);
    message.putLong(-1);
    message.putInt(POISON_PILL);
    message.putInt(errBytes.length);
    message.put(errBytes);
    message.putInt(cause);
    message.flip();
    m_network.enqueue(message);
}

64. TestPicoNetwork#testLargeMessages()

Project: voltdb
Source File: TestPicoNetwork.java
View license
@Test
public void testLargeMessages() throws Exception {
    ByteBuffer buf = ByteBuffer.allocate(1024 * 1024 * 30);
    buf.putInt(buf.capacity() - 4);
    buf.position(0);
    while (buf.hasRemaining()) {
        rawChannel.write(buf);
    }
    ByteBuffer receipt = messages.take();
    assertEquals(receipt.capacity(), buf.capacity() - 4);
    buf.clear();
    pn.enqueue(buf.duplicate());
    buf.clear();
    while (buf.hasRemaining()) {
        rawChannel.read(buf);
    }
}

65. FastLeaderElection#buildMsg()

Project: zookeeper
Source File: FastLeaderElection.java
View license
static ByteBuffer buildMsg(int state, long leader, long zxid, long electionEpoch, long epoch) {
    byte requestBytes[] = new byte[40];
    ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes);
    /*
         * Building notification packet to send, this is called directly only in tests
         */
    requestBuffer.clear();
    requestBuffer.putInt(state);
    requestBuffer.putLong(leader);
    requestBuffer.putLong(zxid);
    requestBuffer.putLong(electionEpoch);
    requestBuffer.putLong(epoch);
    requestBuffer.putInt(0x1);
    return requestBuffer;
}

66. BaseKeyValueOperationTest#testEncode()

View license
@Test
public void testEncode() throws Exception {
    Long key = 12L;
    String value = "The value";
    Operation<Long, String> operation = getNewOperation(key, value, TIME_SOURCE.getTimeMillis());
    ByteBuffer byteBuffer = operation.encode(keySerializer, valueSerializer);
    ByteBuffer expected = ByteBuffer.allocate(BYTE_SIZE_BYTES + INT_SIZE_BYTES + 2 * LONG_SIZE_BYTES + value.length());
    expected.put(getOperationCode().getValue());
    expected.putLong(TIME_SOURCE.getTimeMillis());
    expected.putInt(LONG_SIZE_BYTES);
    expected.putLong(key);
    expected.put(value.getBytes());
    expected.flip();
    assertArrayEquals(expected.array(), byteBuffer.array());
}

67. GeoObjectDimensionValues#toBinary()

View license
@Override
public byte[] toBinary() {
    final ByteBuffer b = ByteBuffer.allocate(((4 + values.length) * 8) + 4 + 8);
    b.putLong(count);
    b.putDouble(x);
    b.putDouble(y);
    b.putDouble(z);
    b.putDouble(distance);
    b.putInt(values.length);
    for (final double value : values) {
        b.putDouble(value);
    }
    return b.array();
}

68. DateRangeFilter#toBinary()

Project: geowave
Source File: DateRangeFilter.java
View license
@Override
public byte[] toBinary() {
    final ByteBuffer bb = ByteBuffer.allocate(4 + fieldId.getBytes().length + 8 + 8 + 8);
    bb.putInt(fieldId.getBytes().length);
    bb.put(fieldId.getBytes());
    bb.putLong(start.getTime());
    bb.putLong(end.getTime());
    final int rangeInclusiveHighInt = (inclusiveHigh) ? 1 : 0;
    final int rangeInclusiveLowInt = (inclusiveLow) ? 1 : 0;
    bb.putInt(rangeInclusiveLowInt);
    bb.putInt(rangeInclusiveHighInt);
    return bb.array();
}

69. DataStoreUtils#merge()

Project: geowave
Source File: DataStoreUtils.java
View license
private static byte[] merge(final byte vis1[], final byte vis2[]) {
    if ((vis1 == null) || (vis1.length == 0)) {
        return vis2;
    } else if ((vis2 == null) || (vis2.length == 0)) {
        return vis1;
    }
    final ByteBuffer buffer = ByteBuffer.allocate(vis1.length + 3 + vis2.length);
    buffer.putChar('(');
    buffer.put(vis1);
    buffer.putChar(')');
    buffer.put(BEG_AND_BYTE);
    buffer.put(vis2);
    buffer.put(END_AND_BYTE);
    return buffer.array();
}

70. AccumuloDataStore#getRowIdBytes()

Project: geowave
Source File: AccumuloDataStore.java
View license
protected static byte[] getRowIdBytes(final AccumuloRowId rowElements) {
    final ByteBuffer buf = ByteBuffer.allocate(12 + rowElements.getDataId().length + rowElements.getAdapterId().length + rowElements.getInsertionId().length);
    buf.put(rowElements.getInsertionId());
    buf.put(rowElements.getAdapterId());
    buf.put(rowElements.getDataId());
    buf.putInt(rowElements.getAdapterId().length);
    buf.putInt(rowElements.getDataId().length);
    buf.putInt(rowElements.getNumberOfDuplicates());
    return buf.array();
}

71. HBaseUtils#merge()

Project: geowave
Source File: HBaseUtils.java
View license
private static byte[] merge(final byte vis1[], final byte vis2[]) {
    if ((vis1 == null) || (vis1.length == 0)) {
        return vis2;
    } else if ((vis2 == null) || (vis2.length == 0)) {
        return vis1;
    }
    final ByteBuffer buffer = ByteBuffer.allocate(vis1.length + 3 + vis2.length);
    buffer.putChar('(');
    buffer.put(vis1);
    buffer.putChar(')');
    buffer.put(BEG_AND_BYTE);
    buffer.put(vis2);
    buffer.put(END_AND_BYTE);
    return buffer.array();
}

72. TestBinaryClient#createPacket()

Project: ignite
Source File: TestBinaryClient.java
View license
/**
     * Creates hessian packet from client message.
     *
     * @param msg Message to be sent.
     * @return Raw packet.
     * @throws IOException If serialization failed.
     */
private byte[] createPacket(GridClientMessage msg) throws IOException {
    msg.clientId(id);
    ByteBuffer res = marsh.marshal(msg, 45);
    ByteBuffer slice = res.slice();
    slice.put((byte) 0x90);
    slice.putInt(res.remaining() - 5);
    slice.putLong(msg.requestId());
    slice.put(U.uuidToBytes(msg.clientId()));
    slice.put(U.uuidToBytes(msg.destinationId()));
    byte[] arr = new byte[res.remaining()];
    res.get(arr);
    return arr;
}

73. MPEGUtilTest#testGotoNextMarker()

Project: jcodec
Source File: MPEGUtilTest.java
View license
@Test
public void testGotoNextMarker() {
    ByteBuffer input = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0x78, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 1, 0x75, 6, 7, 8, 9, 10, 0, 0, 1, 0x72, 11, 12, 13 });
    ByteBuffer before1 = MPEGUtil.gotoNextMarker(input);
    assertArrayEquals(new byte[] { 0, 0, 0, 0, 0, 0, 0 }, NIOUtils.toArray(before1));
    assertEquals(input.position(), 7);
    input.position(input.position() + 4);
    ByteBuffer before2 = MPEGUtil.gotoNextMarker(input);
    assertArrayEquals(new byte[] { 1, 2, 3, 4, 5, 0, 0, 0, 0, 0 }, NIOUtils.toArray(before2));
    assertEquals(input.position(), 21);
    assertEquals(1, before2.get(0));
    input.position(input.position() + 4);
    ByteBuffer before3 = MPEGUtil.gotoNextMarker(input);
    assertArrayEquals(new byte[] { 6, 7, 8, 9, 10 }, NIOUtils.toArray(before3));
    assertEquals(input.position(), 30);
    assertEquals(6, before3.get(0));
    input.position(input.position() + 4);
    ByteBuffer before4 = MPEGUtil.gotoNextMarker(input);
    assertArrayEquals(new byte[] { 11, 12, 13 }, NIOUtils.toArray(before4));
    assertFalse(input.hasRemaining());
    assertEquals(11, before4.get(0));
}

74. SegmentTypeBoxTest#testParse()

Project: jcodec
Source File: SegmentTypeBoxTest.java
View license
@Test
public void testParse() throws Exception {
    ByteBuffer buf = ByteBuffer.allocate(0x18 + 8);
    buf.putInt(0x18);
    buf.put("stypmsdh".getBytes());
    buf.putInt(0);
    buf.put("msdhmsix".getBytes());
    buf.putInt(8);
    buf.put("free".getBytes());
    buf.clear();
    SeekableByteChannel input = new ByteBufferSeekableByteChannel(buf);
    List<Atom> rootAtoms = MP4Util.getRootAtoms(input);
    assertEquals(2, rootAtoms.size());
    Atom atom = rootAtoms.get(0);
    assertEquals("styp", atom.getHeader().getFourcc());
    Box box = atom.parseBox(input);
    assertTrue(SegmentTypeBox.class.isInstance(box));
    SegmentTypeBox ftyp = (SegmentTypeBox) box;
    assertEquals("msdh", ftyp.getMajorBrand());
    assertArrayEquals(new String[] { "msdh", "msix" }, ftyp.getCompBrands().toArray(new String[0]));
}

75. Draft_75#createBinaryFrame()

Project: jsonapi
Source File: Draft_75.java
View license
@Override
public ByteBuffer createBinaryFrame(Framedata framedata) {
    if (framedata.getOpcode() != Opcode.TEXT) {
        throw new RuntimeException("only text frames supported");
    }
    ByteBuffer pay = framedata.getPayloadData();
    ByteBuffer b = ByteBuffer.allocate(pay.remaining() + 2);
    b.put(START_OF_FRAME);
    pay.mark();
    b.put(pay);
    pay.reset();
    b.put(END_OF_FRAME);
    b.flip();
    return b;
}

76. Draft_75#createBinaryFrame()

Project: jxm
Source File: Draft_75.java
View license
@Override
public ByteBuffer createBinaryFrame(Framedata framedata) {
    if (framedata.getOpcode() != Opcode.TEXT) {
        throw new RuntimeException("only text frames supported");
    }
    ByteBuffer pay = framedata.getPayloadData();
    ByteBuffer b = ByteBuffer.allocate(pay.remaining() + 2);
    b.put(START_OF_FRAME);
    pay.mark();
    b.put(pay);
    pay.reset();
    b.put(END_OF_FRAME);
    b.flip();
    return b;
}

77. IoBufferTest#testAddMixedOrderBuffers()

Project: mina
Source File: IoBufferTest.java
View license
/**
     * Test the addition of mixed order buffers
     */
@Test
public void testAddMixedOrderBuffers() {
    ByteBuffer bb1 = ByteBuffer.allocate(5);
    bb1.order(ByteOrder.LITTLE_ENDIAN);
    bb1.put("012".getBytes());
    bb1.flip();
    ByteBuffer bb2 = ByteBuffer.allocateDirect(5);
    bb1.order(ByteOrder.BIG_ENDIAN);
    bb2.put("3456".getBytes());
    bb2.flip();
    IoBuffer ioBuffer = IoBuffer.newInstance();
    ioBuffer.add(bb1, bb2);
}

78. IoBufferTest#testGetInt2Ints2BBsLittleIndian()

Project: mina
Source File: IoBufferTest.java
View license
/**
     * Test the getInt() method, on a buffer containing 2 ints in two
     * ByteBuffers with LittleInidan order
     */
@Test
public void testGetInt2Ints2BBsLittleIndian() {
    ByteBuffer bb1 = ByteBuffer.allocate(4);
    bb1.order(ByteOrder.LITTLE_ENDIAN);
    bb1.putInt(12345);
    bb1.flip();
    ByteBuffer bb2 = ByteBuffer.allocate(4);
    bb2.order(ByteOrder.LITTLE_ENDIAN);
    bb2.putInt(67890);
    bb2.flip();
    IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2);
    ioBuffer.order(ByteOrder.LITTLE_ENDIAN);
    assertEquals(12345, ioBuffer.getInt());
    assertEquals(67890, ioBuffer.getInt());
}

79. BlobStoreHardDelete#toBytes()

Project: ambry
Source File: BlobStoreHardDelete.java
View license
byte[] toBytes() {
    // create a byte array to hold the headerVersion + userMetadataVersion + userMetadataSize + blobRecordVersion +
    // blobType + blobRecordSize + storeKey.
    byte[] bytes = new byte[MessageFormatRecord.Version_Field_Size_In_Bytes + MessageFormatRecord.Version_Field_Size_In_Bytes + Integer.SIZE / 8 + MessageFormatRecord.Version_Field_Size_In_Bytes + (blobRecordVersion == MessageFormatRecord.Blob_Version_V2 ? (Short.SIZE / 8) : 0) + Long.SIZE / 8 + storeKey.sizeInBytes()];
    ByteBuffer bufWrap = ByteBuffer.wrap(bytes);
    bufWrap.putShort(headerVersion);
    bufWrap.putShort(userMetadataVersion);
    bufWrap.putInt(userMetadataSize);
    bufWrap.putShort(blobRecordVersion);
    if (blobRecordVersion == MessageFormatRecord.Blob_Version_V2) {
        bufWrap.putShort((short) blobType.ordinal());
    }
    bufWrap.putLong(blobStreamSize);
    bufWrap.put(storeKey.toBytes());
    return bytes;
}

80. DefaultConsumablePartition#toBytes()

View license
public byte[] toBytes() {
    byte[] partitionKeyBytes = Bytes.toBytes(GSON.toJson(partitionKey));
    // 1 byte for the ProcessState
    int numBytes = 1;
    numBytes += Bytes.SIZEOF_INT;
    numBytes += partitionKeyBytes.length;
    numBytes += Bytes.SIZEOF_LONG;
    numBytes += Bytes.SIZEOF_INT;
    ByteBuffer bb = ByteBuffer.allocate(numBytes);
    bb.put(processState.toByte());
    bb.putInt(partitionKeyBytes.length);
    bb.put(partitionKeyBytes);
    bb.putLong(getTimestamp());
    bb.putInt(getNumFailures());
    return bb.array();
}

81. Transaction#serialize()

Project: concourse
Source File: Transaction.java
View license
/**
     * Serialize the Transaction to a ByteBuffer.
     * <ol>
     * <li><strong>lockSize</strong> - position 0</li>
     * <li><strong>locks</strong> - position 4</li>
     * <li><strong>writes</strong> - position 4 + lockSize</li>
     * </ol>
     * 
     * @return the ByteBuffer representation
     */
private ByteBuffer serialize() {
    ByteBuffer _locks = ByteableCollections.toByteBuffer(locks.values());
    ByteBuffer _writes = ByteableCollections.toByteBuffer(((Queue) buffer).getWrites());
    ByteBuffer bytes = ByteBuffer.allocate(4 + _locks.capacity() + _writes.capacity());
    bytes.putInt(_locks.capacity());
    bytes.put(_locks);
    bytes.put(_writes);
    bytes.rewind();
    return bytes;
}

82. PNG#getPNGBytesForSize()

Project: Direct-File-Downloader
Source File: PNG.java
View license
public static byte[] getPNGBytesForSize(int width, int height) {
    byte[] signature = new PngSignatureChunk().getChunkPayload();
    byte[] ihdr = new IHDRChunk(width, height).getChunkPayload();
    byte[] idat = new IDATChunk(width, height).getChunkPayload();
    byte[] iend = new IENDChunk().getChunkPayload();
    ByteBuffer buffer = ByteBuffer.allocate(signature.length + ihdr.length + idat.length + iend.length);
    buffer.put(signature);
    buffer.put(ihdr);
    buffer.put(idat);
    buffer.put(iend);
    buffer.position(0);
    return buffer.array();
}

83. BaseKeyValueOperationTest#testDecode()

View license
@Test
public void testDecode() throws Exception {
    Long key = 12L;
    String value = "The value";
    ByteBuffer blob = ByteBuffer.allocate(BYTE_SIZE_BYTES + INT_SIZE_BYTES + 2 * LONG_SIZE_BYTES + value.length());
    blob.put(getOperationCode().getValue());
    blob.putLong(TIME_SOURCE.getTimeMillis());
    blob.putInt(LONG_SIZE_BYTES);
    blob.putLong(key);
    blob.put(value.getBytes());
    blob.flip();
    BaseKeyValueOperation<Long, String> operation = getNewOperation(blob, keySerializer, valueSerializer);
    assertEquals(getOperationCode(), operation.getOpCode());
    assertEquals(key, operation.getKey());
    assertEquals(value, operation.getValue());
}

84. EPeer#dsig_cast()

Project: erjang
Source File: EPeer.java
View license
void dsig_cast(EHandle sender, ETuple hdr) throws Pausable {
    ByteBuffer disthdr = ByteBuffer.allocate(3);
    disthdr.put((byte) 131);
    disthdr.put((byte) 68);
    disthdr.put((byte) 0);
    disthdr.flip();
    EOutputStream eos = new EOutputStream(1024, flags);
    hdr.encode(eos);
    ByteBuffer barr = eos.toByteBuffer();
    ByteBuffer[] ev = new ByteBuffer[] { disthdr, barr };
    send_to_port(ev, sender);
}

85. EPeer#dsig_cast()

Project: erjang
Source File: EPeer.java
View license
int dsig_cast(EHandle sender, ETuple hdr, EObject payload) throws Pausable {
    ByteBuffer disthdr = ByteBuffer.allocate(3);
    disthdr.put((byte) 131);
    disthdr.put((byte) 68);
    disthdr.put((byte) 0);
    disthdr.flip();
    EOutputStream eos = new EOutputStream(1024, flags);
    hdr.encode(eos);
    payload.encode(eos);
    ByteBuffer barr = eos.toByteBuffer();
    ByteBuffer[] ev = new ByteBuffer[] { disthdr, barr };
    send_to_port(ev, sender);
    return eos.size();
}

86. MVStoreJournal#record()

Project: es4j
Source File: MVStoreJournal.java
View license
@Override
public Command record(AbstractJournal.Transaction tx, Command<?, ?> command) {
    TransactionStore.Transaction tx0 = ((Transaction) tx).getTx();
    TransactionMap<UUID, ByteBuffer> txCommandPayloads = tx0.openMap("commandPayloads", new ObjectDataType(), new ByteBufferDataType());
    TransactionMap<byte[], Boolean> txHashCommands = tx0.openMap("hashCommands");
    TransactionMap<UUID, byte[]> txCommandHashes = tx0.openMap("commandHashes");
    Layout commandLayout = getLayout(command.getClass());
    // based on SHA-1
    ByteBuffer hashBuffer = ByteBuffer.allocate(16 + 20);
    hashBuffer.put(commandLayout.getHash());
    hashBuffer.putLong(command.uuid().getMostSignificantBits());
    hashBuffer.putLong(command.uuid().getLeastSignificantBits());
    ByteBuffer buffer = serialization.getSerializer(command.getClass()).serialize(command);
    buffer.rewind();
    txCommandPayloads.tryPut(command.uuid(), buffer);
    txHashCommands.tryPut(hashBuffer.array(), true);
    txCommandHashes.tryPut(command.uuid(), commandLayout.getHash());
    buffer.rewind();
    Command command1 = (Command) serialization.getDeserializer(command.getClass()).deserialize(buffer);
    command1.uuid(command.uuid());
    return command1;
}

87. PebbleProtocol#encodeAppReorder()

Project: Gadgetbridge
Source File: PebbleProtocol.java
View license
@Override
public byte[] encodeAppReorder(UUID[] uuids) {
    int length = 2 + uuids.length * LENGTH_UUID;
    ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + length);
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putShort((short) length);
    buf.putShort(ENDPOINT_APPREORDER);
    buf.put((byte) 0x01);
    buf.put((byte) uuids.length);
    for (UUID uuid : uuids) {
        buf.putLong(uuid.getMostSignificantBits());
        buf.putLong(uuid.getLeastSignificantBits());
    }
    return buf.array();
}

88. TestImportExport#testImportExportWKBPolyline()

View license
@Test
public static void testImportExportWKBPolyline() {
    OperatorExportToWkb exporterWKB = (OperatorExportToWkb) OperatorFactoryLocal.getInstance().getOperator(Operator.Type.ExportToWkb);
    OperatorExportToWkt exporterWKT = (OperatorExportToWkt) OperatorFactoryLocal.getInstance().getOperator(Operator.Type.ExportToWkt);
    OperatorImportFromWkb importerWKB = (OperatorImportFromWkb) OperatorFactoryLocal.getInstance().getOperator(Operator.Type.ImportFromWkb);
    // Test Import Polyline with bad paths (i.e. paths with one point or
    // zero points)
    int offset = 0;
    ByteBuffer wkbBuffer = ByteBuffer.allocate(500).order(ByteOrder.nativeOrder());
    wkbBuffer.put(offset, (byte) WkbByteOrder.wkbNDR);
    // byte order
    offset += 1;
    wkbBuffer.putInt(offset, WkbGeometryType.wkbMultiLineString);
    // type
    offset += 4;
    wkbBuffer.putInt(offset, 4);
    // num paths
    offset += 4;
    wkbBuffer.put(offset, (byte) WkbByteOrder.wkbNDR);
    // byte order
    offset += 1;
    wkbBuffer.putInt(offset, WkbGeometryType.wkbLineString);
    // type
    offset += 4;
    wkbBuffer.putInt(offset, 1);
    // num points
    offset += 4;
    wkbBuffer.putDouble(offset, 36.0);
    // x
    offset += // x
    8;
    wkbBuffer.putDouble(offset, 17.0);
    // y
    offset += // y
    8;
    wkbBuffer.put(offset, (byte) WkbByteOrder.wkbNDR);
    // byte order
    offset += 1;
    wkbBuffer.putInt(offset, WkbGeometryType.wkbLineString);
    // type
    offset += 4;
    wkbBuffer.putInt(offset, 0);
    // num points
    offset += 4;
    wkbBuffer.put(offset, (byte) WkbByteOrder.wkbNDR);
    // byte order
    offset += 1;
    wkbBuffer.putInt(offset, WkbGeometryType.wkbLineString);
    // type
    offset += 4;
    wkbBuffer.putInt(offset, 1);
    // num points
    offset += 4;
    wkbBuffer.putDouble(offset, 19.0);
    // x
    offset += // x
    8;
    wkbBuffer.putDouble(offset, 19.0);
    // y
    offset += // y
    8;
    wkbBuffer.put(offset, (byte) WkbByteOrder.wkbNDR);
    // byte order
    offset += 1;
    wkbBuffer.putInt(offset, WkbGeometryType.wkbLineString);
    // type
    offset += 4;
    wkbBuffer.putInt(offset, 3);
    // num points
    offset += 4;
    wkbBuffer.putDouble(offset, 88);
    // x
    offset += // x
    8;
    wkbBuffer.putDouble(offset, 29.0);
    // y
    offset += // y
    8;
    wkbBuffer.putDouble(offset, 13.0);
    // x
    offset += // x
    8;
    wkbBuffer.putDouble(offset, 43.0);
    // y
    offset += // y
    8;
    wkbBuffer.putDouble(offset, 59.0);
    // x
    offset += // x
    8;
    wkbBuffer.putDouble(offset, 88);
    // y
    offset += // y
    8;
    Polyline p = (Polyline) (importerWKB.execute(0, Geometry.Type.Polyline, wkbBuffer, null));
    int pc = p.getPointCount();
    int pac = p.getPathCount();
    assertTrue(p.getPointCount() == 7);
    assertTrue(p.getPathCount() == 3);
    String wktString = exporterWKT.execute(0, p, null);
    assertTrue(wktString.equals("MULTILINESTRING ((36 17, 36 17), (19 19, 19 19), (88 29, 13 43, 59 88))"));
    Polyline polyline = makePolyline();
    polyline.dropAttribute(VertexDescription.Semantics.ID);
    // Test Import Polyline from Polyline
    ByteBuffer polylineWKBBuffer = exporterWKB.execute(0, polyline, null);
    int wkbType = polylineWKBBuffer.getInt(1);
    assertTrue(wkbType == WkbGeometryType.wkbMultiLineStringZM);
    Geometry polylineWKBGeometry = importerWKB.execute(0, Geometry.Type.Polyline, polylineWKBBuffer, null);
    TestCommonMethods.compareGeometryContent((MultiVertexGeometry) polylineWKBGeometry, polyline);
    // Test wkbExportMultiPolyline on nonempty single part polyline
    Polyline polyline2 = makePolyline2();
    assertTrue(polyline2.getPathCount() == 1);
    polylineWKBBuffer = exporterWKB.execute(WkbExportFlags.wkbExportMultiLineString, polyline2, null);
    polylineWKBGeometry = importerWKB.execute(0, Geometry.Type.Polyline, polylineWKBBuffer, null);
    TestCommonMethods.compareGeometryContent((MultiVertexGeometry) polylineWKBGeometry, polyline2);
    wkbType = polylineWKBBuffer.getInt(1);
    assertTrue(wkbType == WkbGeometryType.wkbMultiLineStringZM);
    // Test wkbExportPolyline on nonempty single part polyline
    assertTrue(polyline2.getPathCount() == 1);
    polylineWKBBuffer = exporterWKB.execute(WkbExportFlags.wkbExportLineString, polyline2, null);
    polylineWKBGeometry = importerWKB.execute(0, Geometry.Type.Polyline, polylineWKBBuffer, null);
    TestCommonMethods.compareGeometryContent((MultiVertexGeometry) polylineWKBGeometry, polyline2);
    wkbType = polylineWKBBuffer.getInt(1);
    assertTrue(wkbType == WkbGeometryType.wkbLineStringZM);
    // Test wkbExportPolyline on empty polyline
    Polyline polyline3 = new Polyline();
    polylineWKBBuffer = exporterWKB.execute(WkbExportFlags.wkbExportLineString, polyline3, null);
    polylineWKBGeometry = importerWKB.execute(0, Geometry.Type.Polyline, polylineWKBBuffer, null);
    assertTrue(polylineWKBGeometry.isEmpty() == true);
    wkbType = polylineWKBBuffer.getInt(1);
    assertTrue(wkbType == WkbGeometryType.wkbLineString);
    // Test WKB_export_defaults on empty polyline
    polylineWKBBuffer = exporterWKB.execute(0, polyline3, null);
    polylineWKBGeometry = importerWKB.execute(0, Geometry.Type.Polyline, polylineWKBBuffer, null);
    assertTrue(polylineWKBGeometry.isEmpty() == true);
    wkbType = polylineWKBBuffer.getInt(1);
    assertTrue(wkbType == WkbGeometryType.wkbMultiLineString);
}

89. SecondaryIndex#toBinary()

Project: geowave
Source File: SecondaryIndex.java
View license
@Override
public byte[] toBinary() {
    final byte[] indexStrategyBinary = PersistenceUtils.toBinary(indexStrategy);
    final byte[] fieldIdBinary = ByteArrayId.toBytes(fieldIDs);
    final List<Persistable> persistables = new ArrayList<Persistable>();
    for (DataStatistics<T> dataStatistics : associatedStatistics) {
        persistables.add(dataStatistics);
    }
    final byte[] persistablesBinary = PersistenceUtils.toBinary(persistables);
    final ByteBuffer buf = ByteBuffer.allocate(indexStrategyBinary.length + fieldIdBinary.length + 8 + persistablesBinary.length);
    buf.putInt(indexStrategyBinary.length);
    buf.putInt(fieldIdBinary.length);
    buf.put(indexStrategyBinary);
    buf.put(fieldIdBinary);
    buf.put(persistablesBinary);
    return buf.array();
}

90. LikeFilter#toBinary()

Project: geowave
Source File: LikeFilter.java
View license
@Override
public byte[] toBinary() {
    final byte[] expressionBytes = StringUtils.stringToBinary(expression);
    final ByteBuffer bb = ByteBuffer.allocate(4 + expressionBytes.length + 4 + fieldId.getBytes().length + 4);
    bb.putInt(expressionBytes.length);
    bb.put(expressionBytes);
    bb.putInt(fieldId.getBytes().length);
    bb.put(fieldId.getBytes());
    final int caseSensitiveVal = caseSensitive ? 1 : 0;
    bb.putInt(caseSensitiveVal);
    return bb.array();
}

91. RasterDataAdapter#toBinary()

Project: geowave
Source File: RasterDataAdapter.java
View license
@Override
public byte[] toBinary() {
    final byte[] coverageNameBytes = StringUtils.stringToBinary(coverageName);
    final byte[] sampleModelBinary = getSampleModelBinary(sampleModel);
    final byte[] colorModelBinary = getColorModelBinary(colorModel);
    int metadataBinaryLength = 4;
    final List<byte[]> entryBinaries = new ArrayList<byte[]>();
    for (final Entry<String, String> e : metadata.entrySet()) {
        final byte[] keyBytes = StringUtils.stringToBinary(e.getKey());
        final byte[] valueBytes = StringUtils.stringToBinary(e.getValue());
        final int entryBinaryLength = 4 + valueBytes.length + keyBytes.length;
        final ByteBuffer buf = ByteBuffer.allocate(entryBinaryLength);
        buf.putInt(keyBytes.length);
        buf.put(keyBytes);
        buf.put(valueBytes);
        entryBinaries.add(buf.array());
        metadataBinaryLength += (entryBinaryLength + 4);
    }
    byte[] histogramConfigBinary;
    if (histogramConfig != null) {
        histogramConfigBinary = PersistenceUtils.toBinary(histogramConfig);
    } else {
        histogramConfigBinary = new byte[] {};
    }
    final byte[] noDataBinary = getNoDataBinary(noDataValuesPerBand);
    final byte[] backgroundBinary;
    if (backgroundValuesPerBand != null) {
        final int totalBytes = (backgroundValuesPerBand.length * 8);
        final ByteBuffer backgroundBuf = ByteBuffer.allocate(totalBytes);
        for (final double backgroundValue : backgroundValuesPerBand) {
            backgroundBuf.putDouble(backgroundValue);
        }
        backgroundBinary = backgroundBuf.array();
    } else {
        backgroundBinary = new byte[] {};
    }
    final byte[] minsBinary;
    if (minsPerBand != null) {
        final int totalBytes = (minsPerBand.length * 8);
        final ByteBuffer minsBuf = ByteBuffer.allocate(totalBytes);
        for (final double min : minsPerBand) {
            minsBuf.putDouble(min);
        }
        minsBinary = minsBuf.array();
    } else {
        minsBinary = new byte[] {};
    }
    final byte[] maxesBinary;
    if (maxesPerBand != null) {
        final int totalBytes = (maxesPerBand.length * 8);
        final ByteBuffer maxesBuf = ByteBuffer.allocate(totalBytes);
        for (final double max : maxesPerBand) {
            maxesBuf.putDouble(max);
        }
        maxesBinary = maxesBuf.array();
    } else {
        maxesBinary = new byte[] {};
    }
    final byte[] namesBinary;
    final int namesLength;
    if (namesPerBand != null) {
        int totalBytes = 0;
        final List<byte[]> namesBinaries = new ArrayList<byte[]>(namesPerBand.length);
        for (final String name : namesPerBand) {
            final byte[] nameBinary = StringUtils.stringToBinary(name);
            final int size = nameBinary.length + 4;
            final ByteBuffer nameBuf = ByteBuffer.allocate(size);
            totalBytes += size;
            nameBuf.putInt(nameBinary.length);
            nameBuf.put(nameBinary);
            namesBinaries.add(nameBuf.array());
        }
        final ByteBuffer namesBuf = ByteBuffer.allocate(totalBytes);
        for (final byte[] nameBinary : namesBinaries) {
            namesBuf.put(nameBinary);
        }
        namesBinary = namesBuf.array();
        namesLength = namesPerBand.length;
    } else {
        namesBinary = new byte[] {};
        namesLength = 0;
    }
    byte[] mergeStrategyBinary;
    if (mergeStrategy != null) {
        mergeStrategyBinary = PersistenceUtils.toBinary(mergeStrategy);
    } else {
        mergeStrategyBinary = new byte[] {};
    }
    final ByteBuffer buf = ByteBuffer.allocate(coverageNameBytes.length + sampleModelBinary.length + colorModelBinary.length + metadataBinaryLength + histogramConfigBinary.length + noDataBinary.length + minsBinary.length + maxesBinary.length + namesBinary.length + backgroundBinary.length + mergeStrategyBinary.length + 47);
    buf.putInt(tileSize);
    buf.putInt(coverageNameBytes.length);
    buf.put(coverageNameBytes);
    buf.putInt(sampleModelBinary.length);
    buf.put(sampleModelBinary);
    buf.putInt(colorModelBinary.length);
    buf.put(colorModelBinary);
    buf.putInt(entryBinaries.size());
    for (final byte[] entryBinary : entryBinaries) {
        buf.putInt(entryBinary.length);
        buf.put(entryBinary);
    }
    buf.putInt(histogramConfigBinary.length);
    buf.put(histogramConfigBinary);
    buf.putInt(noDataBinary.length);
    buf.put(noDataBinary);
    buf.putInt(minsBinary.length);
    buf.put(minsBinary);
    buf.putInt(maxesBinary.length);
    buf.put(maxesBinary);
    buf.putInt(namesLength);
    buf.put(namesBinary);
    buf.putInt(backgroundBinary.length);
    buf.put(backgroundBinary);
    buf.putInt(mergeStrategyBinary.length);
    buf.put(mergeStrategyBinary);
    buf.put(buildPyramid ? (byte) 1 : (byte) 0);
    buf.put(equalizeHistogram ? (byte) 1 : (byte) 0);
    buf.put(interpolationToByte(interpolation));
    return buf.array();
}

92. ServerMapArea#toBinary()

Project: geowave
Source File: ServerMapArea.java
View license
@Override
public byte[] toBinary() {
    // we are assuming the data is transmitted in the default reference
    // system
    final double minX = env.getMinX();
    final double minY = env.getMinY();
    final double maxX = env.getMaxX();
    final double maxY = env.getMaxY();
    final String wkt = env.getCoordinateReferenceSystem().toWKT();
    final byte[] wktBinary = StringUtils.stringToBinary(wkt);
    final ByteBuffer buf = ByteBuffer.allocate(32 + wktBinary.length);
    buf.putDouble(minX);
    buf.putDouble(minY);
    buf.putDouble(maxX);
    buf.putDouble(maxY);
    buf.put(wktBinary);
    return buf.array();
}

93. FeatureNumericHistogramStatistics#toBinary()

View license
@Override
public byte[] toBinary() {
    final int positiveBytes = positiveHistogram.getEstimatedFootprintInBytes();
    final int bytesNeeded = positiveBytes + (negativeHistogram == null ? 0 : negativeHistogram.getEstimatedFootprintInBytes());
    final ByteBuffer buffer = super.binaryBuffer(bytesNeeded + 5);
    final int startPosition = buffer.position();
    // buffer out an int
    buffer.putInt(startPosition);
    positiveHistogram.encodeIntoCompressedByteBuffer(buffer);
    final int endPosition = buffer.position();
    buffer.position(startPosition);
    buffer.putInt(endPosition);
    buffer.position(endPosition);
    if (negativeHistogram != null) {
        buffer.put((byte) 0x01);
        negativeHistogram.encodeIntoCompressedByteBuffer(buffer);
    } else {
        buffer.put((byte) 0x00);
    }
    final byte result[] = new byte[buffer.position() + 1];
    buffer.rewind();
    buffer.get(result);
    return result;
}

94. TcpRestParserSelfTest#clientRequestPacket()

Project: ignite
Source File: TcpRestParserSelfTest.java
View license
/**
     * Assembles Ignite client packet.
     *
     * @param msg Message to serialize.
     * @return Raw message bytes.
     * @throws IOException If serialization failed.
     */
private ByteBuffer clientRequestPacket(GridClientMessage msg) throws IOException {
    ByteBuffer res = marshaller.marshal(msg, 45);
    ByteBuffer slice = res.slice();
    slice.put(IGNITE_REQ_FLAG);
    slice.putInt(res.remaining() - 5);
    slice.putLong(msg.requestId());
    slice.put(U.uuidToBytes(msg.clientId()));
    slice.put(U.uuidToBytes(msg.destinationId()));
    return res;
}

95. BufferTest#testCharBufferSubSequence()

Project: j2objc
Source File: BufferTest.java
View license
public void testCharBufferSubSequence() throws Exception {
    ByteBuffer b = ByteBuffer.allocateDirect(10).order(ByteOrder.nativeOrder());
    b.putChar('H');
    b.putChar('e');
    b.putChar('l');
    b.putChar('l');
    b.putChar('o');
    b.flip();
    assertEquals("Hello", b.asCharBuffer().toString());
    CharBuffer cb = b.asCharBuffer();
    CharSequence cs = cb.subSequence(0, cb.length());
    assertEquals("Hello", cs.toString());
}

96. PagingState#generateCompleteOutput()

Project: java-driver
Source File: PagingState.java
View license
private ByteBuffer generateCompleteOutput() {
    ByteBuffer res = ByteBuffer.allocate(pagingState.length + hash.length + 6);
    res.putShort((short) pagingState.length);
    res.putShort((short) hash.length);
    res.put(pagingState);
    res.put(hash);
    res.putShort((short) protocolVersion.toInt());
    res.rewind();
    return res;
}

97. MKVParser#readEbmlId()

Project: jcodec
Source File: MKVParser.java
View license
/**
     * Reads an EBML id from the channel. EBML ids have length encoded inside of them For instance, all one-byte ids have first byte set to '1', like 0xA3 or 0xE7, whereas the two-byte ids have first
     * byte set to '0' and second byte set to '1', thus: 0x42 0x86  or 0x42 0xF7
     * 
     * @return byte array filled with the ebml id
     * @throws IOException
     */
public static byte[] readEbmlId(SeekableByteChannel source) throws IOException {
    if (source.position() == source.size())
        return null;
    ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.limit(1);
    source.read(buffer);
    buffer.flip();
    byte firstByte = buffer.get();
    int numBytes = EbmlUtil.computeLength(firstByte);
    if (numBytes == 0)
        return null;
    if (numBytes > 1) {
        buffer.limit(numBytes);
        source.read(buffer);
    }
    buffer.flip();
    ByteBuffer val = ByteBuffer.allocate(buffer.remaining());
    val.put(buffer);
    return val.array();
}

98. BinaryEncDecTest#testLogClientSync()

Project: kaa
Source File: BinaryEncDecTest.java
View license
@Test
public void testLogClientSync() throws PlatformEncDecException {
    ByteBuffer buf = ByteBuffer.wrap(new byte[4 + 4 + 128]);
    // user assign request
    buf.putShort(BIG_MAGIC_NUMBER);
    buf.put((byte) 0);
    buf.put((byte) 1);
    buf.putInt(127);
    byte[] logData = new byte[127];
    logData[MAGIC_NUMBER] = MAGIC_NUMBER;
    buf.put(logData);
    buf.put((byte) 0);
    ClientSync sync = encDec.decode(concat(buildHeader(Constants.KAA_PLATFORM_PROTOCOL_BINARY_ID, 1, 2), getValidMetaData(), buildExtensionHeader(BinaryEncDec.LOGGING_EXTENSION_ID, 0, 0, buf.array().length), buf.array()));
    Assert.assertNotNull(sync);
    Assert.assertNotNull(sync.getClientSyncMetaData());
    Assert.assertNotNull(sync.getLogSync());
    LogClientSync logSync = sync.getLogSync();
    Assert.assertEquals(BIG_MAGIC_NUMBER, logSync.getRequestId());
    Assert.assertNotNull(logSync.getLogEntries());
    Assert.assertEquals(1, logSync.getLogEntries().size());
    Assert.assertEquals(MAGIC_NUMBER, logSync.getLogEntries().get(0).getData().array()[MAGIC_NUMBER]);
}

99. ByteBufferVol#putSixLong()

Project: mapdb
Source File: ByteBufferVol.java
View license
@Override
public void putSixLong(long pos, long value) {
    final ByteBuffer b = getSlice(pos);
    int bpos = (int) (pos & sliceSizeModMask);
    if (CC.ASSERT && (value >>> 48 != 0))
        throw new DBException.DataCorruption("six long out of range");
    b.put(bpos++, (byte) (0xff & (value >> 40)));
    b.put(bpos++, (byte) (0xff & (value >> 32)));
    b.put(bpos++, (byte) (0xff & (value >> 24)));
    b.put(bpos++, (byte) (0xff & (value >> 16)));
    b.put(bpos++, (byte) (0xff & (value >> 8)));
    b.put(bpos, (byte) (0xff & (value)));
}

100. MessageIteratorUnitTest#testNext_NoAttribute()

View license
@Test
public void testNext_NoAttribute() throws Exception {
    final ByteBuffer buf = ByteBuffer.allocate(MessageUtils.HEADER_LEN + 5);
    // msg length
    buf.putInt(5);
    // checksum
    buf.putInt(CheckSum.crc32("hello".getBytes()));
    // id
    buf.putLong(9999);
    // flag
    buf.putInt(0);
    buf.position(MessageUtils.HEADER_LEN);
    buf.put("hello".getBytes());
    this.it = new MessageIterator("test", buf.array());
    assertTrue(this.it.hasNext());
    final Message msg = this.it.next();
    assertNotNull(msg);
    assertEquals(9999L, msg.getId());
    assertEquals("test", msg.getTopic());
    assertFalse(msg.hasAttribute());
    assertEquals(0, MessageAccessor.getFlag(msg));
    assertEquals("hello", new String(msg.getData()));
}