本文整理了Java中java.nio.ByteBuffer.wrap()
方法的一些代码示例,展示了ByteBuffer.wrap()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuffer.wrap()
方法的具体详情如下:
包路径:java.nio.ByteBuffer
类名称:ByteBuffer
方法名:wrap
[英]Creates a new byte buffer by wrapping the given byte array.
Calling this method has the same effect as wrap(array, 0, array.length).
[中]通过包装给定的字节数组创建新的字节缓冲区。
调用此方法与wrap(array,0,array.length)具有相同的效果。
代码示例来源:origin: Tencent/tinker
/**
* Creates a new empty dex of the specified size.
*/
public Dex(int byteCount) {
this.data = ByteBuffer.wrap(new byte[byteCount]);
this.data.order(ByteOrder.LITTLE_ENDIAN);
this.tableOfContents.fileSize = byteCount;
}
代码示例来源:origin: bumptech/glide
RandomAccessReader(byte[] data, int length) {
this.data = (ByteBuffer) ByteBuffer.wrap(data)
.order(ByteOrder.BIG_ENDIAN)
.limit(length);
}
代码示例来源:origin: apache/flink
private static String deserializeV1(byte[] serialized) {
final ByteBuffer bb = ByteBuffer.wrap(serialized).order(ByteOrder.LITTLE_ENDIAN);
final byte[] targetStringBytes = new byte[bb.getInt()];
bb.get(targetStringBytes);
return new String(targetStringBytes, CHARSET);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void decodeHeartbeat() {
String frame = "\n";
ByteBuffer buffer = ByteBuffer.wrap(frame.getBytes());
final List<Message<byte[]>> messages = decoder.decode(buffer);
assertEquals(1, messages.size());
assertEquals(SimpMessageType.HEARTBEAT, StompHeaderAccessor.wrap(messages.get(0)).getMessageType());
}
代码示例来源:origin: google/guava
@Override
void performAction(Random random, Iterable<? extends PrimitiveSink> sinks) {
byte[] value = new byte[random.nextInt(128)];
random.nextBytes(value);
int pos = random.nextInt(value.length + 1);
int limit = pos + random.nextInt(value.length - pos + 1);
for (PrimitiveSink sink : sinks) {
ByteBuffer buffer = ByteBuffer.wrap(value);
buffer.position(pos);
buffer.limit(limit);
sink.putBytes(buffer);
assertEquals(limit, buffer.limit());
assertEquals(limit, buffer.position());
}
}
},
代码示例来源:origin: bumptech/glide
@Test
public void getOrientation_withExifSegmentLessThanLength_returnsUnknown() throws IOException {
ByteBuffer jpegHeaderBytes = getExifMagicNumber();
byte[] data = new byte[] {
jpegHeaderBytes.get(0), jpegHeaderBytes.get(1),
(byte) DefaultImageHeaderParser.SEGMENT_START_ID,
(byte) DefaultImageHeaderParser.EXIF_SEGMENT_TYPE,
// SEGMENT_LENGTH
(byte) 0xFF, (byte) 0xFF,
};
ByteBuffer byteBuffer = ByteBuffer.wrap(data);
DefaultImageHeaderParser parser = new DefaultImageHeaderParser();
assertEquals(ImageHeaderParser.UNKNOWN_ORIENTATION,
parser.getOrientation(byteBuffer, byteArrayPool));
}
代码示例来源:origin: apache/nifi
private String fixedSizeByteArrayAsASCIIString(int length){
byte[] bBinary = RandomUtils.nextBytes(length);
ByteBuffer bytes = ByteBuffer.wrap(bBinary);
StringBuffer sbBytes = new StringBuffer();
for (int i = bytes.position(); i < bytes.limit(); i++)
sbBytes.append((char)bytes.get(i));
return sbBytes.toString();
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void byteBufferToByteBuffer() throws Exception {
byte[] bytes = new byte[] { 1, 2, 3 };
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
ByteBuffer convert = this.conversionService.convert(byteBuffer, ByteBuffer.class);
assertThat(convert, not(sameInstance(byteBuffer.rewind())));
assertThat(convert, equalTo(byteBuffer.rewind()));
assertThat(convert, equalTo(ByteBuffer.wrap(bytes)));
assertThat(convert.array(), equalTo(bytes));
}
代码示例来源:origin: wildfly/wildfly
/** Copies a ByteBuffer by copying and wrapping the underlying array of a heap-based buffer. Direct buffers
are converted to heap-based buffers */
public static ByteBuffer copyBuffer(final ByteBuffer buf) {
if(buf == null)
return null;
int offset=buf.hasArray()? buf.arrayOffset() + buf.position() : buf.position(), len=buf.remaining();
byte[] tmp=new byte[len];
if(!buf.isDirect())
System.arraycopy(buf.array(), offset, tmp, 0, len);
else {
// buf.get(tmp, 0, len);
for(int i=0; i < len; i++)
tmp[i]=buf.get(i+offset);
}
return ByteBuffer.wrap(tmp);
}
代码示例来源:origin: apache/incubator-druid
@Test
public void testBasicOperations()
{
ByteBuffer compressionOut = compressionStrategy.getCompressor().allocateOutBuffer(originalData.length, closer);
ByteBuffer compressed = compressionStrategy.getCompressor().compress(ByteBuffer.wrap(originalData), compressionOut);
ByteBuffer output = ByteBuffer.allocate(originalData.length);
compressionStrategy.getDecompressor().decompress(compressed, compressed.remaining(), output);
byte[] checkArray = new byte[DATA_SIZER];
output.get(checkArray);
Assert.assertArrayEquals("Uncompressed data does not match", originalData, checkArray);
}
代码示例来源:origin: apache/incubator-druid
@Test
public void testBehaviorWhenReportedSizesLargeAndExceptionIgnored() throws Exception
for (int i = 0; i < 20; ++i) {
final SmooshedWriter writer = smoosher.addWithSmooshedWriter(StringUtils.format("%d", i), 7);
writer.write(ByteBuffer.wrap(Ints.toByteArray(i)));
try {
writer.close();
for (int i = 0; i < 20; ++i) {
ByteBuffer buf = mapper.mapFile(StringUtils.format("%d", i));
Assert.assertEquals(0, buf.position());
Assert.assertEquals(4, buf.remaining());
Assert.assertEquals(4, buf.capacity());
Assert.assertEquals(i, buf.getInt());
代码示例来源:origin: apache/flink
@Override
public ByteBuffer wrap(int offset, int length) {
if (address <= addressLimit) {
if (heapMemory != null) {
return ByteBuffer.wrap(heapMemory, offset, length);
}
else {
try {
ByteBuffer wrapper = offHeapBuffer.duplicate();
wrapper.limit(offset + length);
wrapper.position(offset);
return wrapper;
}
catch (IllegalArgumentException e) {
throw new IndexOutOfBoundsException();
}
}
}
else {
throw new IllegalStateException("segment has been freed");
}
}
代码示例来源:origin: apache/incubator-druid
@Test(expected = BufferUnderflowException.class)
public void testOutOfBounds()
{
ByteBuffer bytes = ByteBuffer.wrap(new byte[]{'a', 'b', 'c', 'd'});
bytes.position(1).limit(3);
StringUtils.fromUtf8(bytes, 3);
}
代码示例来源:origin: apache/kafka
@Test
public void testBadBlockSize() throws Exception {
if (!close || (useBrokenFlagDescriptorChecksum && !ignoreFlagDescriptorChecksum)) return;
thrown.expect(IOException.class);
thrown.expectMessage(CoreMatchers.containsString("exceeded max"));
byte[] compressed = compressedBytes();
final ByteBuffer buffer = ByteBuffer.wrap(compressed).order(ByteOrder.LITTLE_ENDIAN);
int blockSize = buffer.getInt(7);
blockSize = (blockSize & LZ4_FRAME_INCOMPRESSIBLE_MASK) | (1 << 24 & ~LZ4_FRAME_INCOMPRESSIBLE_MASK);
buffer.putInt(7, blockSize);
testDecompression(buffer);
}
代码示例来源:origin: google/guava
static void assertHashByteBufferPreservesByteOrder(HashFunction hashFunction) {
Random rng = new Random(0L);
byte[] bytes = new byte[rng.nextInt(256) + 1];
rng.nextBytes(bytes);
ByteBuffer littleEndian = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
ByteBuffer bigEndian = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN);
assertEquals(hashFunction.hashBytes(littleEndian), hashFunction.hashBytes(littleEndian));
assertEquals(ByteOrder.LITTLE_ENDIAN, littleEndian.order());
assertEquals(ByteOrder.BIG_ENDIAN, littleEndian.order());
}
代码示例来源:origin: jersey/jersey
static ByteBuffer split(ByteBuffer buffer, int position) {
int bytesLength = position - buffer.position();
byte[] bytes = new byte[bytesLength];
buffer.get(bytes);
return ByteBuffer.wrap(bytes);
}
代码示例来源:origin: spring-projects/spring-framework
private void assertIncompleteDecode(String partialFrame) {
ByteBuffer buffer = ByteBuffer.wrap(partialFrame.getBytes());
assertNull(decode(buffer));
assertEquals(0, buffer.position());
}
代码示例来源:origin: kaaproject/kaa
private void resizeIfNeeded(int size) {
if (size > data.remaining()) {
int position = data.position();
data = ByteBuffer.wrap(Arrays.copyOf(
data.array(), Math.max(data.position() + size, data.array().length * 2)));
data.position(position);
}
}
代码示例来源:origin: neo4j/neo4j
private void assertStringHeader( byte[] header, int itemCount )
{
assertEquals( PropertyType.STRING.byteValue(), header[0] );
assertEquals( itemCount, ByteBuffer.wrap( header, 1, 4 ).getInt() );
}
代码示例来源:origin: alibaba/mdrill
public static PacketPair parse_packet(byte[] packet) {
ByteBuffer buff = ByteBuffer.wrap(packet);
Integer port = buff.getInt();
byte[] message = new byte[buff.array().length - (Integer.SIZE / 8)];
buff.get(message);
PacketPair pair = new PacketPair(port,message);
return pair;
}
内容来源于网络,如有侵权,请联系作者删除!