本文整理汇总了Java中java.nio.ByteBuffer.duplicate方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBuffer.duplicate方法的具体用法?Java ByteBuffer.duplicate怎么用?Java ByteBuffer.duplicate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.ByteBuffer
的用法示例。
在下文中一共展示了ByteBuffer.duplicate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getSerializedValue
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Given a serialized map, gets the value associated with a given key.
* @param serializedMap a serialized map
* @param serializedKey a serialized key
* @param keyType the key type for the map
* @return the value associated with the key if one exists, null otherwise
*/
public ByteBuffer getSerializedValue(ByteBuffer serializedMap, ByteBuffer serializedKey, AbstractType keyType)
{
try
{
ByteBuffer input = serializedMap.duplicate();
int n = readCollectionSize(input, ProtocolVersion.V3);
for (int i = 0; i < n; i++)
{
ByteBuffer kbb = readValue(input, ProtocolVersion.V3);
ByteBuffer vbb = readValue(input, ProtocolVersion.V3);
int comparison = keyType.compare(kbb, serializedKey);
if (comparison == 0)
return vbb;
else if (comparison > 0)
// since the map is in sorted order, we know we've gone too far and the element doesn't exist
return null;
}
return null;
}
catch (BufferUnderflowException e)
{
throw new MarshalException("Not enough bytes to read a map");
}
}
示例2: stillAllZeroes
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private boolean stillAllZeroes(ByteBuffer message) {
if (fileWasZero) {
final ByteBuffer checkForZeroes = message.duplicate();
checkForZeroes.rewind();
while (checkForZeroes.hasRemaining()) {
final byte b = checkForZeroes.get();
if (b != 0) {
fileWasZero = false;
}
}
if (fileWasZero) {
LOGGER.debug("saved some resources by not writing zeroes");
return true;
}
}
return false;
}
示例3: decodeInternal
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Override
Map<K, V> decodeInternal(ByteBuffer input) {
if (input == null || input.remaining() == 0) {
return new LinkedHashMap<>();
}
try {
ByteBuffer bytes = input.duplicate();
int n = readSize(bytes);
Map<K, V> m = new LinkedHashMap<>(n);
for (int i = 0; i < n; i++) {
ByteBuffer kbb = readValue(bytes);
ByteBuffer vbb = readValue(bytes);
m.put(keyCodec.decode(kbb), valueCodec.decode(vbb));
}
return m;
} catch (BufferUnderflowException | IllegalArgumentException e) {
throw new InvalidTypeException("Not enough bytes to deserialize a map", e);
}
}
示例4: validateForNativeProtocol
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public void validateForNativeProtocol(ByteBuffer bytes, ProtocolVersion version)
{
try
{
ByteBuffer input = bytes.duplicate();
int n = readCollectionSize(input, version);
for (int i = 0; i < n; i++)
elements.validate(readValue(input, version));
if (input.hasRemaining())
throw new MarshalException("Unexpected extraneous bytes after list value");
}
catch (BufferUnderflowException e)
{
throw new MarshalException("Not enough bytes to read a list");
}
}
示例5: read
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Override
public int read(ByteBuffer dst) throws IOException {
// delay buffer read..
int willActuallyRead = randomInt(dst.remaining());
ByteBuffer mockDst = dst.duplicate();
mockDst.limit(mockDst.position() + willActuallyRead);
try {
return delegate.read(mockDst);
} finally {
dst.position(mockDst.position());
}
}
示例6: sizeDelimited
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Read a size-delimited byte buffer starting at the given offset.
* @param buffer Buffer containing the size and data
* @param start Offset in the buffer to read from
* @return A slice of the buffer containing only the delimited data (excluding the size)
*/
public static ByteBuffer sizeDelimited(ByteBuffer buffer, int start) {
int size = buffer.getInt(start);
if (size < 0) {
return null;
} else {
ByteBuffer b = buffer.duplicate();
b.position(start + 4);
b = b.slice();
b.limit(size);
b.rewind();
return b;
}
}
示例7: testBB
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Test
public void testBB() {
int n = 25;
ByteBuffer bb = ByteBuffer.allocate(n);
for (byte i = 0; i < n; i++) { bb.put(i, i); }
assertEquals(bb.position(), 0);
assertEquals(bb.limit(), n);
assertEquals(bb.get(0), 0);
// print("Orig : ");
// printbb(bb);
bb.limit(20);
bb.position(5);
assertEquals(bb.remaining(), 15);
// print("Set : ");
// printbb(bb);
ByteBuffer dup = bb.duplicate();
assertEquals(dup.position(), 5);
assertEquals(dup.limit(), 20);
assertEquals(dup.capacity(), 25);
// print("Dup : ");
// printbb(dup);
ByteBuffer sl = bb.slice();
assertEquals(sl.position(), 0);
assertEquals(sl.limit(), 15);
assertEquals(sl.capacity(), 15);
// print("Slice: ");
// printbb(sl);
}
示例8: onSPSandPPSRtp
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Override
protected void onSPSandPPSRtp(ByteBuffer sps, ByteBuffer pps) {
ByteBuffer newSps = sps.duplicate();
ByteBuffer newPps = pps.duplicate();
rtspClient.setSPSandPPS(newSps, newPps);
rtspClient.connect();
}
示例9: readBytes
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public static ByteBuffer readBytes(ByteBuffer bb, int length)
{
ByteBuffer copy = bb.duplicate();
copy.limit(copy.position() + length);
bb.position(bb.position() + length);
return copy;
}
示例10: deserialize
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public BigDecimal deserialize(ByteBuffer bytes)
{
if (bytes == null || bytes.remaining() == 0)
return null;
// do not consume the contents of the ByteBuffer
bytes = bytes.duplicate();
int scale = bytes.getInt();
byte[] bibytes = new byte[bytes.remaining()];
bytes.get(bibytes);
BigInteger bi = new BigInteger(bibytes);
return new BigDecimal(bi, scale);
}
示例11: runGCMWithSameBuffer
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private void runGCMWithSameBuffer(int mode, ByteBuffer buffer,
int txtOffset, int length, AlgorithmParameters params)
throws Exception {
// allocate a separate buffer
Cipher cipher = createCipher(mode, params);
ByteBuffer outBB = ByteBuffer.allocateDirect(
cipher.getOutputSize(length));
// first, generate the cipher text at an allocated buffer
buffer.flip();
buffer.limit(AADLength);
cipher.updateAAD(buffer);
buffer.limit(AADLength + txtOffset + length);
buffer.position(AADLength + txtOffset);
cipher.doFinal(buffer, outBB);
outBB.flip(); // cipher text in outBB
// next, generate cipherText again in the same buffer
Cipher anotherCipher = createCipher(mode, params);
buffer.flip();
buffer.limit(AADLength);
anotherCipher.updateAAD(buffer);
buffer.limit(AADLength + txtOffset + length);
buffer.position(AADLength + txtOffset);
// share textBuf context
ByteBuffer buf2 = buffer.duplicate();
buf2.limit(AADLength + txtOffset + anotherCipher.getOutputSize(length));
int dataProcessed2 = anotherCipher.doFinal(buffer, buf2);
buf2.position(AADLength + txtOffset);
buf2.limit(AADLength + txtOffset + dataProcessed2);
if (!buf2.equals(outBB)) {
throw new RuntimeException(
"Two results are not equal, mode:" + mode);
}
}
示例12: onEncodedAnnexbFrame
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private void onEncodedAnnexbFrame(ByteBuffer es, MediaCodec.BufferInfo bi) {
try {
ByteBuffer record = es.duplicate();
mp4Muxer.writeSampleData(videoMp4Track, record, bi);
flvMuxer.writeSampleData(videoFlvTrack, es, bi);
} catch (Exception e) {
Log.e(TAG, "muxer write video sample failed.");
e.printStackTrace();
}
}
示例13: splitName
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public static List<ByteBuffer> splitName(ByteBuffer name)
{
List<ByteBuffer> l = new ArrayList<>();
ByteBuffer bb = name.duplicate();
readStatic(bb);
while (bb.remaining() > 0)
{
l.add(ByteBufferUtil.readBytesWithShortLength(bb));
bb.get(); // skip end-of-component
}
return l;
}
示例14: deserializeForNativeProtocol
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public Map<K, V> deserializeForNativeProtocol(ByteBuffer bytes, ProtocolVersion version)
{
try
{
ByteBuffer input = bytes.duplicate();
int n = readCollectionSize(input, version);
if (n < 0)
throw new MarshalException("The data cannot be deserialized as a map");
// If the received bytes are not corresponding to a map, n might be a huge number.
// In such a case we do not want to initialize the map with that initialCapacity as it can result
// in an OOM when put is called (see CASSANDRA-12618). On the other hand we do not want to have to resize
// the map if we can avoid it, so we put a reasonable limit on the initialCapacity.
Map<K, V> m = new LinkedHashMap<K, V>(Math.min(n, 256));
for (int i = 0; i < n; i++)
{
ByteBuffer kbb = readValue(input, version);
keys.validate(kbb);
ByteBuffer vbb = readValue(input, version);
values.validate(vbb);
m.put(keys.deserialize(kbb), values.deserialize(vbb));
}
if (input.hasRemaining())
throw new MarshalException("Unexpected extraneous bytes after map value");
return m;
}
catch (BufferUnderflowException e)
{
throw new MarshalException("Not enough bytes to read a map");
}
}
示例15: discardToSps
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Discards data from the buffer up to the first SPS, where {@code data.position()} is interpreted
* as the length of the buffer.
* <p>
* When the method returns, {@code data.position()} will contain the new length of the buffer. If
* the buffer is not empty it is guaranteed to start with an SPS.
*
* @param data Buffer containing start code delimited NAL units.
*/
public static void discardToSps(ByteBuffer data) {
int length = data.position();
int consecutiveZeros = 0;
int offset = 0;
while (offset + 1 < length) {
int value = data.get(offset) & 0xFF;
if (consecutiveZeros == 3) {
if (value == 1 && (data.get(offset + 1) & 0x1F) == NAL_UNIT_TYPE_SPS) {
// Copy from this NAL unit onwards to the start of the buffer.
ByteBuffer offsetData = data.duplicate();
offsetData.position(offset - 3);
offsetData.limit(length);
data.position(0);
data.put(offsetData);
return;
}
} else if (value == 0) {
consecutiveZeros++;
}
if (value != 0) {
consecutiveZeros = 0;
}
offset++;
}
// Empty the buffer if the SPS NAL unit was not found.
data.clear();
}