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

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

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

RandomAccessFile.getChannel介绍

[英]Gets this file's FileChannel object.

The file channel's FileChannel#position() is the same as this file's file pointer offset (see #getFilePointer()). Any changes made to this file's file pointer offset are also visible in the file channel's position and vice versa.
[中]获取此文件的FileChannel对象。
文件通道的FileChannel#position()与此文件的文件指针偏移量相同(请参见#getFilePointer()。对该文件的文件指针偏移量所做的任何更改也会显示在文件通道的位置上,反之亦然。

代码示例

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

@SuppressWarnings("resource")
private void createSpillingChannel() throws IOException {
  currentSpillFile = new File(tempDir, spillFilePrefix + (fileCounter++) + ".buffer");
  currentChannel = new RandomAccessFile(currentSpillFile, "rw").getChannel();
}

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

private static MappedByteBuffer mapInternal(File file, MapMode mode, long size)
  throws IOException {
 checkNotNull(file);
 checkNotNull(mode);
 Closer closer = Closer.create();
 try {
  RandomAccessFile raf =
    closer.register(new RandomAccessFile(file, mode == MapMode.READ_ONLY ? "r" : "rw"));
  FileChannel channel = closer.register(raf.getChannel());
  return channel.map(mode, 0, size == -1 ? channel.size() : size);
 } catch (Throwable e) {
  throw closer.rethrow(e);
 } finally {
  closer.close();
 }
}

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

MemoryMappedData(RandomAccessFile file, long length)
     throws IOException {
  FileChannel channel = file.getChannel();
  buf = channel.map(MAP_MODE, 0, length);
  channel.close();
}

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

public StarTreeIndexCombiner(File indexFile)
  throws IOException {
 Preconditions.checkState(!indexFile.exists(), "Star-tree index file already exists");
 _fileChannel = new RandomAccessFile(indexFile, "rw").getChannel();
}

代码示例来源:origin: cmusphinx/sphinx4

/** Lock the test suite so we can manipulate the set of tests */
private void lock() throws IOException {
  RandomAccessFile raf = new RandomAccessFile(lockFile, "rw");
  lock = raf.getChannel().lock();
  raf.close();
}

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

MappedByteBuffer mem =
 new RandomAccessFile("/tmp/mapped.txt", "rw").getChannel()
 .map(FileChannel.MapMode.READ_WRITE, 0, 1);

while(true){
 while(mem.get(0)!=5) Thread.sleep(0); // waiting for client request
 mem.put(0, (byte)10); // sending the reply
}

代码示例来源:origin: killme2008/Metamorphosis

/**
 * 캯ָļҽָָļβ
 * 
 * @param file
 * @throws IOException
 */
DataFile(final File file, final int number, final boolean force) throws IOException {
  this.file = file;
  this.fc = new RandomAccessFile(file, force ? "rws" : "rw").getChannel();
  // ָƵ
  this.fc.position(this.fc.size());
  this.currentPos = this.fc.position();
  this.number = number;
}

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

public static void truncateFile( File file, long position ) throws IOException
{
  try ( RandomAccessFile access = new RandomAccessFile( file, "rw" ) )
  {
    truncateFile( access.getChannel(), position );
  }
}

代码示例来源:origin: GlowstoneMC/Glowstone

public void close() throws IOException {
  file.getChannel().force(true);
  file.close();
}

代码示例来源:origin: SonarSource/sonarqube

public AllProcessesCommands(File directory) {
 if (!directory.isDirectory() || !directory.exists()) {
  throw new IllegalArgumentException("Not a valid directory: " + directory);
 }
 try {
  sharedMemory = new RandomAccessFile(new File(directory, "sharedmemory"), "rw");
  mappedByteBuffer = sharedMemory.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, MAX_SHARED_MEMORY);
 } catch (IOException e) {
  throw new IllegalArgumentException("Unable to create shared memory : ", e);
 }
}

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

private void whileLocked(Runnable runnable) {
 File lockFile = new File(System.getProperty("user.home"), ".robolectric-download-lock");
 try (RandomAccessFile raf = new RandomAccessFile(lockFile, "rw")) {
  try (FileChannel channel = raf.getChannel()) {
   try (FileLock ignored = channel.lock()) {
    runnable.run();
   }
  }
 } catch (IOException e) {
  throw new IllegalStateException("Couldn't create lock file " + lockFile, e);
 } finally {
  lockFile.delete();
 }
}

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

public InflightEventWrapper(File inflightEventsFile) throws Exception {
 if (!inflightEventsFile.exists()) {
  Preconditions.checkState(inflightEventsFile.createNewFile(), "Could not"
    + "create inflight events file: "
    + inflightEventsFile.getCanonicalPath());
 }
 this.inflightEventsFile = inflightEventsFile;
 file = new RandomAccessFile(inflightEventsFile, "rw");
 fileChannel = file.getChannel();
 digest = MessageDigest.getInstance("MD5");
}

代码示例来源:origin: bumptech/glide

public static void toFile(@NonNull ByteBuffer buffer, @NonNull File file) throws IOException {
 buffer.position(0);
 RandomAccessFile raf = null;
 FileChannel channel = null;
 try {
  raf = new RandomAccessFile(file, "rw");
  channel = raf.getChannel();
  channel.write(buffer);
  channel.force(false /*metadata*/);
  channel.close();
  raf.close();
 } finally {
  if (channel != null) {
   try {
    channel.close();
   } catch (IOException e) {
    // Ignored.
   }
  }
  if (raf != null) {
   try {
    raf.close();
   } catch (IOException e) {
    // Ignored.
   }
  }
 }
}

代码示例来源:origin: pxb1988/dex2jar

public ZipFile(File fd) throws IOException {
  RandomAccessFile randomAccessFile = new RandomAccessFile(fd, "r");
  file = randomAccessFile;
  raf = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, fd.length());
  readCentralDir();
}

代码示例来源:origin: sannies/mp4parser

public FileChannel splitFileAndInsert(File f, long pos, long length) throws IOException {
  FileChannel read = new RandomAccessFile(f, "r").getChannel();
  File tmp = File.createTempFile("ChangeMetaData", "splitFileAndInsert");
  FileChannel tmpWrite = new RandomAccessFile(tmp, "rw").getChannel();
  read.position(pos);
  tmpWrite.transferFrom(read, 0, read.size() - pos);
  read.close();
  FileChannel write = new RandomAccessFile(f, "rw").getChannel();
  write.position(pos + length);
  tmpWrite.position(0);
  long transferred = 0;
  while ((transferred += tmpWrite.transferTo(0, tmpWrite.size() - transferred, write)) != tmpWrite.size()) {
    System.out.println(transferred);
  }
  System.out.println(transferred);
  tmpWrite.close();
  tmp.delete();
  return write;
}

代码示例来源:origin: AsyncHttpClient/async-http-client

private FileChannel getChannel() throws IOException {
 if (channel == null) {
  channel = new RandomAccessFile(part.getFile(), "r").getChannel();
 }
 return channel;
}

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

private void mmapData() {
  if (buf == null && blockSize*blocks < Integer.MAX_VALUE) {
    try {
      buf = data.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, data.length());
      mappedSize = blockSize*blocks;
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }
}

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

public static Slice mapFileReadOnly(File file)
      throws IOException
  {
    requireNonNull(file, "file is null");

    if (!file.exists()) {
      throw new FileNotFoundException(file.toString());
    }

    try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
        FileChannel channel = randomAccessFile.getChannel()) {
      MappedByteBuffer byteBuffer = channel.map(MapMode.READ_ONLY, 0, file.length());
      return wrappedBuffer(byteBuffer);
    }
  }
}

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

public static void main(String[] args) throws IOException {
  // trying FileLock mechanics in different processes
  File file = new File("tmp.lock");
  file.createNewFile();
  FileChannel channel = new RandomAccessFile(file, "r").getChannel();
  boolean shared = true;
  FileLock lock1 = channel.tryLock(0, Long.MAX_VALUE, shared);
  System.out.println("locked " + lock1);
  System.in.read();
  System.out.println("release " + lock1);
  lock1.release();
}

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

private FileLock getFileLock() {
  if (fileLock == null) {
    try {
      final File storageDir = lockFile.getParentFile();
      if (!storageDir.mkdirs() && !storageDir.exists()) {
        return null;
      }
      if (input == null || fileChannel == null) {
        input = new RandomAccessFile(lockFile, "rw");
        fileChannel = input.getChannel();
      }
      fileLock = fileChannel.tryLock();
    } catch (final IOException e) {
      return null;
    } catch (final OverlappingFileLockException e) {
      return null;
    }
  }
  return fileLock;
}

相关文章