java.nio.Buffer.position()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(6.8k)|赞(0)|评价(0)|浏览(443)

本文整理了Java中java.nio.Buffer.position()方法的一些代码示例,展示了Buffer.position()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Buffer.position()方法的具体详情如下:
包路径:java.nio.Buffer
类名称:Buffer
方法名:position

Buffer.position介绍

[英]The current position of this buffer. Position is always no less than zero and no greater than limit.
[中]此缓冲区的当前位置。位置始终不小于零且不大于limit

代码示例

代码示例来源:origin: apache/incubator-dubbo

@Override
public void setBytes(int index, byte[] src, int srcIndex, int length) {
  ByteBuffer data = buffer.duplicate();
  data.limit(index + length).position(index);
  data.put(src, srcIndex, length);
}

代码示例来源:origin: apache/incubator-dubbo

@Override
public void setBytes(int index, ByteBuffer src) {
  ByteBuffer data = buffer.duplicate();
  data.limit(index + src.remaining()).position(index);
  data.put(src);
}

代码示例来源:origin: apache/incubator-dubbo

@Override
public void getBytes(int index, byte[] dst, int dstIndex, int length) {
  ByteBuffer data = buffer.duplicate();
  try {
    data.limit(index + length).position(index);
  } catch (IllegalArgumentException e) {
    throw new IndexOutOfBoundsException();
  }
  data.get(dst, dstIndex, length);
}

代码示例来源:origin: netty/netty

private void getBytes(int index, byte[] dst, int dstIndex, int length, boolean internal) {
  checkDstIndex(index, length, dstIndex, dst.length);
  ByteBuffer tmpBuf;
  if (internal) {
    tmpBuf = internalNioBuffer();
  } else {
    tmpBuf = buffer.duplicate();
  }
  tmpBuf.clear().position(index).limit(index + length);
  tmpBuf.get(dst, dstIndex, length);
}

代码示例来源:origin: apache/kafka

if (in.remaining() < 6) {
  throw new IOException(PREMATURE_EOS);
flg = FLG.fromByte(in.get());
maxBlockSize = BD.fromByte(in.get()).getBlockMaximumSize();
  if (in.remaining() < 8) {
    throw new IOException(PREMATURE_EOS);
int len = in.position() - in.reset().position();
        CHECKSUM.hash(in, in.position(), len, 0);
in.position(in.position() + len);
if (in.get() != (byte) ((hash >> 8) & 0xFF)) {
  throw new IOException(DESCRIPTOR_HASH_MISMATCH);

代码示例来源:origin: netty/netty

@Override
public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
  ensureAccessible();
  if (length == 0) {
    return this;
  }
  if (buffer.hasArray()) {
    out.write(buffer.array(), index + buffer.arrayOffset(), length);
  } else {
    byte[] tmp = ByteBufUtil.threadLocalTempArray(length);
    ByteBuffer tmpBuf = internalNioBuffer();
    tmpBuf.clear().position(index);
    tmpBuf.get(tmp, 0, length);
    out.write(tmp, 0, length);
  }
  return this;
}

代码示例来源:origin: netty/netty

@Override
public int setBytes(int index, InputStream in, int length) throws IOException {
  ensureAccessible();
  if (buffer.hasArray()) {
    return in.read(buffer.array(), buffer.arrayOffset() + index, length);
  } else {
    byte[] tmp = ByteBufUtil.threadLocalTempArray(length);
    int readBytes = in.read(tmp, 0, length);
    if (readBytes <= 0) {
      return readBytes;
    }
    ByteBuffer tmpBuf = internalNioBuffer();
    tmpBuf.clear().position(index);
    tmpBuf.put(tmp, 0, readBytes);
    return readBytes;
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Try to read an EOL incrementing the buffer position if successful.
 * @return whether an EOL was consumed
 */
private boolean tryConsumeEndOfLine(ByteBuffer byteBuffer) {
  if (byteBuffer.remaining() > 0) {
    byte b = byteBuffer.get();
    if (b == '\n') {
      return true;
    }
    else if (b == '\r') {
      if (byteBuffer.remaining() > 0 && byteBuffer.get() == '\n') {
        return true;
      }
      else {
        throw new StompConversionException("'\\r' must be followed by '\\n'");
      }
    }
    // Explicit cast for compatibility with covariant return type on JDK 9's ByteBuffer
    ((Buffer) byteBuffer).position(byteBuffer.position() - 1);
  }
  return false;
}

代码示例来源:origin: stackoverflow.com

ByteBuffer outBuf = codecOutputBuffers[index];
outBuf.position(info.offset);
outBuf.limit(info.offset + outBitsSize);
try {
  byte[] data = new byte[outPacketSize];  //space for ADTS header included
  addADTStoPacket(data, outPacketSize);
  outBuf.get(data, 7, outBitsSize);
  outBuf.position(info.offset);
  mFileStream.write(data, 0, outPacketSize);  //open FileOutputStream beforehand
} catch (IOException e) {

代码示例来源:origin: stackoverflow.com

byte length = buffer.get();
if (length == 0) break;
byte type = buffer.get();
switch (type) {
  case 0x02: // Partial list of 16-bit UUIDs
    buffer.position(buffer.position() + length - 1);
    break;

代码示例来源:origin: netty/netty

@Override
public int setBytes(int index, FileChannel in, long position, int length) throws IOException {
  checkIndex(index, length);
  ByteBuffer tmpBuf = internalNioBuffer();
  index = idx(index);
  tmpBuf.clear().position(index).limit(index + length);
  try {
    return in.read(tmpBuf, position);
  } catch (ClosedChannelException ignored) {
    return -1;
  }
}

代码示例来源:origin: stackoverflow.com

cbRef.set(out);
        while(in.remaining()>0) {
          out.append((char)in.get());
charBuffer.position(0);
charBuffer.put("xyz");

代码示例来源:origin: spring-projects/spring-framework

@Override
public ByteBuffer asByteBuffer(int index, int length) {
  checkIndex(index, length);
  ByteBuffer duplicate = this.byteBuffer.duplicate();
  // Explicit access via Buffer base type for compatibility
  // with covariant return type on JDK 9's ByteBuffer...
  Buffer buffer = duplicate;
  buffer.position(index);
  buffer.limit(index + length);
  return duplicate.slice();
}

代码示例来源:origin: apache/incubator-dubbo

@Override
public void setBytes(int index, ByteBuffer src) {
  ByteBuffer data = buffer.duplicate();
  data.limit(index + src.remaining()).position(index);
  data.put(src);
}

代码示例来源:origin: netty/netty

private int getBytes(int index, FileChannel out, long position, int length, boolean internal) throws IOException {
  ensureAccessible();
  if (length == 0) {
    return 0;
  }
  ByteBuffer tmpBuf = internal ? internalNioBuffer() : buffer.duplicate();
  tmpBuf.clear().position(index).limit(index + length);
  return out.write(tmpBuf, position);
}

代码示例来源:origin: redisson/redisson

@Override
public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
  ensureAccessible();
  if (length == 0) {
    return this;
  }
  if (buffer.hasArray()) {
    out.write(buffer.array(), index + buffer.arrayOffset(), length);
  } else {
    byte[] tmp = ByteBufUtil.threadLocalTempArray(length);
    ByteBuffer tmpBuf = internalNioBuffer();
    tmpBuf.clear().position(index);
    tmpBuf.get(tmp, 0, length);
    out.write(tmp, 0, length);
  }
  return this;
}

代码示例来源:origin: netty/netty

@Override
public int setBytes(int index, InputStream in, int length) throws IOException {
  checkIndex(index, length);
  byte[] tmp = ByteBufUtil.threadLocalTempArray(length);
  int readBytes = in.read(tmp, 0, length);
  if (readBytes <= 0) {
    return readBytes;
  }
  ByteBuffer tmpBuf = internalNioBuffer();
  tmpBuf.clear().position(idx(index));
  tmpBuf.put(tmp, 0, readBytes);
  return readBytes;
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

if (n > 0) {
 n = Math.min(n, len);
 ((ByteBuffer) compressedDirectBuf).get(b, off, n);
 bytesWritten += n;
 return n;
compressedDirectBuf.limit(0);
if (0 == uncompressedDirectBuf.position()) {
 if (0 == uncompressedDirectBuf.position()) {
compressedDirectBuf.limit(n);
((ByteBuffer) compressedDirectBuf).get(b, off, n);

代码示例来源:origin: apache/incubator-dubbo

@Override
public void getBytes(int index, byte[] dst, int dstIndex, int length) {
  ByteBuffer data = buffer.duplicate();
  try {
    data.limit(index + length).position(index);
  } catch (IllegalArgumentException e) {
    throw new IndexOutOfBoundsException();
  }
  data.get(dst, dstIndex, length);
}

代码示例来源:origin: apache/incubator-dubbo

@Override
public void setBytes(int index, byte[] src, int srcIndex, int length) {
  ByteBuffer data = buffer.duplicate();
  data.limit(index + length).position(index);
  data.put(src, srcIndex, length);
}

相关文章