本文整理汇总了Java中java.nio.ByteBuffer.allocate方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBuffer.allocate方法的具体用法?Java ByteBuffer.allocate怎么用?Java ByteBuffer.allocate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.ByteBuffer
的用法示例。
在下文中一共展示了ByteBuffer.allocate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: xor_A_with_B
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Override
public void xor_A_with_B ( AT_Machine_State state ) {
ByteBuffer a = ByteBuffer.allocate(32);
a.order( ByteOrder.LITTLE_ENDIAN );
a.put(state.get_A1());
a.put(state.get_A2());
a.put(state.get_A3());
a.put(state.get_A4());
a.clear();
ByteBuffer b = ByteBuffer.allocate(32);
b.order( ByteOrder.LITTLE_ENDIAN );
b.put(state.get_B1());
b.put(state.get_B2());
b.put(state.get_B3());
b.put(state.get_B4());
b.clear();
state.set_A1(AT_API_Helper.getByteArray(a.getLong(0) ^ b.getLong(0)));
state.set_A2(AT_API_Helper.getByteArray(a.getLong(8) ^ b.getLong(8)));
state.set_A3(AT_API_Helper.getByteArray(a.getLong(16) ^ b.getLong(16)));
state.set_A4(AT_API_Helper.getByteArray(a.getLong(24) ^ b.getLong(24)));
}
示例2: testRegion
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Test
public void testRegion() {
ByteBuffer byteBuffer = ByteBuffer.allocate(10);
byteBuffer.limit(7);
Buffer buff = Buffer.wrap(byteBuffer); //assuming buff has cap of 8
assertEquals(buff.getCapacity(), 10); //wrong should be 8
buff.getByte(); //pos moves to 1
Buffer copyBuff = buff.region(); //pos: 0, start: 0, end: 6: cap: 7
assertEquals(copyBuff.getEnd(), 6);
assertEquals(copyBuff.getCapacity(), 6);
assertEquals(copyBuff.getStart(), 0);
assertEquals(copyBuff.getPosition(), 0);
buff.setStartPositionEnd(1, 1, 5);
buff.getByte();
Buffer copyBuff2 = buff.region();
assertEquals(copyBuff2.getEnd(), 3);
assertEquals(copyBuff2.getCapacity(), 3);
assertEquals(copyBuff2.getStart(), 0);
assertEquals(copyBuff2.getPosition(), 0);
}
示例3: toIntFromByteArray
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Returns an integer value out of a byte array.
*
* @param byteArray The byte array to be converted into its decimal representation
* @return The integer value representing the byte array
*/
public static int toIntFromByteArray(byte[] byteArray) {
// Allocating a byte buffer holding 4 bytes for the int value
ByteBuffer bb = ByteBuffer.allocate(4);
/*
* The given byte array might hold less than 4 bytes, e.g. the SECC port of the SECC Discovery
* Response only holds 2 bytes. Thus, we must account for this and guarantee the Big Endian
* byte order.
*/
bb.position(4 - byteArray.length);
bb.put(byteArray);
// Setting the current position to 0, otherwise getInt() would throw a BufferUnderflowException
bb.position(0);
return bb.getInt();
}
示例4: createMockPacket
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private static ByteBuffer createMockPacket() {
ByteBuffer buffer = ByteBuffer.allocate(32);
buffer.put((byte) ((4 << 4) | 5)); // versionAndIHL
buffer.put((byte) 0); // ToS
buffer.putShort((short) 32); // total length 20 + 8 + 4
buffer.putInt(0); // IdFlagsFragmentOffset
buffer.put((byte) 0); // TTL
buffer.put((byte) 17); // protocol (UDP)
buffer.putShort((short) 0); // checksum
buffer.putInt(0x12345678); // source address
buffer.putInt(0x42424242); // destination address
buffer.putShort((short) 1234); // source port
buffer.putShort((short) 5678); // destination port
buffer.putShort((short) 4); // length
buffer.putShort((short) 0); // checksum
buffer.putInt(0x11223344); // payload
return buffer;
}
示例5: combine
import java.nio.ByteBuffer; //导入方法依赖的package包/类
ByteBufferReference combine(ByteBufferReference ref1, ByteBufferReference ref2) {
ByteBuffer buf1 = ref1.get();
ByteBuffer buf2 = ref2.get();
int avail1 = buf1.capacity() - buf1.remaining();
if (buf2.remaining() < avail1) {
buf1.compact();
buf1.put(buf2);
buf1.flip();
ref2.clear();
return ref1;
}
int newsize = buf1.remaining() + buf2.remaining();
ByteBuffer newbuf = ByteBuffer.allocate(newsize); // getting rid of buffer pools
newbuf.put(buf1);
newbuf.put(buf2);
newbuf.flip();
ref1.clear();
ref2.clear();
return ByteBufferReference.of(newbuf);
}
示例6: allocateByteBuffer
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public static ByteBuffer allocateByteBuffer(boolean useDirectBuffer,
int bufSize) {
if (useDirectBuffer) {
return ByteBuffer.allocateDirect(bufSize);
} else {
return ByteBuffer.allocate(bufSize);
}
}
示例7: append
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private void append(FileRecords fileRecords, byte[][] values) throws IOException {
long offset = 0L;
for (byte[] value : values) {
ByteBuffer buffer = ByteBuffer.allocate(128);
MemoryRecordsBuilder builder = MemoryRecords.builder(buffer, RecordBatch.CURRENT_MAGIC_VALUE,
CompressionType.NONE, TimestampType.CREATE_TIME, offset);
builder.appendWithOffset(offset++, System.currentTimeMillis(), null, value);
fileRecords.append(builder.build());
}
fileRecords.flush();
}
示例8: calculateChecksum
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Calculated checksum for given data.
*
* @param content of file.
* @return CRC32 checksum.
*/
public static byte[] calculateChecksum(byte[] content) {
CRC32 crc = new CRC32();
crc.update(content);
ByteBuffer buffer = ByteBuffer.allocate(Long.SIZE / Byte.SIZE);
return buffer.putLong(crc.getValue()).array();
}
示例9: testDecodeModifyDNResponseEmptyResult
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Test the decoding of a ModifyDNResponse with no LdapResult
*/
@Test
public void testDecodeModifyDNResponseEmptyResult()
{
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate( 0x07 );
stream.put( new byte[]
{ 0x30, 0x05, // LDAPMessage ::=SEQUENCE {
0x02,
0x01,
0x01, // messageID MessageID
0x6D,
0x00 // CHOICE { ..., modifyDNResponse ModifyDNResponse,
// ...
} );
stream.flip();
// Allocate a LdapMessage Container
LdapMessageContainer<ModifyDnResponseDecorator> ldapMessageContainer =
new LdapMessageContainer<ModifyDnResponseDecorator>( codec );
// Decode a ModifyDNResponse message
try
{
ldapDecoder.decode( stream, ldapMessageContainer );
}
catch ( DecoderException de )
{
assertTrue( true );
return;
}
fail( "We should not reach this point" );
}
示例10: loadCatalog
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/** write the catalog as a UTF-8 byte string via connection */
@Override
public void loadCatalog(final String serializedCatalog) throws EEException {
int result = ExecutionEngine.ERRORCODE_ERROR;
m_data.clear();
try {
final byte catalogBytes[] = serializedCatalog.getBytes("UTF-8");
if (m_data.capacity() < catalogBytes.length + 100) {
m_data = ByteBuffer.allocate(catalogBytes.length + 100);
}
m_data.putInt(Commands.LoadCatalog.m_id);
m_data.put(catalogBytes);
m_data.put((byte)'\0');
} catch (final UnsupportedEncodingException ex) {
Logger.getLogger(ExecutionEngineIPC.class.getName()).log(
Level.SEVERE, null, ex);
}
try {
m_data.flip();
m_connection.write();
result = m_connection.readStatusByte();
} catch (final IOException e) {
System.out.println("Exception: " + e.getMessage());
throw new RuntimeException(e);
}
checkErrorCode(result);
}
示例11: testEquivalence
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Test
public void testEquivalence() {
ByteBuffer bb = ByteBuffer.allocate(1);
this.reservoir.putBuffer(bb);
this.reservoir.putBuffer(bb);
this.reservoir.putBuffer(bb);
assertEquals(3, this.reservoir.buffers.size());
}
示例12: getFullBytes
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public byte[] getFullBytes() {
ByteBuffer buffer = ByteBuffer.allocate(ADDITIONAL_SECTION_SIZE);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.put(md5Hash);
buffer.put(padding);
buffer.putInt(getSecondaryAdler32());
return buffer.array();
}
示例13: toByteArray
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Convert to a byte array. Note that the numbers are stored in big-endian
* order.
*
* @return the byte array
*/
public byte[] toByteArray() {
ByteBuffer buffer = ByteBuffer.allocate(12);
putToByteBuffer(buffer);
return buffer.array(); // using .allocate ensures there is a backing
// array that can be returned
}
示例14: get
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Override
public ByteBuffer get() {
ByteBuffer content = ByteBuffer.allocate(1);
content.put((byte) (levelIndependentlyDecodable ? 0x80 : 0x00));
content.rewind();
return content;
}
示例15: nextBytes
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Returns <code>n</code> random bytes
*
* @param n number of bytes to return
* @return the next <code>n</code> random bytes
*/
byte[] nextBytes(int n)
{
ByteBuffer buf = ByteBuffer.allocate(n);
while (buf.hasRemaining())
{
ByteBuffer cbuf = ByteBuffer.allocate(seed.length + 4);
cbuf.put(seed);
cbuf.putInt(counter);
byte[] array = cbuf.array();
byte[] hash = new byte[hashAlg.getDigestSize()];
hashAlg.update(array, 0, array.length);
hashAlg.doFinal(hash, 0);
if (buf.remaining() < hash.length)
{
buf.put(hash, 0, buf.remaining());
}
else
{
buf.put(hash);
}
counter++;
}
return buf.array();
}