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

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

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

RandomAccessFile.read介绍

[英]Reads a single byte from the current position in this file and returns it as an integer in the range from 0 to 255. Returns -1 if the end of the file has been reached. Blocks until one byte has been read, the end of the file is detected, or an exception is thrown.
[中]从文件中的当前位置读取一个字节,并将其作为0到255之间的整数返回。如果已到达文件末尾,则返回-1。直到读取一个字节、检测到文件结尾或引发异常为止。

代码示例

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

@Override public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException {
  synchronized (sharedRaf) {
    final long length = endOffset - offset;
    if (byteCount > length) {
      byteCount = (int) length;
    }
    sharedRaf.seek(offset);
    int count = sharedRaf.read(buffer, byteOffset, byteCount);
    if (count > 0) {
      offset += count;
      return count;
    } else {
      return -1;
    }
  }
}
@Override public long skip(long byteCount) throws IOException {

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

/**
 * Given a zip File input it will return its size Only works for zip files whose uncompressed size is less than 4 GB, otherwise returns
 * the size module 2^32, per gzip specifications
 *
 * @param myFile The zip file as input
 * @return zip file size as a long
 *
 * @throws IOException
 */
public static long zipFileSize(File myFile) throws IOException {
  try (RandomAccessFile raf = new RandomAccessFile(myFile, "r")) {
    raf.seek(raf.length() - 4);
    long b4 = raf.read();
    long b3 = raf.read();
    long b2 = raf.read();
    long b1 = raf.read();
    return (b1 << 24) | (b2 << 16) + (b3 << 8) + b4;
  }
}

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

RandomAccessFile in = new RandomAccessFile("filename", "r");
int version = in.readInt();
byte type = in.readByte();
int beginOfData = in.readInt();
byte[] tempId;
in.read(tempId, 0, 16);
String id = new String(tempId);

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

public static boolean isEncrypted(File file) throws IOException {
  byte[] actualMagic = new byte[MultiCipherOutputStream.STREAM_MAGIC.length];
  RandomAccessFile rFile = new RandomAccessFile(file, "r");
  rFile.read(actualMagic);
  rFile.close();
  return Arrays.equals(actualMagic, MultiCipherOutputStream.STREAM_MAGIC);
}

代码示例来源:origin: org.apache.ant/ant

final byte[] sig) throws IOException {
boolean found = false;
long off = archive.length() - minDistanceFromEnd;
final long stopSearching =
  Math.max(0L, archive.length() - maxDistanceFromEnd);
if (off >= 0) {
  for (; off >= stopSearching; off--) {
    archive.seek(off);
    int curr = archive.read();
    if (curr == -1) {
      break;
      curr = archive.read();
      if (curr == sig[POS_1]) {
        curr = archive.read();
        if (curr == sig[POS_2]) {
          curr = archive.read();
          if (curr == sig[POS_3]) {
            found = true;
  archive.seek(off);

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

public Void invoke(File f, VirtualChannel channel) throws IOException {
    try (OutputStream os = p.getOut();
       OutputStream out = new java.util.zip.GZIPOutputStream(os, 8192);
       RandomAccessFile raf = new RandomAccessFile(reading(f), "r")) {
      raf.seek(offset);
      byte[] buf = new byte[8192];
      int len;
      while ((len = raf.read(buf)) >= 0) {
        out.write(buf, 0, len);
      }
      return null;
    }
  }
});

代码示例来源: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') {
      numOfLines++;
      pos = lastPos - BUFFERSIZE;
    raf.seek(pos);
    raf.seek(pos);
    if ((endPos - pos) < BUFFERSIZE) {
      int remainer = (int) (endPos - pos);
  info = buildDisplayingHeader(sb.length(), raf.length());
} catch (FileNotFoundException e) {
  sb = null;
  try {
    if (raf != null) {
      raf.close();

代码示例来源:origin: ahmetaa/zemberek-nlp

public ByteGramProvider(File file) throws IOException {
 RandomAccessFile raf = new RandomAccessFile(file, "r");
 this.order = raf.readInt();
 this.ngramCount = raf.readInt();
 final int byteAmount = order * ngramCount * 4;
 data = new byte[byteAmount];
 int actual = raf.read(data);
 if (actual != byteAmount) {
  throw new IllegalStateException(
    "File suppose to have " + byteAmount + " bytes for " + ngramCount + " ngrams");
 }
 raf.close();
}

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

if (oldFile.read(oldBuffer, 0, ctrl[0]) < ctrl[0]) {
      outStream.close();
      return RETURN_DIFF_FILE_ERR;
    oldFile.seek(oldpos);
  extraBlockIn.close();
} finally {
  oldFile.close();
  outStream.close();

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

public static boolean isGZipped(File file)
{
  try (RandomAccessFile inputFile = new RandomAccessFile(file, "r")) {
    int magic = inputFile.read() & 0xff | ((inputFile.read() << 8) & 0xff00);
    return magic == GZIP_MAGIC;
  }
  catch (IOException e) {
    throw new PrestoException(LOCAL_FILE_READ_ERROR, "Error reading file: " + file.getName(), e);
  }
}

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

mFileSizes.add(file.length());
final int sourceNameLength = baseFile.readInt();
final byte[] nameData = new byte[sourceNameLength];
baseFile.read(nameData, 0, sourceNameLength);

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

private void readFile() throws IOException {
 if ((raf.length() - raf.getFilePointer()) < BUFFER_SIZE) {
  buffer = new byte[(int) (raf.length() - raf.getFilePointer())];
 } else {
  buffer = new byte[BUFFER_SIZE];
 }
 raf.read(buffer, 0, buffer.length);
 bufferPos = 0;
}

代码示例来源:origin: ahmetaa/zemberek-nlp

public boolean hasNext() {
 try {
  readByteAmount = raf.read(data);
  if (readByteAmount > 0) {
   if (readByteAmount < chunkByteSize) {
    data = Arrays.copyOf(data, readByteAmount);
   }
   return true;
  } else {
   raf.close();
   return false;
  }
 } catch (IOException e) {
  e.printStackTrace();
 }
 return false;
}

代码示例来源:origin: alibaba/jstorm

/**
 * Given a zip File input it will return its size
 * Only works for zip files whose uncompressed size is less than 4 GB,
 * otherwise returns the size module 2^32, per gzip specifications
 *
 * @param myFile The zip file as input
 * @return zip file size as a long
 * @throws IOException
 */
public static long zipFileSize(File myFile) throws IOException {
  RandomAccessFile raf = new RandomAccessFile(myFile, "r");
  raf.seek(raf.length() - 4);
  long b4 = raf.read();
  long b3 = raf.read();
  long b2 = raf.read();
  long b1 = raf.read();
  long val = (b1 << 24) | (b2 << 16) + (b3 << 8) + b4;
  raf.close();
  return val;
}

代码示例来源:origin: scouter-project/scouter

@RequestHandler(RequestCmd.OBJECT_DOWNLOAD_HEAP_DUMP)
public Pack downloadHeapDump(Pack param, DataInputX in, DataOutputX out) {
  int buff = 2 * 1024 * 1024;
  File downloadFile = new File(folderName + "/" + ((MapPack) param).getText("fileName"));
  try {
    RandomAccessFile raf = new RandomAccessFile(downloadFile, "r");
    byte[] buffer = new byte[buff];
    int read = 0;
    long offset = downloadFile.length();
    int unitsize;
    while (read < offset) {
      unitsize = (int) (((offset - read) >= buff) ? buff : (offset - read));
      raf.read(buffer, 0, unitsize);
      out.writeByte(TcpFlag.HasNEXT);
      out.writeBlob(buffer, 0, unitsize);
      read += unitsize;
    }
    raf.close();
    return null;
  } catch (Throwable e) {
    e.printStackTrace();
  }
  return null;
}

代码示例来源:origin: alibaba/mdrill

byte[] sig) throws IOException {
boolean found = false;
long off = archive.length() - minDistanceFromEnd;
final long stopSearching =
  Math.max(0L, archive.length() - maxDistanceFromEnd);
if (off >= 0) {
  for (; off >= stopSearching; off--) {
    archive.seek(off);
    int curr = archive.read();
    if (curr == -1) {
      break;
      curr = archive.read();
      if (curr == sig[POS_1]) {
        curr = archive.read();
        if (curr == sig[POS_2]) {
          curr = archive.read();
          if (curr == sig[POS_3]) {
            found = true;
  archive.seek(off);

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

public byte[] getDiskBound() throws IOException {
  if (diskBound == null && diskBoundLocation != -1) {
    diskBound = new byte[length];
    try(RandomAccessFile file = new RandomAccessFile(tmpFile, "r")) {
      file.seek(diskBoundLocation);
      file.read(diskBound);
    }
    diskBoundLocation = -1;
  }
  return diskBound;
}

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

private int fillBuf() throws IOException {
  super.seek(this.bufStartPos);
  this.bufDirty = false;
  return super.read(this.buf);
}

代码示例来源:origin: i2p/i2p.i2p

byte[] delim = new byte[3];
file.seek(file.length() - 3);
for (int i = 0; i < STRUCTURE_INFO_MAX_SIZE; i++) {
  int read = file.read(delim);
  if (read == 3 && (delim[0] & 0xFF) == 255
      && (delim[1] & 0xFF) == 255 && (delim[2] & 0xFF) == 255) {
    break;
  file.seek(file.getFilePointer() - 4);
  file.seek(file.getFilePointer() - 6);
} else {
  file.seek(file.length() - 3);

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

public int readAllInOnce(ByteBuffer byteBuffer) throws IOException {
  if (byteBuffer.remaining() > raf.length()) {
    throw new IOException("trying to readAllInOnce past end of stream");
  }
  byte[] buf = new byte[byteBuffer.remaining()];
  int read = raf.read(buf);
  byteBuffer.put(buf, 0, read);
  return read;
}

相关文章