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

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

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

FileInputStream.getChannel介绍

[英]Returns a read-only FileChannel that shares its position with this stream.
[中]返回与此流共享其位置的只读文件通道。

代码示例

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

/**
 * Creates a new instance that fetches data from the specified file.
 *
 * @param chunkSize the number of bytes to fetch on each
 *                  {@link #readChunk(ChannelHandlerContext)} call
 */
public ChunkedNioFile(File in, int chunkSize) throws IOException {
  this(new FileInputStream(in).getChannel(), chunkSize);
}

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

public static void copyFile( File from, File to ) throws IOException {

  if ( !to.exists() ) { to.createNewFile(); }

  try (
    FileChannel in = new FileInputStream( from ).getChannel();
    FileChannel out = new FileOutputStream( to ).getChannel() ) {

    out.transferFrom( in, 0, in.size() );
  }
}

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

private static byte[] readBytesFromFileInputStream(FileInputStream fis) throws IOException
{
  FileChannel channel = fis.getChannel();
  int fileSize = (int) channel.size();
  ByteBuffer byteBuffer = ByteBuffer.allocate(fileSize);
  channel.read(byteBuffer);
  byteBuffer.flip();
  byte[] bytes = byteBuffer.array();
  byteBuffer.clear();
  channel.close();
  fis.close();
  return bytes;
}

代码示例来源:origin: twitter/ambrose

public static String readFile(String path) throws IOException {
 FileInputStream stream = new FileInputStream(new File(path));
 try {
  FileChannel fc = stream.getChannel();
  MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
  return Charset.defaultCharset().decode(bb).toString();
 } finally {
  stream.close();
 }
}

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

lastPosition.metricFileName = null;
lastPosition.indexFileName = null;
if (!new File(idxFileName).exists()) {
  return -1;
FileInputStream in = new FileInputStream(idxFileName);
in.getChannel().position(offsetInIndex);
DataInputStream indexIn = new DataInputStream(in);
long offset;
try {
  long second;
  lastPosition.offsetInIndex = in.getChannel().position();
  while ((second = indexIn.readLong()) < beginSecond) {
    offset = indexIn.readLong();
    lastPosition.offsetInIndex = in.getChannel().position();

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

public FileDataSourceViaHeapImpl(String f) throws FileNotFoundException {
  File file = new File(f);
  this.fc = new FileInputStream(file).getChannel();
  this.filename = file.getName();
}

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

File chunkFile = new File(tempFolder, Integer.toString(chunkId));
chunkFile.createNewFile();
  FileOutputStream stream = new FileOutputStream(chunkFile);
dataFiles.add(new LocalDataFileChunk(new FileInputStream(chunkFile).getChannel()));
dataFileSizes.add((int) chunkFile.length());

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

int showLogLength = Integer.parseInt(str[0]);
if (file != null && file.exists()) {
  try {
    FileInputStream fis = new FileInputStream(file);
    FileChannel filechannel = fis.getChannel();
    size = filechannel.size();
    ByteBuffer bb;
    if (size <= showLogLength) {

代码示例来源:origin: WVector/AppUpdate

public static String getFileMD5(File file) {
  if (!file.exists()) {
    return "";
  }
  FileInputStream in = null;
  try {
    in = new FileInputStream(file);
    FileChannel channel = in.getChannel();
    MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(buffer);
    return bytes2Hex(md.digest());
  } catch (NoSuchAlgorithmException | IOException e) {
    e.printStackTrace();
  } finally {
    if (in != null) {
      try {
        in.close();
      } catch (IOException ignored) {
      }
    }
  }
  return "";
}

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

public static long sizeViaChannel(String fname) throws IOException {
  FileInputStream fis = new FileInputStream(fname);
  FileChannel ch = fis.getChannel();
  return ch.size(); // nothing escapes, probably should report
}

代码示例来源:origin: looly/hutool

/**
 * 拷贝文件流,使用NIO
 * 
 * @param in 输入
 * @param out 输出
 * @return 拷贝的字节数
 * @throws IORuntimeException IO异常
 */
public static long copy(FileInputStream in, FileOutputStream out) throws IORuntimeException {
  Assert.notNull(in, "FileInputStream is null!");
  Assert.notNull(out, "FileOutputStream is null!");
  final FileChannel inChannel = in.getChannel();
  final FileChannel outChannel = out.getChannel();
  try {
    return inChannel.transferTo(0, inChannel.size(), outChannel);
  } catch (IOException e) {
    throw new IORuntimeException(e);
  }
}

代码示例来源:origin: lets-blade/blade

public static void copyFile(File source, File dest) throws IOException {
  try (FileChannel in = new FileInputStream(source).getChannel(); FileChannel out = new FileOutputStream(dest).getChannel()) {
    out.transferFrom(in, 0, in.size());
  }
}

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

public static void main(String[] args) throws IOException {
  System.out.println("PWD: " + System.getProperty("user.dir"));
  System.out.println("Input: " + args[0]);
  FileInputStream fis = new FileInputStream(new File(args[0]));
  System.out.println("input size: " + fis.getChannel().size());
  PrintStructure ps = new PrintStructure();
  ps.print(fis.getChannel(), 0, 0, 0);
}

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

if (!new File(lastPosition.indexFileName).exists()) {
  return false;
  in = new FileInputStream(lastPosition.indexFileName);
  in.getChannel().position(lastPosition.offsetInIndex);
  DataInputStream indexIn = new DataInputStream(in);

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

public FileDataSourceImpl(String f) throws FileNotFoundException {
  File file = new File(f);
  this.fc = new FileInputStream(file).getChannel();
  this.filename = file.getName();
}

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

int showLogLength = Integer.parseInt(str[0]);
if (file != null && file.exists()) {
  try {
    FileInputStream fis = new FileInputStream(file);
    FileChannel filechannel = fis.getChannel();
    size = filechannel.size();
    ByteBuffer bb;
    if (size <= showLogLength) {

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

File jobLog = getJobLog();
launchCount = 0; 
if(!jobLog.exists()) return;
    startPosition = jobLog.length()-(FileUtils.ONE_KB * 100);
  FileInputStream jobLogIn = new FileInputStream(jobLog);
  jobLogIn.getChannel().position(startPosition);
  BufferedReader jobLogReader = new BufferedReader(
      new InputStreamReader(jobLogIn));

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

/**
 * Creates a new instance that fetches data from the specified file.
 */
public ChunkedNioFile(File in) throws IOException {
  this(new FileInputStream(in).getChannel());
}

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

@NoWarning("OBL_UNSATISFIED_OBLIGATION,OS_OPEN_STREAM")
public static long sizeViaChannelCloseDoNotReport(String fname) throws IOException {
  FileInputStream fis = new FileInputStream(fname);
  FileChannel ch = fis.getChannel();
  long sz = ch.size();
  // fis.close();
  ch.close();
  return sz;
}

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

@Override
public byte[] read() throws IOException {
 Closer closer = Closer.create();
 try {
  FileInputStream in = closer.register(openStream());
  return ByteStreams.toByteArray(in, in.getChannel().size());
 } catch (Throwable e) {
  throw closer.rethrow(e);
 } finally {
  closer.close();
 }
}

相关文章