本文整理汇总了Java中java.nio.MappedByteBuffer.force方法的典型用法代码示例。如果您正苦于以下问题:Java MappedByteBuffer.force方法的具体用法?Java MappedByteBuffer.force怎么用?Java MappedByteBuffer.force使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.MappedByteBuffer
的用法示例。
在下文中一共展示了MappedByteBuffer.force方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeFileFromBytesByMap
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* 将字节数组写入文件
*
* @param file 文件
* @param bytes 字节数组
* @param append 是否追加在文件末
* @param isForce 是否写入文件
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
*/
public static boolean writeFileFromBytesByMap(final File file, final byte[] bytes, final boolean append, final boolean isForce) {
if (bytes == null || !createOrExistsFile(file)) return false;
FileChannel fc = null;
try {
fc = new FileOutputStream(file, append).getChannel();
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, fc.size(), bytes.length);
mbb.put(bytes);
if (isForce) mbb.force();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
CloseUtils.closeIO(fc);
}
}
示例2: writeFileFromBytesByMap
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* 将字节数组写入文件
*
* @param file 文件
* @param bytes 字节数组
* @param append 是否追加在文件末
* @param isForce 是否写入文件
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
*/
public static boolean writeFileFromBytesByMap(File file, final byte[] bytes, boolean append, boolean isForce) {
if (bytes == null || !FileUtils.createOrExistsFile(file)) return false;
if (!append && !FileUtils.createFileByDeleteOldFile(file)) return false;
FileChannel fc = null;
try {
fc = new RandomAccessFile(file, "rw").getChannel();
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, fc.size(), bytes.length);
mbb.put(bytes);
if (isForce) mbb.force();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
CloseUtils.closeIO(fc);
}
}
示例3: writeFileFromBytesByMap
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* 将字节数组写入文件
*
* @param file 文件
* @param bytes 字节数组
* @param append 是否追加在文件末
* @param isForce 是否写入文件
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
*/
public static boolean writeFileFromBytesByMap(File file, final byte[] bytes, boolean append, boolean isForce) {
if (bytes == null || !FileUtils.createOrExistsFile(file)) return false;
FileChannel fc = null;
try {
fc = new FileOutputStream(file, append).getChannel();
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, fc.size(), bytes.length);
mbb.put(bytes);
if (isForce) mbb.force();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
CloseUtils.closeIO(fc);
}
}
示例4: writeFileFromBytesByMap
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* 将字节数组写入文件
*
* @param file 文件
* @param bytes 字节数组
* @param append 是否追加在文件末
* @param isForce 是否写入文件
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
*/
public static boolean writeFileFromBytesByMap(final File file, final byte[] bytes, final boolean append, final boolean isForce) {
if (bytes == null || !createOrExistsFile(file)) {
return false;
}
FileChannel fc = null;
try {
fc = new FileOutputStream(file, append).getChannel();
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, fc.size(), bytes.length);
mbb.put(bytes);
if (isForce) {
mbb.force();
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
CloseUtils.closeIO(fc);
}
}
示例5: force
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
public static MappedByteBuffer force(MappedByteBuffer buf)
{
Preconditions.checkNotNull(buf);
if (SKIP_SYNC)
{
Object fd = null;
try
{
if (mbbFDField != null)
{
fd = mbbFDField.get(buf);
}
}
catch (Exception e)
{
throw new RuntimeException(e);
}
//This is what MappedByteBuffer.force() throws if a you call force() on an umapped buffer
if (mbbFDField != null && fd == null)
throw new UnsupportedOperationException();
return buf;
}
else
{
return buf.force();
}
}
示例6: wipeFile
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
public static void wipeFile(String file2wipe) throws IOException, FileNotFoundException
{
File f2w = new File(file2wipe);
if (f2w.exists())
{
SecureRandom sr = new SecureRandom();
RandomAccessFile raf = new RandomAccessFile(f2w, "rw");
FileChannel channel = raf.getChannel();
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, raf.length());
while (buffer.hasRemaining())
{
buffer.put((byte) 0);
}
buffer.force();
buffer.rewind();
while (buffer.hasRemaining())
{
buffer.put((byte) 0xFF);
}
buffer.force();
buffer.rewind();
byte[] data = new byte[1];
while (buffer.hasRemaining())
{
sr.nextBytes(data);
buffer.put(data[0]);
}
buffer.force();
raf.close();
f2w.delete();
}
}
示例7: close
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
private void close(RandomAccessFile file, Channel channel, MappedByteBuffer mappedFile) {
if (mappedFile == null) {
return;
}
// sync to disk
mappedFile.force();
try {
// close mappedFile
AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
Method cleaner = mappedFile.getClass().getMethod("cleaner");
cleaner.setAccessible(true);
((Cleaner) cleaner.invoke(mappedFile)).clean();
return null;
});
// close channel
if (channel != null && channel.isOpen()) {
channel.close();
}
// close file
if (file != null) {
file.close();
}
} catch (Exception e) {
logger.error(e.getMessage());
e.printStackTrace();
}
}