本文整理汇总了Java中java.nio.MappedByteBuffer.remaining方法的典型用法代码示例。如果您正苦于以下问题:Java MappedByteBuffer.remaining方法的具体用法?Java MappedByteBuffer.remaining怎么用?Java MappedByteBuffer.remaining使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.MappedByteBuffer
的用法示例。
在下文中一共展示了MappedByteBuffer.remaining方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readFile2BytesByMap
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* 读取文件到字符数组中
*
* @param file 文件
* @return 字符数组
*/
public static byte[] readFile2BytesByMap(File file) {
if (file == null) return null;
FileChannel fc = null;
try {
fc = new RandomAccessFile(file, "r").getChannel();
MappedByteBuffer byteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).load();
byte[] result = new byte[(int) fc.size()];
if (byteBuffer.remaining() > 0) {
byteBuffer.get(result, 0, byteBuffer.remaining());
}
return result;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
CloseUtils.closeIO(fc);
}
}
示例2: put
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
public final void put(ByteBuffer byteBuffer) throws IOException {
try {
int index = getIndex();
long length = byteBuffer.limit() - byteBuffer.position();
this.position += length;
MappedByteBuffer mappedBuffer = bufferList.get(index);
if (mappedBuffer.remaining() < length) {
byte[] temp = new byte[mappedBuffer.remaining()];
byteBuffer.get(temp);
bufferList.get(index).put(temp);
bufferList.get(index + 1).put(byteBuffer);
} else {
bufferList.get(index).put(byteBuffer);
}
} catch (Exception e) {
throw new IOException("LargeMappedByteBuffer put rawPosition-"+rawPosition+" size-"+size, e);
}
}
示例3: mapFileIn
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* Converts the file into a byte[].
* @param infile The File you want to specify
* @return a byte array
* @throws IOException if something goes wrong reading the file.
*/
private byte[] mapFileIn(File infile) throws IOException{
FileInputStream fis = new FileInputStream(infile);
try{
FileChannel fc = fis.getChannel(); // Get the file's size and then map it into memory
int sz = (int)fc.size();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);
byte[] data2 = new byte[bb.remaining()];
bb.get(data2);
return data2;
}
finally{//Ensures resources are closed regardless of whether the action suceeded
fis.close();
}
}
示例4: toByteArray
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* 将文件转换为字节数组,
* 使用MappedByteBuffer,可以在处理大文件时,提升性能
*
* @param file 文件
* @return 二维码图片的字节数组
*/
private static byte[] toByteArray(File file) {
try (FileChannel fc = new RandomAccessFile(file, "r").getChannel();) {
MappedByteBuffer byteBuffer = fc.map(MapMode.READ_ONLY, 0, fc.size()).load();
byte[] result = new byte[(int) fc.size()];
if (byteBuffer.remaining() > 0) {
byteBuffer.get(result, 0, byteBuffer.remaining());
}
return result;
} catch (Exception e) {
logger.warn("文件转换成byte[]发生异常!", e);
return null;
}
}