Here are the examples of the java api class java.nio.ByteBuffer taken from open source projects.
1. ClusterMetricsSnapshot#serialize()
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()
View licensepublic 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()
View licensepublic 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()
View licensepublic 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()
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()
View licenseprivate 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()
View licensepublic 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()
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. IPv4Test#setUp()
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(); }
11. PebbleProtocol#encodeInstallMetadata()
View licensepublic 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()); }
12. PebbleProtocol#encodePhoneVersion2x()
View licenseprivate 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(); }
13. 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; }
14. ProxyLoginHandler#createSocks4aMessage()
View licenseprivate 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) }; }
15. BaseBinaryCommandUnitTest#constructResponse()
View licensepublic 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. FastLeaderElection#buildMsg()
View licensestatic 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; }
17. ProxyLoginHandler#createSocks4Message()
View licenseprivate 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) }; }
18. CRCedChunk#getChunkPayload()
View licensepublic 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(); }
19. BTreeBlockFinderTest#getFileMatadataWithOnlyNoDictionaryKey()
View licenseprivate 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; }
20. BTreeBlockFinderTest#getFileFooterWithOnlyDictionaryKey()
View licenseprivate 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; }
21. BTreeBlockFinderTest#getFileMatadataWithOnlyNoDictionaryKey()
View licenseprivate 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; }
22. BTreeBlockFinderTest#getFileFooterWithOnlyDictionaryKey()
View licenseprivate 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; }
23. 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()); }
24. ByteBufferDataTypeTest#testWrite()
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); }
25. ARP#serialize()
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; }
26. HyperLogLogCollectorTest#makeCollectorBuffer()
View licenseprivate 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(); }
27. SpatialQueryFilter#toBinary()
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(); }
28. ArpTest#setUp()
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(); }
29. ARP#serialize()
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; }
30. IntroductionProcessor#createIntroductionBuffer()
View licenseprivate 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; }
31. CassandraKeyValueServices#makeCompositeBuffer()
View licensestatic 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; }
32. IntroductionProcessor#createIntroductionBuffer()
View licenseprivate 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; }
33. DigestManager#computeDigestAndPackageForSending()
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)); }
34. 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)); } }
35. 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); }
36. ALPNHackServerHelloExplorer#createNewOutputRecords()
View licensestatic 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; }
37. GenerateCPPTestFiles#writeServerCallResponse()
View licenseprivate 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); }
38. GenerateCPPTestFiles#writeServerAuthenticationResponse()
View licenseprivate 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); }
39. 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); }
40. RCONPacket#toBuffer()
View licensepublic 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; }
41. CompressedResourceHeader#getBytes()
View licensepublic 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(); }
42. IoBufferTest#testPositionBuffer()
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(); } }
43. IoBufferTest#testSetPositionBuffer()
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()); } }
44. IoBufferTest#testPositionNegativeValue()
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); }
45. IoBufferTest#testPositionAboveValue()
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); }
46. ConsistentHashResolverTest#createNodeListWithThreeItems()
View licenseprivate 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); }
47. PebbleProtocol#encodeBlobDBClear()
View licenseprivate 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(); }
48. PebbleProtocol#encodeApplicationMessageAck()
View licensebyte[] 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(); }
49. BaseKeyValueOperation#encode()
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; }
50. 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()); }
51. 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; }
52. PsshAtomUtil#buildPsshAtom()
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(); }
53. TextRangeFilter#toBinary()
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(); }
54. ChannelStatusObject#getData()
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(); }
55. RconHandler#assemblePacket()
View licenseprivate 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; }
56. 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()); }
57. ID3v2ChapterFrameData#packFrameData()
View licenseprotected 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(); }
58. ClusterMessage#getBytes()
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(); }
59. Draft_75#createBinaryFrame()
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; }
60. Http2Frame#toBuffer()
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; }
61. IoBufferTest#testGetInt2Ints2BBsLittleIndian()
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()); }
62. IoBufferTest#testAddMixedOrderBuffers()
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); }
63. PubkeyChannelObject#getData()
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(); }
64. AcceptMessage#toByteArray()
View licensepublic 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; }
65. FastLeaderElection#buildMsg()
View licensestatic 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. ForeignHost#sendPoisonPill()
View licensepublic 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); }
67. TestBinaryClient#createPacket()
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; }
68. DateRangeFilter#toBinary()
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. SegmentTypeBoxTest#testParse()
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])); }
70. MPEGUtilTest#testGotoNextMarker()
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)); }
71. Draft_75#createBinaryFrame()
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; }
72. Draft_75#createBinaryFrame()
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; }
73. 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()); }
74. 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(); }
75. HBaseUtils#merge()
View licenseprivate 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(); }
76. AccumuloDataStore#getRowIdBytes()
View licenseprotected 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(); }
77. DataStoreUtils#merge()
View licenseprivate 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(); }
78. TestPicoNetwork#testLargeMessages()
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); } }
79. PNG#getPNGBytesForSize()
View licensepublic 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(); }
80. PagingState#generateCompleteOutput()
View licenseprivate 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; }
81. MKVParser#readEbmlId()
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(); }
82. BinaryEncDecTest#testLogClientSync()
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]); }
83. BufferTest#testCharBufferSubSequence()
View licensepublic 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()); }
84. TcpRestParserSelfTest#clientRequestPacket()
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; }
85. BlobStoreHardDelete#toBytes()
View licensebyte[] 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; }
86. AbstractHistogram#toUncompressedBytes()
View licensepublic byte[] toUncompressedBytes() { ByteBuffer buf = ByteBuffer.allocate(8 * countsArrayLength + (3 * 8) + 4); buf.order(ByteOrder.LITTLE_ENDIAN); buf.putLong(lowestDiscernibleValue); buf.putLong(highestTrackableValue); buf.putInt(numberOfSignificantValueDigits); buf.putLong(getTotalCount()); for (int ii = 0; ii < countsArrayLength; ii++) { buf.putLong(getCountAtIndex(ii)); } return buf.array(); }
87. Site#generateDREvent()
View license/** * Generate a in-stream DR event which pushes an event buffer to topend */ public void generateDREvent(EventType type, long uniqueId, long lastCommittedSpHandle, long spHandle, byte[] payloads) { m_ee.quiesce(lastCommittedSpHandle); ByteBuffer paramBuffer = m_ee.getParamBufferForExecuteTask(32 + payloads.length); paramBuffer.putInt(type.ordinal()); paramBuffer.putLong(uniqueId); paramBuffer.putLong(lastCommittedSpHandle); paramBuffer.putLong(spHandle); paramBuffer.putInt(payloads.length); paramBuffer.put(payloads); m_ee.executeTask(TaskType.GENERATE_DR_EVENT, paramBuffer); }
88. DefaultConsumablePartition#toBytes()
View licensepublic 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(); }
89. Transaction#serialize()
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; }
90. 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); }
91. PebbleProtocol#encodeAppReorder()
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(); }
92. 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; }
93. ServerMapArea#toBinary()
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(); }
94. 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())); }
95. UriBeacon#toByteArray()
View license/** * The advertisement data for the UriBeacon as a byte array. * * @return the UriBeacon bytes */ public byte[] toByteArray() { int totalUriBytes = totalBytes(mUriString); if (totalUriBytes == 0) { return null; } ByteBuffer buffer = ByteBuffer.allocateDirect(totalUriBytes); buffer.put(URI_SERVICE_UUID_FIELD); byte[] uriBytes; uriBytes = encodeUri(mUriString); byte length = (byte) (URI_SERVICE_DATA_FIELD_HEADER.length + URI_SERVICE_FLAGS_TXPOWER_SIZE + uriBytes.length); buffer.put(length); buffer.put(URI_SERVICE_DATA_FIELD_HEADER); buffer.put(mFlags); buffer.put(mTxPowerLevel); buffer.put(uriBytes); return byteBufferToArray(buffer); }
96. StpConnection#send()
View license/** * Queues up an STP/1 message sent from another thread and wakes up selector to register it to the * key. * * @param command to add to the request queue */ public void send(Command command) { logger.finest(command.toString()); byte[] payload = command.toByteArray(); // increment 1 for message type int totalSize = payload.length + 1; ByteBuffer outMessageSize = encodeMessageSize(totalSize); // create the message ByteBuffer buffer = ByteBuffer.allocateDirect(prefix.length + outMessageSize.position() + 1 + payload.length); buffer.put(prefix, 0, prefix.length); outMessageSize.flip(); buffer.put(outMessageSize); buffer.put((byte) 1); buffer.put(payload); // Log what is being sent. logger.finest("SEND: " + command); requests.add(buffer); monitor.modify(socketChannel, this, SelectionKey.OP_READ | SelectionKey.OP_WRITE); }
97. MessageUtils#makeMessageBuffer()
View license/** * ?buffer??????£ * <ul> * <li>message length(4 bytes),including attribute and payload</li> * <li>checksum(4 bytes)</li> * <li>message id(8 bytes)</li> * <li>message flag(4 bytes)</li> * <li>attribute length(4 bytes) + attribute,optional</li> * <li>payload</li> * </ul> * * @param req * @return */ public static final ByteBuffer makeMessageBuffer(final long msgId, final PutCommand req) { // message length + checksum + id +flag + data final ByteBuffer buffer = ByteBuffer.allocate(4 + 4 + 8 + 4 + req.getData().length); buffer.putInt(req.getData().length); int checkSum = CheckSum.crc32(req.getData()); // If client passes checksum,compare them if (req.getCheckSum() != -1) { if (checkSum != req.getCheckSum()) { throw new InvalidCheckSumException("Checksum failure,message may be corrupted when transfering on networking."); } } buffer.putInt(checkSum); buffer.putLong(msgId); buffer.putInt(req.getFlag()); buffer.put(req.getData()); buffer.flip(); return buffer; }
98. SimpleFetchManagerUnitTest#makeInvalidMessageBuffer()
View licensepublic static ByteBuffer makeInvalidMessageBuffer(final long msgId, final PutCommand req) { // message length + checksum + id +flag + data final ByteBuffer buffer = ByteBuffer.allocate(4 + 4 + 8 + 4 + req.getData().length); buffer.putInt(req.getData().length); // invalid checksum buffer.putInt(CheckSum.crc32(req.getData()) + 1); buffer.putLong(msgId); buffer.putInt(req.getFlag()); buffer.put(req.getData()); buffer.flip(); return buffer; }
99. ErrorPacket#writeToBytes()
View licensepublic byte[] writeToBytes() { ByteBuffer buffer = ByteBuffer.allocate(calcPacketSize() + 4); int size = calcPacketSize(); BufferUtil.writeUB3(buffer, size); buffer.put(packetId); buffer.put(fieldCount); BufferUtil.writeUB2(buffer, errno); buffer.put(mark); buffer.put(sqlState); if (message != null) { buffer.put(message); } buffer.flip(); byte[] data = new byte[buffer.limit()]; buffer.get(data); return data; }
100. RasterDataAdapter#toBinary()
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(); }