本文整理汇总了Java中java.nio.channels.FileChannel.isOpen方法的典型用法代码示例。如果您正苦于以下问题:Java FileChannel.isOpen方法的具体用法?Java FileChannel.isOpen怎么用?Java FileChannel.isOpen使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.FileChannel
的用法示例。
在下文中一共展示了FileChannel.isOpen方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: flush
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
public void flush() {
if (!needsFlush) {
return;
}
needsFlush = false;
LOGGER.info("flushing bucket {}", bucketNumber);
try {
synchronized (this) {
final FileChannel openChannel = getOpenChannel();
if (openChannel.isOpen()) {
openChannel.force(wantsTimestampUpdate);
if (!wantsTimestampUpdate) {
Files.setLastModifiedTime(filePath, lastModifiedTime);
} else {
lastModifiedTime = FileTime.from(Instant.now());
}
wantsTimestampUpdate = false;
}
}
} catch (IOException e) {
LOGGER.warn("unable to flush file of bucket {}", bucketNumber);
}
}
示例2: write
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
/**
* Dispose of a ByteBuffer which has been acquired for writing by one of
* the write methods, writing its contents to the file.
*/
public int write (ByteBuffer bb) throws IOException {
synchronized (this) {
if (bb == buffer) {
buffer = null;
}
}
int position = size();
int byteCount = bb.position();
bb.flip();
FileChannel channel = writeChannel();
if (channel.isOpen()) { //If a thread was terminated while writing, it will be closed
Thread.interrupted(); // #186629: must clear interrupt flag or channel will be broken
channel.write (bb);
synchronized (this) {
bytesWritten += byteCount;
outstandingBufferCount--;
}
}
return position;
}
示例3: transfer
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
static public long transfer(long position, long count, FileInputStream src, FileOutputStream dst) throws IOException {
FileChannel srcChannel = src.getChannel();
FileChannel dstChannel = dst.getChannel();
if (!srcChannel.isOpen()) {
throw new ClosedChannelException();
}
if (!dstChannel.isOpen()) {
throw new ClosedChannelException();
}
if (position < 0 || count < 0) {
throw new IllegalArgumentException("position=" + position + " count=" + count);
}
if (count == 0 || position >= srcChannel.size()) {
return 0;
}
count = Math.min(count, srcChannel.size() - position);
FileDescriptor inFd = src.getFD();
FileDescriptor outFd = dst.getFD();
long rc = 0;
rc = native_sendfile_64(outFd, inFd, position, count);
return rc;
}
示例4: copiarArquivo
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
public void copiarArquivo(File source, File destination) throws IOException {
if (destination.exists()) {
destination.delete();
}
FileChannel sourceChannel = null;
FileChannel destinationChannel = null;
try {
sourceChannel = new FileInputStream(source).getChannel();
destinationChannel = new FileOutputStream(destination).getChannel();
sourceChannel.transferTo(0, sourceChannel.size(),
destinationChannel);
} finally {
if (sourceChannel != null && sourceChannel.isOpen()) {
sourceChannel.close();
}
if (destinationChannel != null && destinationChannel.isOpen()) {
destinationChannel.close();
}
}
}
示例5: force
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
public static void force(FileChannel fc, boolean metaData) throws IOException
{
Preconditions.checkNotNull(fc);
if (SKIP_SYNC)
{
if (!fc.isOpen())
throw new ClosedChannelException();
}
else
{
fc.force(metaData);
}
}
示例6: close
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
public void close() throws IOException {
LOGGER.debug("closing bucket {} ", bucketNumber);
final FileChannel openChannel = getOpenChannel();
if (openChannel.isOpen()) {
flush();
openChannel.close();
} else {
LOGGER.warn("closing bucket {} without an open channel.", bucketNumber);
}
randomAccessFile.close();
}
示例7: AsyncFileLock
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
/**
* Creates a new AsyncFileLock from the given FileChannel.
* The channel must have been opened with the WRITE option.
* @param file to lock.
* @throws IOException I/O error happened.
*/
public AsyncFileLock(FileChannel file) throws IOException
{
Objects.requireNonNull(file);
if (!file.isOpen())
{
throw new IllegalArgumentException("param `file` is closed");
}
fileToLock = file;
closeChannel = false;
}
示例8: copiarArquivos
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
/**
* Cria a cópia de um arquivo para o local passado como parâmetro
*
* @param origem caminho completo do arquivo a ser copiado
* @param destino caminho completo (com nome e extensão) do local de
* gravação
* @return {@code true} ou {@code false}
* @throws java.io.IOException
*
*/
public static boolean copiarArquivos(File origem, File destino) throws IOException {
if (destino.exists()) {
destino.delete();
}
FileChannel sourceChannel = null;
FileChannel destinationChannel = null;
try {
sourceChannel = new FileInputStream(origem).getChannel();
destinationChannel = new FileOutputStream(destino).getChannel();
sourceChannel.transferTo(0, sourceChannel.size(),
destinationChannel);
} catch (IOException ioe) {
return false;
} finally {
if (sourceChannel != null && sourceChannel.isOpen()) {
sourceChannel.close();
}
if (destinationChannel != null && destinationChannel.isOpen()) {
destinationChannel.close();
}
}
return true;
}
示例9: writeChannel
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
private FileChannel writeChannel() throws IOException {
FileChannel channel = fileChannel();
closed = !channel.isOpen();
return channel;
}
示例10: read
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
public void
read(
RandomAccessFile raf,
DirectByteBuffer buffer,
long offset )
throws FMFileManagerException
{
if (raf == null){
throw new FMFileManagerException( "read: raf is null" );
}
FileChannel fc = raf.getChannel();
if ( !fc.isOpen()){
Debug.out("FileChannel is closed: " + owner.getName());
throw( new FMFileManagerException( "read - file is closed"));
}
AEThread2.setDebug( owner );
try{
if(USE_MMAP)
{
long remainingInFile = fc.size()-offset;
long remainingInTargetBuffer = buffer.remaining(DirectByteBuffer.SS_FILE);
MappedByteBuffer buf = fc.map(MapMode.READ_ONLY, offset, Math.min(remainingInFile,remainingInTargetBuffer));
buffer.put(DirectByteBuffer.SS_FILE, buf);
} else {
fc.position(offset);
while (fc.position() < fc.size() && buffer.hasRemaining(DirectByteBuffer.SS_FILE))
buffer.read(DirectByteBuffer.SS_FILE,fc);
}
}catch ( Exception e ){
Debug.printStackTrace( e );
throw( new FMFileManagerException( "read fails", e ));
}
}