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

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

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

FileChannel.size介绍

[英]Returns the size of the file underlying this channel in bytes.
[中]返回此通道下的文件大小(字节)。

代码示例

代码示例来源: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: alibaba/Sentinel

private boolean validSize() throws Exception {
  long size = outMetric.getChannel().size();
  return size < singleFileSize;
}

代码示例来源: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();
 }
}

代码示例来源: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: apache/nifi

} else {
  final String filename = tailFile;
  final File file = new File(filename);
    final long timestamp = file.lastModified();
    try (final InputStream fis = new FileInputStream(file);
        final CheckedInputStream in = new CheckedInputStream(fis, checksum)) {
      StreamUtils.copy(in, new NullOutputStream(), position);
file = new File(tailFile);
reader = createReader(file, position);
if (reader == null) {
} else {
  try {
    final long readerSize = reader.size();
    final long readerPosition = reader.position();

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

FileChannel toChannel = null;
try {
  fromChannel = fromFile.getChannel();
  toChannel = toFile.getChannel();
  fromChannel.transferTo(0, fromChannel.size(), toChannel);
} finally {
  try {

代码示例来源: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: 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: 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: 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: aragozin/jvm-tools

HprofMappedByteBuffer(File dumpFile) throws IOException {
  FileInputStream fis = new FileInputStream(dumpFile);
  FileChannel channel = fis.getChannel();
  length = channel.size();
  dumpBuffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, length);
  channel.close();
  readHeader();
}

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

public static ByteArrayFileStream createByteArrayFileStream(FileInputStream fileInputStream) throws IOException
{
  FileChannel channel = fileInputStream.getChannel();
  long size = channel.size();
  int bufferSize = (int) Math.min(1048576, size);
  ByteBuffer byteBuffer = ByteBuffer.allocate(bufferSize);
  if (channel.read(byteBuffer) == size)
  {
    channel.close();
    channel = null;
  }
  byteBuffer.flip();
  byte[] bytes = byteBuffer.array();
  return new ByteArrayFileStream(bytes, bufferSize, channel);
}

代码示例来源: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: stackoverflow.com

public void copy(File src, File dst) throws IOException {
  FileInputStream inStream = new FileInputStream(src);
  FileOutputStream outStream = new FileOutputStream(dst);
  FileChannel inChannel = inStream.getChannel();
  FileChannel outChannel = outStream.getChannel();
  inChannel.transferTo(0, inChannel.size(), outChannel);
  inStream.close();
  outStream.close();
}

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

HprofLongMappedByteBuffer(File dumpFile) throws IOException {
  FileInputStream fis = new FileInputStream(dumpFile);
  FileChannel channel = fis.getChannel();
  length = channel.size();
  dumpBuffer = new MappedByteBuffer[(int) (((length + BUFFER_SIZE) - 1) / BUFFER_SIZE)];
  for (int i = 0; i < dumpBuffer.length; i++) {
    long position = i * BUFFER_SIZE;
    long size = Math.min(BUFFER_SIZE + BUFFER_EXT, length - position);
    dumpBuffer[i] = channel.map(FileChannel.MapMode.READ_ONLY, position, size);
  }
  channel.close();
  readHeader();
}

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

@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();
 }
}

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * A raw file copy function -- this is not public since no error checks are made as to the
 * consistency of the file being copied. Use instead:
 * @see IOUtils#cp(java.io.File, java.io.File, boolean)
 * @param source The source file. This is guaranteed to exist, and is guaranteed to be a file.
 * @param target The target file.
 * @throws IOException Throws an exception if the copy fails.
 */
private static void copyFile(File source, File target) throws IOException {
 FileChannel sourceChannel = new FileInputStream( source ).getChannel();
 FileChannel targetChannel = new FileOutputStream( target ).getChannel();
 // allow for the case that it doesn't all transfer in one go (though it probably does for a file cp)
 long pos = 0;
 long toCopy = sourceChannel.size();
 while (toCopy > 0) {
  long bytes = sourceChannel.transferTo(pos, toCopy, targetChannel);
  pos += bytes;
  toCopy -= bytes;
 }
 sourceChannel.close();
 targetChannel.close();
}

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

final FileChannel channel = new FileInputStream(fileName).getChannel();
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

// when finished
channel.close();

代码示例来源:origin: yanzhenjie/NoHttp

@Override
public long getBinaryLength() {
  try {
    if (inputStream instanceof FileInputStream)
      return ((FileInputStream) inputStream).getChannel().size();
    return inputStream.available();
  } catch (IOException e) {
    Logger.e(e);
  }
  return 0;
}

相关文章