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

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

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

FileChannel.close介绍

暂无

代码示例

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

/**
 * 快速保存
 *
 * @param path
 * @param content
 * @return
 */
public static boolean saveTxt(String path, String content)
{
  try
  {
    FileChannel fc = new FileOutputStream(path).getChannel();
    fc.write(ByteBuffer.wrap(content.getBytes()));
    fc.close();
  }
  catch (Exception e)
  {
    logger.throwing("IOUtil", "saveTxt", e);
    logger.warning("IOUtil saveTxt 到" + path + "失败" + e.toString());
    return false;
  }
  return true;
}

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

public void getBox(WritableByteChannel writableByteChannel) throws IOException {
  writableByteChannel.write((ByteBuffer) header.rewind());
  FileChannel fc = new FileInputStream(dataFile).getChannel();
  fc.transferTo(0, dataFile.lastModified(), writableByteChannel);
  fc.close();
}

代码示例来源:origin: Meituan-Dianping/walle

FileOutputStream outStream = null;
  try {
    outStream = new FileOutputStream(tempCentralBytesFile);
    final byte[] buffer = new byte[1024];
  centralDirBytes = new byte[(int) (fileChannel.size() - centralDirStartOffset)];
  fIn.read(centralDirBytes);
  FileInputStream inputStream = null;
  try {
    inputStream = new FileInputStream(tempCentralBytesFile);
    final byte[] buffer = new byte[1024];
fIn.seek(fileChannel.size() - commentLength - 6);
fileChannel.close();

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

public synchronized void writeToConfigXmlFile(String content) {
  FileChannel channel = null;
  FileOutputStream outputStream = null;
  FileLock lock = null;
  try {
    RandomAccessFile randomAccessFile = new RandomAccessFile(fileLocation(), "rw");
    channel = randomAccessFile.getChannel();
    lock = channel.lock();
    randomAccessFile.seek(0);
    randomAccessFile.setLength(0);
    outputStream = new FileOutputStream(randomAccessFile.getFD());
    IOUtils.write(content, outputStream, UTF_8);
  } catch (Exception e) {
    throw new RuntimeException(e);
  } finally {
    if (channel != null && lock != null) {
      try {
        lock.release();
        channel.close();
        IOUtils.closeQuietly(outputStream);
      } catch (IOException e) {
        LOGGER.error("Error occured when releasing file lock and closing file.", e);
      }
    }
  }
}

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

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);
} else {
  try {
    final long readerSize = reader.size();
    final long readerPosition = reader.position();
  reader.close();
  getLogger().debug("Closed FileChannel {}", new Object[]{reader, reader});
} catch (final IOException ioe) {

代码示例来源:origin: com.h2database/h2

private void closeAndThrow(int id, FileChannel[] array, FileChannel o,
    long maxLength) throws IOException {
  String message = "Expected file length: " + maxLength + " got: " +
      o.size() + " for " + getName(id);
  for (FileChannel f : array) {
    f.close();
  }
  throw new IOException(message);
}

代码示例来源:origin: ZHENFENG13/My-Blog

public static void copyFileUsingFileChannels(File source, File dest) throws IOException {
  FileChannel inputChannel = null;
  FileChannel outputChannel = null;
  try {
    inputChannel = new FileInputStream(source).getChannel();
    outputChannel = new FileOutputStream(dest).getChannel();
    outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
  } finally {
    assert inputChannel != null;
    inputChannel.close();
    assert outputChannel != null;
    outputChannel.close();
  }
}

代码示例来源:origin: android-hacker/VirtualXposed

public static void writeToFile(byte[] data, File target) throws IOException {
  FileOutputStream fo = null;
  ReadableByteChannel src = null;
  FileChannel out = null;
  try {
    src = Channels.newChannel(new ByteArrayInputStream(data));
    fo = new FileOutputStream(target);
    out = fo.getChannel();
    out.transferFrom(src, 0, data.length);
  } finally {
    if (fo != null) {
      fo.close();
    }
    if (src != null) {
      src.close();
    }
    if (out != null) {
      out.close();
    }
  }
}

代码示例来源:origin: io.netty/netty

/**
 * Utility function
 * @return the array of bytes
 */
private static byte[] readFrom(File src) throws IOException {
  long srcsize = src.length();
  if (srcsize > Integer.MAX_VALUE) {
    throw new IllegalArgumentException(
        "File too big to be loaded in memory");
  }
  FileInputStream inputStream = new FileInputStream(src);
  FileChannel fileChannel = inputStream.getChannel();
  byte[] array = new byte[(int) srcsize];
  ByteBuffer byteBuffer = ByteBuffer.wrap(array);
  int read = 0;
  while (read < srcsize) {
    read += fileChannel.read(byteBuffer);
  }
  fileChannel.close();
  return array;
}

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

int availableBytes = (int) (fileChannel.size() - fileChannel.position());
ByteBuffer byteBuffer = ByteBuffer.allocate(Math.min(availableBytes, offset));
int readBytes = fileChannel.read(byteBuffer);
if (readBytes == availableBytes)
  fileChannel.close();
  fileChannel = null;

代码示例来源: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: mcxiaoke/packer-ng-plugin

public static void copyFile(File src, File dest) throws IOException {
  if (!dest.exists()) {
    dest.createNewFile();
  }
  FileChannel source = null;
  FileChannel destination = null;
  try {
    source = new FileInputStream(src).getChannel();
    destination = new FileOutputStream(dest).getChannel();
    destination.transferFrom(source, 0, source.size());
  } finally {
    if (source != null) {
      source.close();
    }
    if (destination != null) {
      destination.close();
    }
  }
}

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

@Override
public void truncate() throws IOException {
  if ( isOpen.get() ) {
    throw new IOException( "Cannot truncate while log is open for writing. Close the log then truncate." );
  }
  // Synchronize on isOpen to prevent re-opening while truncating (rare)
  synchronized ( isOpen ) {
    File results = new File( resultsFile.get() );
    FileChannel channel = new FileOutputStream( results, true ).getChannel();
    channel.truncate( 0 );
    channel.close();
    resultCount.set( 0 );
  }
}

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

@Override
public ByteBuf getChunk(int length) throws IOException {
  if (file == null || length == 0) {
    return EMPTY_BUFFER;
  }
  if (fileChannel == null) {
    FileInputStream inputStream = new FileInputStream(file);
    fileChannel = inputStream.getChannel();
  }
  int read = 0;
  ByteBuffer byteBuffer = ByteBuffer.allocate(length);
  while (read < length) {
    int readnow = fileChannel.read(byteBuffer);
    if (readnow == -1) {
      fileChannel.close();
      fileChannel = null;
      break;
    } else {
      read += readnow;
    }
  }
  if (read == 0) {
    return EMPTY_BUFFER;
  }
  byteBuffer.flip();
  ByteBuf buffer = wrappedBuffer(byteBuffer);
  buffer.readerIndex(0);
  buffer.writerIndex(read);
  return buffer;
}

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

private void closeAndThrow(int id, FileChannel[] array, FileChannel o, long maxLength) throws IOException {
  String message = "Expected file length: " + maxLength + " got: " + o.size() + " for " + getName(id);
  for (FileChannel f : array) {
    f.close();
  }
  throw new IOException(message);
}

相关文章