本文整理汇总了Java中java.nio.ByteBuffer.position方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBuffer.position方法的具体用法?Java ByteBuffer.position怎么用?Java ByteBuffer.position使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.ByteBuffer
的用法示例。
在下文中一共展示了ByteBuffer.position方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: alignedWriteSnippet
import java.nio.ByteBuffer; //导入方法依赖的package包/类
byte[] alignedWriteSnippet(byte a, byte b, short c, int d, long e, double f, float g) {
byte[] ret = new byte[28];
ByteBuffer buffer = makeDirect(28, byteOrder);
buffer.put(a);
buffer.put(b);
buffer.putShort(c);
buffer.putInt(d);
buffer.putLong(e);
buffer.putDouble(f);
buffer.putFloat(g);
buffer.position(0);
buffer.get(ret);
return ret;
}
示例2: if
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* When the size of the metadata has changed and it cant be compensated for by free atom
* we have to adjust the size of the size field upto the moovheader level for the udta atom and
* its child meta atom.
*
* @param moovHeader
* @param moovBuffer
* @param sizeAdjustment can be negative or positive *
* @param udtaHeader
* @param metaHeader
* @return
* @throws java.io.IOException
*/
private void adjustSizeOfMoovHeader
(Mp4BoxHeader moovHeader,
ByteBuffer moovBuffer,
int sizeAdjustment,
Mp4BoxHeader udtaHeader,
Mp4BoxHeader metaHeader) throws IOException {
//Adjust moov header size, adjusts the underlying buffer
moovHeader.setLength(moovHeader.getLength() + sizeAdjustment);
//Edit the fields in moovBuffer (note moovbuffer doesnt include header)
if (udtaHeader != null) {
//Write the updated udta atom header to moov buffer
udtaHeader.setLength(udtaHeader.getLength() + sizeAdjustment);
moovBuffer.position((int) (udtaHeader.getFilePos() - moovHeader.getFilePos() - Mp4BoxHeader.HEADER_LENGTH));
moovBuffer.put(udtaHeader.getHeaderData());
}
if (metaHeader != null) {
//Write the updated udta atom header to moov buffer
metaHeader.setLength(metaHeader.getLength() + sizeAdjustment);
moovBuffer.position((int) (metaHeader.getFilePos() - moovHeader.getFilePos() - Mp4BoxHeader.HEADER_LENGTH));
moovBuffer.put(metaHeader.getHeaderData());
}
}
示例3: afterReceived
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Override
protected void afterReceived(ByteBuffer buffer) throws Exception {
if (!m_TunnelEstablished) {
//收到代理服务器响应数据
//分析响应并判断是否连接成功
String response = new String(buffer.array(), buffer.position(), 12);
if (response.matches("^HTTP/1.[01] 200$")) {
buffer.limit(buffer.position());
} else {
throw new Exception(String.format("Proxy server responsed an error: %s", response));
}
m_TunnelEstablished = true;
super.onTunnelEstablished();
}
}
示例4: decode
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Decodes all bytes from the input byte buffer using the {@link Base64}
* encoding scheme, writing the results into a newly-allocated ByteBuffer.
*
* <p> Upon return, the source buffer's position will be updated to
* its limit; its limit will not have been changed. The returned
* output buffer's position will be zero and its limit will be the
* number of resulting decoded bytes
*
* <p> {@code IllegalArgumentException} is thrown if the input buffer
* is not in valid Base64 encoding scheme. The position of the input
* buffer will not be advanced in this case.
*
* @param buffer
* the ByteBuffer to decode
*
* @return A newly-allocated byte buffer containing the decoded bytes
*
* @throws IllegalArgumentException
* if {@code src} is not in valid Base64 scheme.
*/
public ByteBuffer decode(ByteBuffer buffer) {
int pos0 = buffer.position();
try {
byte[] src;
int sp, sl;
if (buffer.hasArray()) {
src = buffer.array();
sp = buffer.arrayOffset() + buffer.position();
sl = buffer.arrayOffset() + buffer.limit();
buffer.position(buffer.limit());
} else {
src = new byte[buffer.remaining()];
buffer.get(src);
sp = 0;
sl = src.length;
}
byte[] dst = new byte[outLength(src, sp, sl)];
return ByteBuffer.wrap(dst, 0, decode0(src, sp, sl, dst));
} catch (IllegalArgumentException iae) {
buffer.position(pos0);
throw iae;
}
}
示例5: a
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public static void a(ByteBuffer byteBuffer, StringBuilder stringBuilder) {
byte[] array = byteBuffer.array();
int arrayOffset = byteBuffer.arrayOffset();
int position = arrayOffset + byteBuffer.position();
int limit = byteBuffer.limit() + arrayOffset;
if (limit - position > 128) {
arrayOffset = position + 128;
} else {
arrayOffset = limit;
}
for (int i = position; i < arrayOffset; i++) {
if (i > position) {
stringBuilder.append(" ");
}
stringBuilder.append(a(array[i]));
}
if (limit != arrayOffset) {
stringBuilder.append("...");
}
}
示例6: 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();
}
示例7: deserializeString
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public static String deserializeString(ByteBuffer string) {
if (string.hasArray()) {
int base = string.arrayOffset();
return new String(string.array(), base + string.position(), string.remaining());
} else {
return new String(Utils.toByteArray(string), UTF8_CHARSET);
}
}
示例8: write
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Writes the content of the provided {@link ByteBuffer} into the {@link SmbRandomAccessFile} handled by the current
* instance of {@link SeekableSMBByteChannel}. The {@link ByteBuffer} is read from its current position to it end.
*
* @param src {@link ByteBuffer} from which to read the data.
* @return Number of bytes that were written.
* @throws IOException If something goes wrong while writing to the file.
*/
@Override
public synchronized int write(ByteBuffer src) throws IOException {
if (!this.open) throw new ClosedChannelException();
final int len = src.limit() - src.position();
final byte[] buffer = new byte[len];
src.get(buffer);
this.random.write(buffer);
return len;
}
示例9: read
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public int read(byte b[], int off, int len) throws IOException {
ensureOpen();
initDataPos();
if (rem == 0) {
return -1;
}
if (len <= 0) {
return 0;
}
if (len > rem) {
len = (int) rem;
}
// readFullyAt()
long n = 0;
ByteBuffer bb = ByteBuffer.wrap(b);
bb.position(off);
bb.limit(off + len);
synchronized(zfch) {
n = zfch.position(pos).read(bb);
}
if (n > 0) {
pos += n;
rem -= n;
}
if (rem == 0) {
close();
}
return (int)n;
}
示例10: decodeArrayLoop
import java.nio.ByteBuffer; //导入方法依赖的package包/类
protected CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
byte[] sa = src.array();
int sp = src.arrayOffset() + src.position();
int sl = src.arrayOffset() + src.limit();
char[] da = dst.array();
int dp = dst.arrayOffset() + dst.position();
int dl = dst.arrayOffset() + dst.limit();
try {
while (sp < sl && dp < dl) {
// inline the decodeSingle/Double() for better performance
int inSize = 1;
int b1 = sa[sp] & 0xff;
char c = b2cSB[b1];
if (c == UNMAPPABLE_DECODING) {
if (sl - sp < 2)
return crMalformedOrUnderFlow(b1);
int b2 = sa[sp + 1] & 0xff;
if (b2 < b2Min || b2 > b2Max ||
(c = b2c[b1][b2 - b2Min]) == UNMAPPABLE_DECODING) {
return crMalformedOrUnmappable(b1, b2);
}
inSize++;
}
da[dp++] = c;
sp += inSize;
}
return (sp >= sl) ? CoderResult.UNDERFLOW
: CoderResult.OVERFLOW;
} finally {
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
}
}
示例11: encodeToVideoTrack
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void encodeToVideoTrack(int index) {
ByteBuffer encodedData = mEncoder.getOutputBuffer(index);
if ((mBufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
// The codec config data was pulled out and fed to the muxer when we got
// the INFO_OUTPUT_FORMAT_CHANGED status.
// Ignore it.
Log.d(TAG, "ignoring BUFFER_FLAG_CODEC_CONFIG");
mBufferInfo.size = 0;
}
if (mBufferInfo.size == 0) {
Log.d(TAG, "info.size == 0, drop it.");
encodedData = null;
} else {
Log.d(TAG, "got buffer, info: size=" + mBufferInfo.size
+ ", presentationTimeUs=" + mBufferInfo.presentationTimeUs
+ ", offset=" + mBufferInfo.offset);
}
if (encodedData != null) {
encodedData.position(mBufferInfo.offset);
encodedData.limit(mBufferInfo.offset + mBufferInfo.size);
mMuxer.writeSampleData(mVideoTrackIndex, encodedData, mBufferInfo);
Log.i(TAG, "sent mBufferInfo.offset" + mBufferInfo.offset + " bytes to muxer...");
Log.i(TAG, "sent mBufferInfo.size" + mBufferInfo.size + " bytes to muxer...");
}
}
示例12: testWriteEndTxnMarkerNonControlBatch
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Test(expected = IllegalArgumentException.class)
public void testWriteEndTxnMarkerNonControlBatch() {
ByteBuffer buffer = ByteBuffer.allocate(128);
buffer.position(bufferOffset);
long pid = 9809;
short epoch = 15;
int sequence = RecordBatch.NO_SEQUENCE;
MemoryRecordsBuilder builder = new MemoryRecordsBuilder(buffer, RecordBatch.CURRENT_MAGIC_VALUE, compressionType, TimestampType.CREATE_TIME,
0L, 0L, pid, epoch, sequence, true, false, RecordBatch.NO_PARTITION_LEADER_EPOCH, buffer.capacity());
builder.appendEndTxnMarker(RecordBatch.NO_TIMESTAMP, new EndTransactionMarker(ControlRecordType.ABORT, 0));
}
示例13: encodeLoop
import java.nio.ByteBuffer; //导入方法依赖的package包/类
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
char[] sa = src.array();
int sp = src.arrayOffset() + src.position();
int sl = src.arrayOffset() + src.limit();
byte[] da = dst.array();
int dp = dst.arrayOffset() + dst.position();
int dl = dst.arrayOffset() + dst.limit();
try {
while (sp < sl) {
char c = sa[sp];
if (c <= '\u007f')
return CoderResult.unmappableForLength(1);
int ncode = encodeDouble(c);
if (ncode != 0 && c != '\u0000' ) {
da[dp++] = (byte) ((ncode >> 8) & 0x7f);
da[dp++] = (byte) (ncode & 0x7f);
sp++;
continue;
}
return CoderResult.unmappableForLength(1);
}
return CoderResult.UNDERFLOW;
} finally {
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
}
}
示例14: decompressDirect
import java.nio.ByteBuffer; //导入方法依赖的package包/类
int decompressDirect(ByteBuffer src, ByteBuffer dst) throws IOException {
assert (this instanceof SnappyDirectDecompressor);
ByteBuffer presliced = dst;
if (dst.position() > 0) {
presliced = dst;
dst = dst.slice();
}
Buffer originalCompressed = compressedDirectBuf;
Buffer originalUncompressed = uncompressedDirectBuf;
int originalBufferSize = directBufferSize;
compressedDirectBuf = src.slice();
compressedDirectBufLen = src.remaining();
uncompressedDirectBuf = dst;
directBufferSize = dst.remaining();
int n = 0;
try {
n = decompressBytesDirect();
presliced.position(presliced.position() + n);
// SNAPPY always consumes the whole buffer or throws an exception
src.position(src.limit());
finished = true;
} finally {
compressedDirectBuf = originalCompressed;
uncompressedDirectBuf = originalUncompressed;
compressedDirectBufLen = 0;
directBufferSize = originalBufferSize;
}
return n;
}
示例15: update
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Starts or continues a SHA-1 message digest calculation.
* Only the remaining bytes of the given ByteBuffer are used.
* @param buffer input data
*/
public void update(ByteBuffer buffer) {
length += buffer.remaining();
//Save current position to leave given buffer unchanged
int position = buffer.position();
//Complete the final buffer if needed
completeFinalBuffer(buffer);
if(!buffer.hasArray() || buffer.isDirect())
{
if(cacheBlock == null) // only allocate if we process direct byte buffers
cacheBlock = new byte[cacheSize];
while(buffer.remaining() >= 64)
{
int toProcess = Math.min(buffer.remaining()-buffer.remaining()%64,cacheSize);
buffer.get(cacheBlock, 0, toProcess);
for(int i = 0;i < toProcess; i+=64)
transform(cacheBlock,i);
}
} else // use direct array access for heap buffers
{
final int endPos = buffer.position()+buffer.remaining()-buffer.remaining()%64;
final int internalEndPos = endPos+buffer.arrayOffset();
for(int i = buffer.arrayOffset()+buffer.position();i < internalEndPos;i+=64)
transform(buffer.array(),i);
buffer.position(endPos);
}
if(buffer.remaining() != 0) {
finalBuffer.put(buffer);
}
buffer.position(position);
}