本文整理汇总了Java中java.nio.ByteBuffer.putInt方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBuffer.putInt方法的具体用法?Java ByteBuffer.putInt怎么用?Java ByteBuffer.putInt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.ByteBuffer
的用法示例。
在下文中一共展示了ByteBuffer.putInt方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: iteratorRaisesOnTooSmallRecords
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Test(expected = CorruptRecordException.class)
public void iteratorRaisesOnTooSmallRecords() throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(1024);
MemoryRecordsBuilder builder = MemoryRecords.builder(buffer, CompressionType.NONE, TimestampType.CREATE_TIME, 0L);
builder.append(15L, "a".getBytes(), "1".getBytes());
builder.append(20L, "b".getBytes(), "2".getBytes());
builder.close();
int position = buffer.position();
builder = MemoryRecords.builder(buffer, CompressionType.NONE, TimestampType.CREATE_TIME, 2L);
builder.append(30L, "c".getBytes(), "3".getBytes());
builder.append(40L, "d".getBytes(), "4".getBytes());
builder.close();
buffer.flip();
buffer.putInt(position + DefaultRecordBatch.LENGTH_OFFSET, 9);
ByteBufferLogInputStream logInputStream = new ByteBufferLogInputStream(buffer, Integer.MAX_VALUE);
assertNotNull(logInputStream.nextBatch());
logInputStream.nextBatch();
}
示例2: headerToBytes
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Set the header into a single byte array.
*
* @return a byte[] representing the header
*/
public byte[] headerToBytes() {
final ByteBuffer buffer = ByteBuffer.allocate(Parameters.BLOCK_HEADER_SIZE);
// insert magic number (4 bytes)
buffer.putInt(Parameters.MAGIC_NUMBER);
// insert reference to previous block (32 bytes)
buffer.put(previousBlock);
// insert Merkle root
buffer.put(merkleRoot); // buffer.put(getTransactionHash());
// insert difficulty (4 bytes)
buffer.putInt(difficulty);
// No need to store the number of transactions in byte representation
// insert nonce (4 bytes)
buffer.putInt(nonce);
return buffer.array();
}
示例3: writeEntries
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private int writeEntries(DataOutput payload, ByteBuffer offsets, boolean shrink)
throws IOException {
int entryOffset = 0;
for (int i = 0; i < entryCount; ++i) {
Entry entry = entries.get(i);
if (entry == null) {
offsets.putInt(Entry.NO_ENTRY);
} else {
byte[] encodedEntry = entry.toByteArray(shrink);
payload.write(encodedEntry);
offsets.putInt(entryOffset);
entryOffset += encodedEntry.length;
}
}
entryOffset = writePad(payload, entryOffset);
return entryOffset;
}
示例4: buildSubMessageHeader
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private boolean buildSubMessageHeader(MPIBuffer buffer, int length) {
ByteBuffer byteBuffer = buffer.getByteBuffer();
if (byteBuffer.remaining() < 4) {
return false;
}
// LOG.info(String.format("%d adding sub-header pos: %d", executor, byteBuffer.position()));
byteBuffer.putInt(length);
return true;
}
示例5: calculateChunkedSums
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Calculate checksums for the given data.
*
* The 'mark' of the ByteBuffer parameters may be modified by this function,
* but the position is maintained.
*
* @param data the DirectByteBuffer pointing to the data to checksum.
* @param checksums the DirectByteBuffer into which checksums will be
* stored. Enough space must be available in this
* buffer to put the checksums.
*/
public void calculateChunkedSums(ByteBuffer data, ByteBuffer checksums) {
if (type.size == 0) return;
if (data.hasArray() && checksums.hasArray()) {
calculateChunkedSums(data.array(), data.arrayOffset() + data.position(), data.remaining(),
checksums.array(), checksums.arrayOffset() + checksums.position());
return;
}
if (NativeCrc32.isAvailable()) {
NativeCrc32.calculateChunkedSums(bytesPerChecksum, type.id,
checksums, data);
return;
}
data.mark();
checksums.mark();
try {
byte[] buf = new byte[bytesPerChecksum];
while (data.remaining() > 0) {
int n = Math.min(data.remaining(), bytesPerChecksum);
data.get(buf, 0, n);
summer.reset();
summer.update(buf, 0, n);
checksums.putInt((int)summer.getValue());
}
} finally {
data.reset();
checksums.reset();
}
}
示例6: fillPreBlank
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private void fillPreBlank(final MappedFile mappedFile, final long untilWhere) {
ByteBuffer byteBuffer = ByteBuffer.allocate(CQ_STORE_UNIT_SIZE);
byteBuffer.putLong(0L);
byteBuffer.putInt(Integer.MAX_VALUE);
byteBuffer.putLong(0L);
int until = (int) (untilWhere % this.mappedFileQueue.getMappedFileSize());
for (int i = 0; i < until; i += CQ_STORE_UNIT_SIZE) {
mappedFile.appendMessage(byteBuffer.array());
}
}
示例7: runTest1
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private void runTest1(Table hTable) throws IOException {
// [0, 2, ?, ?, ?, ?, 0, 0, 0, 1]
byte[] mask = new byte[] { 0, 0, 1, 1, 1, 1, 0, 0, 0, 0 };
List<Pair<byte[], byte[]>> list = new ArrayList<Pair<byte[], byte[]>>();
for (int i = 0; i < totalFuzzyKeys; i++) {
byte[] fuzzyKey = new byte[10];
ByteBuffer buf = ByteBuffer.wrap(fuzzyKey);
buf.clear();
buf.putShort((short) 2);
for (int j = 0; j < 4; j++) {
buf.put(fuzzyValue);
}
buf.putInt(i);
Pair<byte[], byte[]> pair = new Pair<byte[], byte[]>(fuzzyKey, mask);
list.add(pair);
}
int expectedSize = secondPartCardinality * totalFuzzyKeys * colQualifiersTotal;
FuzzyRowFilter fuzzyRowFilter0 = new FuzzyRowFilter(list);
// Filters are not stateless - we can't reuse them
FuzzyRowFilter fuzzyRowFilter1 = new FuzzyRowFilter(list);
// regular test
runScanner(hTable, expectedSize, fuzzyRowFilter0);
// optimized from block cache
runScanner(hTable, expectedSize, fuzzyRowFilter1);
}
示例8: createUniqIDBuffer
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private static byte[] createUniqIDBuffer() {
ByteBuffer buffer = ByteBuffer.allocate(4 + 2);
long current = System.currentTimeMillis();
if (current >= nextStartTime) {
setStartTime(current);
}
buffer.position(0);
buffer.putInt((int) (System.currentTimeMillis() - startTime));
buffer.putShort((short) COUNTER.getAndIncrement());
return buffer.array();
}
示例9: testPacketSizeTooBig
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Tests that packets that are too big during Kafka SASL handshake request flow
* or the actual SASL authentication flow result in authentication failure
* and do not cause any failures in the server.
*/
@Test
public void testPacketSizeTooBig() throws Exception {
SecurityProtocol securityProtocol = SecurityProtocol.SASL_PLAINTEXT;
configureMechanisms("PLAIN", Arrays.asList("PLAIN"));
server = createEchoServer(securityProtocol);
// Send SASL packet with large size after valid handshake request
String node1 = "invalid1";
createClientConnection(SecurityProtocol.PLAINTEXT, node1);
sendHandshakeRequestReceiveResponse(node1);
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.putInt(Integer.MAX_VALUE);
buffer.put(new byte[buffer.capacity() - 4]);
buffer.rewind();
selector.send(new NetworkSend(node1, buffer));
NetworkTestUtils.waitForChannelClose(selector, node1, ChannelState.READY);
selector.close();
// Test good connection still works
createAndCheckClientConnection(securityProtocol, "good1");
// Send packet with large size before handshake request
String node2 = "invalid2";
createClientConnection(SecurityProtocol.PLAINTEXT, node2);
buffer.clear();
buffer.putInt(Integer.MAX_VALUE);
buffer.put(new byte[buffer.capacity() - 4]);
buffer.rewind();
selector.send(new NetworkSend(node2, buffer));
NetworkTestUtils.waitForChannelClose(selector, node2, ChannelState.READY);
selector.close();
// Test good connection still works
createAndCheckClientConnection(securityProtocol, "good2");
}
示例10: recordsWithInvalidRecordCount
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private static DefaultRecordBatch recordsWithInvalidRecordCount(Byte magicValue, long timestamp,
CompressionType codec, int invalidCount) {
ByteBuffer buf = ByteBuffer.allocate(512);
MemoryRecordsBuilder builder = MemoryRecords.builder(buf, magicValue, codec, TimestampType.CREATE_TIME, 0L);
builder.appendWithOffset(0, timestamp, null, "hello".getBytes());
builder.appendWithOffset(1, timestamp, null, "there".getBytes());
builder.appendWithOffset(2, timestamp, null, "beautiful".getBytes());
MemoryRecords records = builder.build();
ByteBuffer buffer = records.buffer();
buffer.position(0);
buffer.putInt(RECORDS_COUNT_OFFSET, invalidCount);
buffer.position(0);
return new DefaultRecordBatch(buffer);
}
示例11: p_serializeToBuffer
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Override
protected void p_serializeToBuffer(ByteBuffer b) throws IOException {
super.p_serializeToBuffer(b);
b.putInt(type.getValue());
FastSerializer.writeString(tableName, b);
b.putInt(buffer.capacity());
buffer.rewind();
b.put(buffer);
}
示例12: generateApkSigningBlock
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private static byte[] generateApkSigningBlock(byte[] apkSignatureSchemeV2Block) {
// FORMAT:
// uint64: size (excluding this field)
// repeated ID-value pairs:
// uint64: size (excluding this field)
// uint32: ID
// (size - 4) bytes: value
// uint64: size (same as the one above)
// uint128: magic
int resultSize =
8 // size
+ 8 + 4 + apkSignatureSchemeV2Block.length // v2Block as ID-value pair
+ 8 // size
+ 16 // magic
;
ByteBuffer result = ByteBuffer.allocate(resultSize);
result.order(ByteOrder.LITTLE_ENDIAN);
long blockSizeFieldValue = resultSize - 8;
result.putLong(blockSizeFieldValue);
long pairSizeFieldValue = 4 + apkSignatureSchemeV2Block.length;
result.putLong(pairSizeFieldValue);
result.putInt(APK_SIGNATURE_SCHEME_V2_BLOCK_ID);
result.put(apkSignatureSchemeV2Block);
result.putLong(blockSizeFieldValue);
result.put(APK_SIGNING_BLOCK_MAGIC);
return result.array();
}
示例13: encode
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public static byte[] encode(Message messageExt, boolean needCompress) {
int sysFlag = messageExt.getSysFlag();
byte[] body = messageExt.getBody();
if (needCompress && (sysFlag & COMPRESSED_FLAG) == COMPRESSED_FLAG) {
body = Util.compress(body, 5);
}
int bodyLength = body.length;
int storeSize = messageExt.getStoreSize();
ByteBuffer byteBuffer;
if (storeSize > 0) {
byteBuffer = ByteBuffer.allocate(storeSize);
} else {
storeSize = 4 // 1 TOTALSIZE
+ 4 // 2 MAGICCODE
+ 4 // 3 BODYCRC
+ 8 // 4 QUEUEID
+ 8 // 5 PHYSICALOFFSET
+ 4 // 6 SYSFLAG
+ 8 // 7 BORNTIMESTAMP
+ 8 // 8 STORETIMESTAMP
+ 4 + bodyLength // 9 BODY
+ 0;
byteBuffer = ByteBuffer.allocate(storeSize);
}
byteBuffer.putInt(storeSize); // 1 TOTALSIZE
byteBuffer.putInt(MESSAGE_MAGIC_CODE); // 2 MAGICCODE
byteBuffer.putInt( messageExt.getBodyCRC() ); // 3 BODYCRC
byteBuffer.putLong( messageExt.getQueueId() ); // 4 QUEUEID
byteBuffer.putLong( messageExt.getCommitLogOffset() ); // 5 PHYSICALOFFSET
byteBuffer.putInt(sysFlag); // 6 SYSFLAG
byteBuffer.putLong( messageExt.getBornTimestamp() ); // 7 BORNTIMESTAMP
byteBuffer.putLong( messageExt.getStoreTimestamp() ); // 8 STORETIMESTAMP
byteBuffer.putInt(bodyLength); // 9 BODY
byteBuffer.put( body );
return byteBuffer.array();
}
示例14: writeHeader
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Override
protected void writeHeader(ByteBuffer output) {
int entriesStart = getHeaderSize() + getOffsetSize();
output.putInt(id); // Write an unsigned byte with 3 bytes padding
output.putInt(entryCount);
output.putInt(entriesStart);
output.put(configuration.toByteArray(false));
}
示例15: sendOKHandshakeReply
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private void sendOKHandshakeReply() throws IOException, ConnectionException {
byte[] my_okHandshakeBytes = null;
ByteBuffer my_okHandshakeBuf = null;
if (this.isReceiver) {
DistributionConfig cfg = owner.getConduit().config;
ByteBuffer bb;
if (useNIO() && TCPConduit.useDirectBuffers) {
bb = ByteBuffer.allocateDirect(128);
} else {
bb = ByteBuffer.allocate(128);
}
bb.putInt(0); // reserve first 4 bytes for packet length
bb.put((byte) NORMAL_MSG_TYPE);
bb.putShort(MsgIdGenerator.NO_MSG_ID);
bb.put(REPLY_CODE_OK_WITH_ASYNC_INFO);
bb.putInt(cfg.getAsyncDistributionTimeout());
bb.putInt(cfg.getAsyncQueueTimeout());
bb.putInt(cfg.getAsyncMaxQueueSize());
// write own product version
Version.writeOrdinal(bb, Version.CURRENT.ordinal(), true);
// now set the msg length into position 0
bb.putInt(0, calcHdrSize(bb.position() - MSG_HEADER_BYTES));
if (useNIO()) {
my_okHandshakeBuf = bb;
bb.flip();
} else {
my_okHandshakeBytes = new byte[bb.position()];
bb.flip();
bb.get(my_okHandshakeBytes);
}
} else {
my_okHandshakeBuf = okHandshakeBuf;
my_okHandshakeBytes = okHandshakeBytes;
}
if (useNIO()) {
synchronized (my_okHandshakeBuf) {
my_okHandshakeBuf.position(0);
nioWriteFully(getSocket().getChannel(), my_okHandshakeBuf, false, null);
}
} else {
synchronized (outLock) {
try {
// this.writerThread = Thread.currentThread();
this.output.write(my_okHandshakeBytes, 0, my_okHandshakeBytes.length);
this.output.flush();
} finally {
// this.writerThread = null;
}
}
}
}