java.io.RandomAccessFile.readFully()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(7.5k)|赞(0)|评价(0)|浏览(445)

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

RandomAccessFile.readFully介绍

[英]Equivalent to readFully(dst, 0, dst.length);.
[中]相当于readfull(dst,0,dst.length);。

代码示例

代码示例来源:origin: prestodb/presto

@Override
  protected void readInternal(long position, byte[] buffer, int bufferOffset, int bufferLength)
      throws IOException
  {
    input.seek(position);
    input.readFully(buffer, bufferOffset, bufferLength);
  }
}

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

private byte[] readFile(File file) throws IOException {
  RandomAccessFile raf = new RandomAccessFile(file, "r");
  byte[] buffer = new byte[(int) raf.length()];
  raf.readFully(buffer);
  raf.close();
  return buffer;
}

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

public static byte[] readBytes(File file, int fixedLength) throws IOException {
  if (!file.exists()) {
    throw new FileNotFoundException(MSG_NOT_FOUND + file);
  }
  if (!file.isFile()) {
    throw new IOException(MSG_NOT_A_FILE + file);
  }
  long len = file.length();
  if (len >= Integer.MAX_VALUE) {
    throw new IOException("File is larger then max array size");
  }
  if (fixedLength > -1 && fixedLength < len) {
    len = fixedLength;
  }
  byte[] bytes = new byte[(int) len];
  RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
  randomAccessFile.readFully(bytes);
  randomAccessFile.close();
  return bytes;
}

代码示例来源:origin: jenkinsci/jenkins

try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
  long len = raf.length();
  raf.seek(pos);
  raf.readFully(tail);

代码示例来源:origin: nutzam/nutz

public static int readRange(File f, int pos, byte[] buf, int at, int len) {
  try {
    if (f == null || !f.exists())
      return 0;
    long fsize = f.length();
    if (pos > fsize)
      return 0;
    len = Math.min(len, buf.length - at);
    if (pos + len > fsize) {
      len = (int)(fsize - pos);
    }
    RandomAccessFile raf = new RandomAccessFile(f, "r");
    raf.seek(pos);
    raf.readFully(buf, at, len);
    raf.close();
    return len;
  }
  catch (IOException e) {
    return -1;
  }
}

代码示例来源:origin: internetarchive/heritrix3

StringBuffer sb = new StringBuffer();
try {
  endPos = raf.length();
  lastPos = endPos;
    raf.seek(endPos - 1);
    raf.read(oneByte);
    if ((char) oneByte[0] != '\n') {
      pos = lastPos - BUFFERSIZE;
    raf.seek(pos);
      buffer = new byte[remainer];
    raf.readFully(buffer);
    raf.seek(pos);
    if ((endPos - pos) < BUFFERSIZE) {
      int remainer = (int) (endPos - pos);
      buffer = new byte[remainer];
    raf.readFully(buffer);
    sb.append(new String(buffer));
  info = buildDisplayingHeader(sb.length(), raf.length());
} catch (FileNotFoundException e) {
  sb = null;

代码示例来源:origin: jphp-group/jphp

@Signature
public Memory readFully(Environment env, Memory... args){
  if (!canRead)
    throwCannotRead(env);
  long len = 0;
  try {
    len = accessFile.length() - position;
    if (len <= 0)
      return Memory.FALSE;
    byte[] buff = new byte[(int)len];
    accessFile.readFully(buff);
    position += len;
    return new BinaryMemory(buff);
  } catch (IOException e) {
    return Memory.FALSE;
  }
}

代码示例来源:origin: org.jsoup/jsoup

static ByteBuffer readFileToByteBuffer(File file) throws IOException {
  RandomAccessFile randomAccessFile = null;
  try {
    randomAccessFile = new RandomAccessFile(file, "r");
    byte[] bytes = new byte[(int) randomAccessFile.length()];
    randomAccessFile.readFully(bytes);
    return ByteBuffer.wrap(bytes);
  } finally {
    if (randomAccessFile != null)
      randomAccessFile.close();
  }
}

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

void addPatchFile(String zipFileName) throws IOException {
  File file = new File(zipFileName);
  RandomAccessFile f = new RandomAccessFile(file, "r");
  long fileLength = f.length();
  f.seek(0);
  f.seek(searchStart);
  ByteBuffer bbuf = ByteBuffer.allocate((int) readAmount);
  byte[] buffer = bbuf.array();
  f.readFully(buffer);
  bbuf.order(ByteOrder.LITTLE_ENDIAN);

代码示例来源:origin: prestodb/presto

@Override
public void readFully(long position, byte[] buffer, int bufferOffset, int bufferLength)
    throws IOException
{
  long start = System.nanoTime();
  input.seek(position);
  input.readFully(buffer, bufferOffset, bufferLength);
  readTimeNanos += System.nanoTime() - start;
  readBytes += bufferLength;
}

代码示例来源:origin: loklak/loklak_server

return null;
final RandomAccessFile raf = new RandomAccessFile(file, "r");
final byte[] a = new byte[mb];
try {
  raf.seek(0);
  raf.readFully(a, 0, mb);
  digest.update(a, 0, mb);
  raf.seek(fl - mb);
  raf.readFully(a, 0, mb);
  digest.update(a, 0, mb);
  digest.update(longToBytes(fl), 0, 8);

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

long scanOffset = raf.length() - ENDHDR;
if (scanOffset < 0) {
  throw new ZipException("File too short to be a zip file: " + raf.length());
raf.seek(0);
final int headerMagic = Integer.reverseBytes(raf.readInt());
if (headerMagic != LOCSIG) {
  raf.seek(scanOffset);
  if (Integer.reverseBytes(raf.readInt()) == ENDSIG) {
    break;
raf.readFully(eocd);
  raf.readFully(commentBytes);
  comment = new String(commentBytes, 0, commentBytes.length, StandardCharsets.UTF_8);

代码示例来源:origin: Bilibili/DanmakuFlameMaster

RandomAccessFile fp = null;
try {
  fp = new RandomAccessFile(libc, "r");
  fp.readFully(data);
  int machine = (data[19] << 8) | data[18];
  switch (machine) {

代码示例来源:origin: osmandapp/Osmand

long remain = raf.length() - raf.getFilePointer();
bufferSize = (int) Math.min(remain, buffer.length);
if(bufferSize > 0) {
  raf.readFully(buffer, 0, bufferSize);
} else {
  bufferSize = -1;

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

RandomAccessFile raf = new RandomAccessFile("d:\\host.pk8", "r");
byte[] buf = new byte[(int)raf.length()];
raf.readFully(buf);
raf.close();
PKCS8EncodedKeySpec kspec = new PKCS8EncodedKeySpec(buf);
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey privKey = kf.generatePrivate(kspec);

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

void addPatchFile(String zipFileName) throws IOException {
  File file = new File(zipFileName);
  RandomAccessFile f = new RandomAccessFile(file, "r");
  long fileLength = f.length();
  f.seek(0);
  f.seek(searchStart);
  ByteBuffer bbuf = ByteBuffer.allocate((int) readAmount);
  byte[] buffer = bbuf.array();
  f.readFully(buffer);
  bbuf.order(ByteOrder.LITTLE_ENDIAN);

代码示例来源:origin: smuyyh/BookReader

/**
 * Read len bytes from file beginning from offset. Since it's really a
 * ByteArrayInputStream, close() operation is optional
 */
private synchronized InputStream createInputStream(long offset, int len) throws IOException {
  fileAccess.seek(offset);
  byte[] b = new byte[len]; // TODO performance?
  fileAccess.readFully(b);
  return new ByteArrayInputStream(b);
}

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

if(!indexFile.exists() || !dataFile.exists())
  break;
RandomAccessFile index = new RandomAccessFile(indexFile, "r");
RandomAccessFile data = new RandomAccessFile(dataFile, "r");
try {
  while(true) {
    index.readFully(keyMd5);
    position = index.readInt();
    data.seek(position);
    int size = data.readInt();
    byte[] value = new byte[size];
    data.readFully(value);
    System.out.println(ByteUtils.toHexString(keyMd5) + "\t=>\t"
              + serializer.toObject(value).toString());

代码示例来源:origin: square/tape

this.zero = zero;
raf.seek(0);
raf.readFully(buffer);
if (fileLength > raf.length()) {
 throw new IOException(
   "File is truncated. Expected length: " + fileLength + ", Actual length: " + raf.length());
} else if (fileLength <= headerLength) {
 throw new IOException(

代码示例来源:origin: oblac/jodd

/**
 * Read file and returns byte array with contents.
 *
 * @param file  {@link File} to read
 * @param count number of bytes to read
 * @return byte array from {@link File} contents.
 * @throws IOException if not a {@link File} or {@link File} does not exist or file size is
 *                     larger than {@link Integer#MAX_VALUE}.
 */
public static byte[] readBytes(final File file, final int count) throws IOException {
  checkExists(file);
  checkIsFile(file);
  long numToRead = file.length();
  if (numToRead >= Integer.MAX_VALUE) {
    throw new IOException("File is larger then max array size");
  }
  if (count > NEGATIVE_ONE && count < numToRead) {
    numToRead = count;
  }
  byte[] bytes = new byte[(int) numToRead];
  RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
  randomAccessFile.readFully(bytes);
  randomAccessFile.close();
  return bytes;
}

相关文章