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

x33g5p2x  于2022-01-18 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(285)

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

FileChannel.position介绍

[英]Returns the current value of the file position pointer.
[中]返回文件位置指针的当前值。

代码示例

代码示例来源:origin: org.apache.spark/spark-core

private long skipFromFileChannel(long n) throws IOException {
 long currentFilePosition = fileChannel.position();
 long size = fileChannel.size();
 if (n > size - currentFilePosition) {
  fileChannel.position(size);
  return size - currentFilePosition;
 } else {
  fileChannel.position(currentFilePosition + n);
  return n;
 }
}

代码示例来源:origin: ltsopensource/light-task-scheduler

/**
 * 读取指定位置数据
 */
public byte[] readData(long fromIndex, int length) throws IOException {
  fileChannel.position(fromIndex);
  ByteBuffer byteBuffer = ByteBuffer.allocate(length);
  fileChannel.read(byteBuffer);
  return byteBuffer.array();
}

代码示例来源:origin: aragozin/jvm-tools

@Override
protected int readPage(long offset, ByteBuffer bb) {
  try {
    channel.position(offset);
    channel.read(bb);
    bb.flip();
    return bb.remaining();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: hankcs/HanLP

int availableBytes = (int) (fileChannel.size() - fileChannel.position());
ByteBuffer byteBuffer = ByteBuffer.allocate(Math.min(availableBytes, offset));
int readBytes = fileChannel.read(byteBuffer);
if (readBytes == availableBytes)
byteBuffer.flip();
byte[] bytes = byteBuffer.array();
System.arraycopy(this.bytes, offset, this.bytes, offset - readBytes, bufferSize - offset);

代码示例来源:origin: Tencent/tinker

public ByteBuffer getSection(SectionHeader sectionHeader) throws IOException {
  final ByteBuffer result = ByteBuffer.allocate((int) sectionHeader.shSize);
  fis.getChannel().position(sectionHeader.shOffset);
  readUntilLimit(fis.getChannel(), result, "failed to read section: " + sectionHeader.shNameStr);
  return result;
}

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

ByteBuffer bb = olf.writeBuf;
if (bb != null && bb.position() != 0) {
 bb.flip();
 int flushed = 0;
 int numChannelRetries = 0;
  int channelBytesWritten = 0;
  final int bbStartPos = bb.position();
  final long channelStartPos = olf.channel.position();
  channelBytesWritten = olf.channel.write(bb);
    channelBytesWritten = (int) (olf.channel.position() - channelStartPos);
    bb.position(bbStartPos + channelBytesWritten);
   } else {
    throw new IOException("Failed to write Oplog entry to " + olf.f.getName() + ": "
      + "channel.write() returned " + channelBytesWritten + ", "
      + "change in channel position = " + (olf.channel.position() - channelStartPos)
      + ", " + "change in source buffer position = " + (bb.position() - bbStartPos));

代码示例来源:origin: liuyangming/ByteTCC

public void delete(CleanupRecord record) throws RuntimeException {
  record.setEnabled(false);
  record.setRecordFlag(0x0);
  try {
    ByteBuffer buffer = ByteBuffer.allocate(1);
    int startIndex = record.getStartIndex();
    this.channel.position(startIndex);
    buffer.put((byte) 0x0);
    buffer.flip();
    this.channel.write(buffer);
  } catch (Exception ex) {
    throw new IllegalStateException(ex);
  }
  this.unRegisterRecord(record); // unRegister record
}

代码示例来源:origin: google/guava

if (from instanceof FileChannel) {
 FileChannel sourceChannel = (FileChannel) from;
 long oldPosition = sourceChannel.position();
 long position = oldPosition;
 long copied;
  copied = sourceChannel.transferTo(position, ZERO_COPY_CHUNK_SIZE, to);
  position += copied;
  sourceChannel.position(position);
 } while (copied > 0 || position < sourceChannel.size());
 return position - oldPosition;
long total = 0;
while (from.read(buf) != -1) {
 buf.flip();
 while (buf.hasRemaining()) {
  total += to.write(buf);

代码示例来源:origin: ltsopensource/light-task-scheduler

@Override
public void read(FileChannel fileChannel) throws IOException {
  if (fileChannel.size() == 0) {
    return;
  }
  fileChannel.position(0);
  fileChannel.read(byteBuffer());
  byteBuffer().position(0);
  short readMagic = byteBuffer().getShort();
  if (readMagic != magic) {
    throw new IOException("Invalid file type magic number 0x" + Integer.toHexString(readMagic & 0xFFFF));
  }
  this.storeTxLogRecordId = byteBuffer().getLong();
}

代码示例来源:origin: aws/aws-sdk-java

RandomAccessFile randomAccessFile = new RandomAccessFile(destinationFile, "rw");
FileChannel channel = randomAccessFile.getChannel();
channel.position(position);
S3ObjectInputStream objectContent = null;
long filePosition;
    byteBuffer.flip();
      channel.write(byteBuffer);
  filePosition = channel.position();
} finally {
  IOUtils.closeQuietly(objectContent, LOG);

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

public boolean appendMessage(final byte[] data) {
  int currentPos = this.wrotePosition.get();
  if ((currentPos + data.length) <= this.fileSize) {
    try {
      this.fileChannel.position(currentPos);
      this.fileChannel.write(ByteBuffer.wrap(data));
    } catch (Throwable e) {
      log.error("Error occurred when append message to mappedFile.", e);
    }
    this.wrotePosition.addAndGet(data.length);
    return true;
  }
  return false;
}

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

@Override
public synchronized void seek(long newPos) throws IOException {
 logger.trace("Seek to position: {}", newPos);
 // check to see if we can seek within our existing buffer
 long relativeChange = newPos - position;
 if (relativeChange == 0) return; // seek to current pos => no-op
 long newBufPos = buf.position() + relativeChange;
 if (newBufPos >= 0 && newBufPos < buf.limit()) {
  // we can reuse the read buffer
  buf.position((int)newBufPos);
 } else {
  // otherwise, we have to invalidate the read buffer
  buf.clear();
  buf.flip();
 }
 // clear decoder state
 decoder.reset();
 // perform underlying file seek
 chan.position(newPos);
 // reset position pointers
 position = syncPosition = newPos;
}

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

public void cachePackage(final byte[] body) throws Exception {
  checkOpen();
  cachedChannel.position(cachedChannel.size());
  cachedChannel.write(ByteBuffer.wrap(body));
  close();
}

代码示例来源:origin: ltsopensource/light-task-scheduler

public byte[] readEntry(long position) throws IOException {
  fileChannel.position(position);
  entryBuffer.clear();
  fileChannel.read(entryBuffer);
  entryBuffer.position(0);
  byte readMagic = entryBuffer.get();
  if (readMagic != magic) {
    throw new IOException("Invalid entry type magic number 0x" + Integer.toHexString(readMagic & 0xFFFF));
  }
  int length = entryBuffer.getInt();
  byte[] entry = new byte[length];
  entryBuffer.get(entry);
  return entry;
}

代码示例来源:origin: Meituan-Dianping/walle

final long archiveSize = fileChannel.size();
if (archiveSize < ZIP_EOCD_REC_MIN_SIZE) {
  throw new IOException("APK too small for ZIP End of Central Directory (EOCD) record");
  final long eocdStartPos = eocdWithEmptyCommentStartPosition - expectedCommentLength;
  final ByteBuffer byteBuffer = ByteBuffer.allocate(4);
  fileChannel.position(eocdStartPos);
  fileChannel.read(byteBuffer);
  byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    final ByteBuffer commentLengthByteBuffer = ByteBuffer.allocate(2);
    fileChannel.position(eocdStartPos + ZIP_EOCD_COMMENT_LENGTH_FIELD_OFFSET);
    fileChannel.read(commentLengthByteBuffer);
    commentLengthByteBuffer.order(ByteOrder.LITTLE_ENDIAN);

代码示例来源:origin: Tencent/tinker

public ByteBuffer getSegment(ProgramHeader programHeader) throws IOException {
  final ByteBuffer result = ByteBuffer.allocate((int) programHeader.pFileSize);
  fis.getChannel().position(programHeader.pOffset);
  readUntilLimit(fis.getChannel(), result, "failed to read segment (type: " + programHeader.pType + ").");
  return result;
}

代码示例来源:origin: ltsopensource/light-task-scheduler

@Override
  public void write(FileChannel fileChannel) throws IOException {
    byteBuffer().position(0);
    byteBuffer().putShort(magic);
    byteBuffer().putLong(this.firstRecordId);
    byteBuffer().flip();

    fileChannel.position(0);
    fileChannel.write(byteBuffer());
    fileChannel.force(true);
  }
}

代码示例来源:origin: ltsopensource/light-task-scheduler

/**
 * 读取指定位置数据
 */
public byte[] readData(long fromIndex, int length) throws IOException {
  fileChannel.position(fromIndex);
  ByteBuffer byteBuffer = ByteBuffer.allocate(length);
  fileChannel.read(byteBuffer);
  return byteBuffer.array();
}

代码示例来源:origin: liuyangming/ByteTCC

public void forget(CleanupRecord record) throws RuntimeException {
  Xid xid = record.getXid();
  String resourceId = record.getResource();
  byte[] globalTransactionId = xid.getGlobalTransactionId();
  byte[] branchQualifier = xid.getBranchQualifier();
  byte[] keyByteArray = resourceId.getBytes();
  byte[] resourceByteArray = new byte[CONSTANTS_RES_ID_MAX_SIZE];
  System.arraycopy(keyByteArray, 0, resourceByteArray, 0, keyByteArray.length);
  ByteBuffer buffer = ByteBuffer.allocate(1 + CONSTANTS_RECORD_SIZE);
  buffer.put((byte) record.getRecordFlag());
  buffer.put(globalTransactionId);
  buffer.put(branchQualifier);
  buffer.put(resourceByteArray);
  buffer.flip();
  try {
    this.channel.position(record.getStartIndex());
    buffer.rewind();
    this.channel.write(buffer);
    buffer.rewind();
  } catch (Exception ex) {
    throw new IllegalStateException(ex);
  }
  byte recordFlag = buffer.get();
  buffer.rewind();
  this.registerRecord(buffer, recordFlag, record.getStartIndex());
}

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

private void refillBuf() throws IOException {
 buf.compact();
 chan.position(position); // ensure we read from the proper offset
 chan.read(buf);
 buf.flip();
}

相关文章