本文整理汇总了Java中java.nio.channels.FileChannel.force方法的典型用法代码示例。如果您正苦于以下问题:Java FileChannel.force方法的具体用法?Java FileChannel.force怎么用?Java FileChannel.force使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.FileChannel
的用法示例。
在下文中一共展示了FileChannel.force方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeFileFromBytesByChannel
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
/**
* 将字节数组写入文件
*
* @param file 文件
* @param bytes 字节数组
* @param append 是否追加在文件末
* @param isForce 是否写入文件
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
*/
public static boolean writeFileFromBytesByChannel(File file, final byte[] bytes, boolean append, boolean isForce) {
if (bytes == null) return false;
if (!append && !FileUtils.createFileByDeleteOldFile(file)) return false;
FileChannel fc = null;
try {
fc = new RandomAccessFile(file, "rw").getChannel();
fc.position(fc.size());
fc.write(ByteBuffer.wrap(bytes));
if (isForce) fc.force(true);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
CloseUtils.closeIO(fc);
}
}
示例2: writeFileFromBytesByChannel
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
/**
* 将字节数组写入文件
*
* @param file 文件
* @param bytes 字节数组
* @param append 是否追加在文件末
* @param isForce 是否写入文件
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
*/
public static boolean writeFileFromBytesByChannel(final File file, final byte[] bytes, final boolean append, final boolean isForce) {
if (bytes == null) return false;
FileChannel fc = null;
try {
fc = new FileOutputStream(file, append).getChannel();
fc.position(fc.size());
fc.write(ByteBuffer.wrap(bytes));
if (isForce) fc.force(true);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
CloseUtils.closeIO(fc);
}
}
示例3: 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);
}
}
示例4: writeFileFromBytesByChannel
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
/**
* 将字节数组写入文件
*
* @param file 文件
* @param bytes 字节数组
* @param append 是否追加在文件末
* @param isForce 是否写入文件
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
*/
public static boolean writeFileFromBytesByChannel(File file, final byte[] bytes, boolean append, boolean isForce) {
if (bytes == null) return false;
FileChannel fc = null;
try {
fc = new FileOutputStream(file, append).getChannel();
fc.position(fc.size());
fc.write(ByteBuffer.wrap(bytes));
if (isForce) fc.force(true);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
CloseUtils.closeIO(fc);
}
}
示例5: writeFileFromBytesByChannel
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
/**
* 将字节数组写入文件
*
* @param file 文件
* @param bytes 字节数组
* @param append 是否追加在文件末
* @param isForce 是否写入文件
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
*/
public static boolean writeFileFromBytesByChannel(final File file, final byte[] bytes, final boolean append, final boolean isForce) {
if (bytes == null) {
return false;
}
FileChannel fc = null;
try {
fc = new FileOutputStream(file, append).getChannel();
fc.position(fc.size());
fc.write(ByteBuffer.wrap(bytes));
if (isForce) {
fc.force(true);
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
CloseUtils.closeIO(fc);
}
}
示例6: create
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
public static TranslogWriter create(Type type, ShardId shardId, String translogUUID, long fileGeneration, Path file, Callback<ChannelReference> onClose, int bufferSize, ChannelFactory channelFactory) throws IOException {
final BytesRef ref = new BytesRef(translogUUID);
final int headerLength = getHeaderLength(ref.length);
final FileChannel channel = channelFactory.open(file);
try {
// This OutputStreamDataOutput is intentionally not closed because
// closing it will close the FileChannel
final OutputStreamDataOutput out = new OutputStreamDataOutput(java.nio.channels.Channels.newOutputStream(channel));
CodecUtil.writeHeader(out, TRANSLOG_CODEC, VERSION);
out.writeInt(ref.length);
out.writeBytes(ref.bytes, ref.offset, ref.length);
channel.force(true);
writeCheckpoint(headerLength, 0, file.getParent(), fileGeneration, StandardOpenOption.WRITE);
final TranslogWriter writer = type.create(shardId, fileGeneration, new ChannelReference(file, fileGeneration, channel, onClose), bufferSize);
return writer;
} catch (Throwable throwable){
// if we fail to bake the file-generation into the checkpoint we stick with the file and once we recover and that
// file exists we remove it. We only apply this logic to the checkpoint.generation+1 any other file with a higher generation is an error condition
IOUtils.closeWhileHandlingException(channel);
throw throwable;
}
}
示例7: 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);
}
}
示例8: writeFileFromBytes
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
private static void writeFileFromBytes(final File file, final byte[] bytes) {
FileChannel fc = null;
try {
fc = new FileOutputStream(file, false).getChannel();
fc.write(ByteBuffer.wrap(bytes));
fc.force(true);
} catch (IOException e) {
e.printStackTrace();
} finally {
CloseUtils.closeIO(fc);
}
}
示例9: trim
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
@Override
public void trim(long offset, long length) throws IOException {
final long offsetInThisBucket = offsetInThisBucket(offset);
final long lengthInThisBucket = calcLengthInThisBucket(offsetInThisBucket, length); //should be always equal to length since it is normalized in MineboxEport
final FileChannel channel = getOpenChannel();
final long fileSize = channel.size();
if (fileSize == 0 || offsetInThisBucket >= fileSize) {
//if the file is empty, there is nothing to trim
} else if (lengthInThisBucket == this.bucketSize) {
if (fileSize > 0)
//if we are trimming the whole bucket we can truncate to 0
{
synchronized (this) {
channel.truncate(0);
channel.force(true);
}
needsFlush = true;
}
} else if (offsetInThisBucket == 0 && lengthInThisBucket >= fileSize) {
//we are trimming the whole file, so we can truncate it.
synchronized (this) {
channel.truncate(0);
channel.force(true);
}
needsFlush = true;
} else if (offsetInThisBucket + lengthInThisBucket == this.bucketSize) {
//truncating from index until end, we can shorten the file now
synchronized (this) {
channel.truncate(offsetInThisBucket);
channel.force(false); //since we assume the un-truncated file was actually backed up, we don't care if this shortened file is not the one uploaded, since truncate is a "best effort" operations btrfs should tolerate those data being non-zero
}
needsFlush = true;
} else {
final int intLen = Ints.checkedCast(length); //buckets can not be bigger than 2GB right now, could be fixed
final ByteBuffer bb = ByteBuffer.allocate(intLen);
bb.put(new byte[intLen]);
bb.flip();
putBytesInternal(offset, bb, false); //sadly, this will encrypt zeroes. we need a workaround
}
}
示例10: truncateFileToConsistentSize
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
static void truncateFileToConsistentSize(FileOutputStream fos, long size) {
try {
FileChannel fch = fos.getChannel();
fch.truncate(size);
fch.force(true);
} catch (IOException ex) {
Logger.getLogger(DataConsistentFileOutputStream.class.getName()).log(
Level.INFO,
"Not able to truncate file to the data consistent size of "+size+" bytes.",
ex);
}
}
示例11: create
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
public static TranslogWriter create(
ShardId shardId,
String translogUUID,
long fileGeneration,
Path file,
ChannelFactory channelFactory,
ByteSizeValue bufferSize,
final LongSupplier globalCheckpointSupplier) throws IOException {
final BytesRef ref = new BytesRef(translogUUID);
final int headerLength = getHeaderLength(ref.length);
final FileChannel channel = channelFactory.open(file);
try {
// This OutputStreamDataOutput is intentionally not closed because
// closing it will close the FileChannel
final OutputStreamDataOutput out = new OutputStreamDataOutput(java.nio.channels.Channels.newOutputStream(channel));
writeHeader(out, ref);
channel.force(true);
final Checkpoint checkpoint =
Checkpoint.emptyTranslogCheckpoint(headerLength, fileGeneration, globalCheckpointSupplier.getAsLong());
writeCheckpoint(channelFactory, file.getParent(), checkpoint);
return new TranslogWriter(channelFactory, shardId, checkpoint, channel, file, bufferSize, globalCheckpointSupplier);
} catch (Exception exception) {
// if we fail to bake the file-generation into the checkpoint we stick with the file and once we recover and that
// file exists we remove it. We only apply this logic to the checkpoint.generation+1 any other file with a higher generation is an error condition
IOUtils.closeWhileHandlingException(channel);
throw exception;
}
}
示例12: writeFileFromBytes
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
private static void writeFileFromBytes(File file, byte[] bytes) {
FileChannel fc = null;
try {
fc = new FileOutputStream(file, false).getChannel();
fc.write(ByteBuffer.wrap(bytes));
fc.force(true);
} catch (IOException e) {
e.printStackTrace();
} finally {
CloseUtils.closeIO(fc);
}
}
示例13: force
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
public void force(boolean metaData) {
try {
for (FileChannel file : this.files) {
file.force(metaData);
}
} catch (IOException e) {
closeFileSilently();
throw TmpFileException.get(ErrorCode.ER_FILE_FORCE, e, name);
}
}
示例14: flush
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
@Override
public synchronized void flush() throws IOException {
super.flush();
FileChannel fch = fos.getChannel();
fch.force(true);
}
示例15: testReadableByteChannel
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
@Test
public void testReadableByteChannel() throws Exception {
int[] testSizes = { 0, 10, 1023, 1024, 1025, 2047, 2048, 2049 };
for (int size : testSizes) {
SelectorProvider sp = SelectorProvider.provider();
Pipe p = sp.openPipe();
Pipe.SinkChannel sink = p.sink();
Pipe.SourceChannel source = p.source();
sink.configureBlocking(false);
ByteBuffer outgoingdata = ByteBuffer.allocateDirect(size + 10);
byte[] someBytes = new byte[size + 10];
generator.nextBytes(someBytes);
outgoingdata.put(someBytes);
outgoingdata.flip();
int totalWritten = 0;
while (totalWritten < size + 10) {
int written = sink.write(outgoingdata);
if (written < 0)
throw new Exception("Write failed");
totalWritten += written;
}
File f = File.createTempFile("blah"+size, null);
f.deleteOnExit();
RandomAccessFile raf = new RandomAccessFile(f, "rw");
FileChannel fc = raf.getChannel();
long oldPosition = fc.position();
long bytesWritten = fc.transferFrom(source, 0, size);
fc.force(true);
if (bytesWritten != size)
throw new RuntimeException("Transfer failed");
if (fc.position() != oldPosition)
throw new RuntimeException("Position changed");
if (fc.size() != size)
throw new RuntimeException("Unexpected sink size "+ fc.size());
fc.close();
sink.close();
source.close();
f.delete();
}
}