本文整理汇总了Java中java.nio.MappedByteBuffer.get方法的典型用法代码示例。如果您正苦于以下问题:Java MappedByteBuffer.get方法的具体用法?Java MappedByteBuffer.get怎么用?Java MappedByteBuffer.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.MappedByteBuffer
的用法示例。
在下文中一共展示了MappedByteBuffer.get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readFile2Bytes
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
private static byte[] readFile2Bytes(final File file) {
FileChannel fc = null;
try {
fc = new RandomAccessFile(file, "r").getChannel();
int size = (int) fc.size();
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, size).load();
byte[] data = new byte[size];
mbb.get(data, 0, size);
return data;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
CloseUtils.closeIO(fc);
}
}
示例2: testWrite
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* Maps blah file with a random offset and checks to see if data
* written out to the file can be read back in
*/
private static void testWrite() throws Exception {
StringBuilder sb = new StringBuilder();
sb.setLength(4);
for (int x=0; x<1000; x++) {
try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) {
FileChannel fc = raf.getChannel();
long offset = generator.nextInt(1000);
MappedByteBuffer b = fc.map(MapMode.READ_WRITE,
offset, 100);
for (int i=0; i<4; i++) {
b.put(i, (byte)('0' + i));
}
for (int i=0; i<4; i++) {
byte aByte = b.get(i);
sb.setCharAt(i, (char)aByte);
}
if (!sb.toString().equals("0123"))
throw new Exception("Write test failed");
}
}
}
示例3: readFile2BytesByMap
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* 读取文件到字节数组中
*
* @param file 文件
* @return 字符数组
*/
public static byte[] readFile2BytesByMap(File file) {
if (!FileUtils.isFileExists(file)) return null;
FileChannel fc = null;
try {
fc = new RandomAccessFile(file, "r").getChannel();
int size = (int) fc.size();
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, size).load();
byte[] result = new byte[size];
mbb.get(result, 0, size);
return result;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
CloseUtils.closeIO(fc);
}
}
示例4: readFile2Bytes
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
private static byte[] readFile2Bytes(File file) {
FileChannel fc = null;
try {
fc = new RandomAccessFile(file, "r").getChannel();
int size = (int) fc.size();
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, size).load();
byte[] data = new byte[size];
mbb.get(data, 0, size);
return data;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
CloseUtils.closeIO(fc);
}
}
示例5: readFile2BytesByMap
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* 读取文件到字节数组中
*
* @param file 文件
* @return 字符数组
*/
public static byte[] readFile2BytesByMap(final File file) {
if (!isFileExists(file)) return null;
FileChannel fc = null;
try {
fc = new RandomAccessFile(file, "r").getChannel();
int size = (int) fc.size();
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, size).load();
byte[] result = new byte[size];
mbb.get(result, 0, size);
return result;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
CloseUtils.closeIO(fc);
}
}
示例6: toBytes
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* file path to
*
* @param filepath
* @param sizes
* @return
*/
public static List<byte[]> toBytes(String filepath, int[] sizes) {
List<byte[]> result = new ArrayList<byte[]>();
try {
RandomAccessFile randomAccessFile = new RandomAccessFile(filepath, "r");
MappedByteBuffer buffer = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, randomAccessFile.length());
if (sizes != null && sizes.length > 0) {
for (int size : sizes) {
byte[] r = new byte[size];
buffer.get(r);//fill buffer
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
示例7: readFile2BytesByMap
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* 读取文件到字节数组中
*
* @param file 文件
* @return 字符数组
*/
public static byte[] readFile2BytesByMap(final File file) {
if (!isFileExists(file)) {
return null;
}
FileChannel fc = null;
try {
fc = new RandomAccessFile(file, "r").getChannel();
int size = (int) fc.size();
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, size).load();
byte[] result = new byte[size];
mbb.get(result, 0, size);
return result;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
CloseUtils.closeIO(fc);
}
}
示例8: testRead
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* Maps blah file with a random offset and checks to see if read
* from the ByteBuffer gets the right line number
*/
private static void testRead() throws Exception {
StringBuilder sb = new StringBuilder();
sb.setLength(4);
for (int x=0; x<1000; x++) {
try (FileInputStream fis = new FileInputStream(blah)) {
FileChannel fc = fis.getChannel();
long offset = generator.nextInt(10000);
long expectedResult = offset / CHARS_PER_LINE;
offset = expectedResult * CHARS_PER_LINE;
MappedByteBuffer b = fc.map(MapMode.READ_ONLY,
offset, 100);
for (int i=0; i<4; i++) {
byte aByte = b.get(i);
sb.setCharAt(i, (char)aByte);
}
int result = Integer.parseInt(sb.toString());
if (result != expectedResult) {
err.println("I expected "+expectedResult);
err.println("I got "+result);
throw new Exception("Read test failed");
}
}
}
}
示例9: insertTagAndShiftViaMappedByteBuffer
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* Insert new metadata into file by using memory mapped file
* <p>
* But this is problematic on 32bit systems for large flac files may not be able to map a contiguous address space large enough
* for a large audio size , so no longer used
*
* @param tag
* @param mappedFile
* @param fc
* @param targetSizeBeforeAudioData
* @param totalTargetSize
* @param blockInfo
* @param flacStream
* @param neededRoom
* @param availableRoom
* @throws IOException
* @throws UnsupportedEncodingException
*/
private void insertTagAndShiftViaMappedByteBuffer(Tag tag, MappedByteBuffer mappedFile, FileChannel fc, long targetSizeBeforeAudioData, long totalTargetSize, MetadataBlockInfo blockInfo, FlacStreamReader flacStream, int neededRoom, int availableRoom) throws IOException, UnsupportedEncodingException {
//Find end of metadata blacks (start of Audio)
int currentEndOfFilePosition = safeLongToInt(fc.size());
/*
* First shift data to the 'right' of the tag to the end of the file, whose position is currentEndOfTagsPosition
*/
int currentEndOfTagsPosition = safeLongToInt((targetSizeBeforeAudioData - FlacTagCreator.DEFAULT_PADDING) - neededRoom + availableRoom);
int lengthDiff = safeLongToInt(totalTargetSize - currentEndOfFilePosition);
final int BLOCK_SIZE = safeLongToInt(TagOptionSingleton.getInstance().getWriteChunkSize());
int currentPos = currentEndOfFilePosition - BLOCK_SIZE;
byte[] buffer = new byte[BLOCK_SIZE];
for (; currentPos >= currentEndOfTagsPosition; currentPos -= BLOCK_SIZE) {
mappedFile.position(currentPos);
mappedFile.get(buffer, 0, BLOCK_SIZE);
mappedFile.position(currentPos + lengthDiff);
mappedFile.put(buffer, 0, BLOCK_SIZE);
}
/*
* Final movement of start bytes. This also covers cases where BLOCK_SIZE is larger than the audio data
*/
int remainder = (currentPos + BLOCK_SIZE) - currentEndOfTagsPosition;
if (remainder > 0) {
mappedFile.position(currentEndOfTagsPosition);
mappedFile.get(buffer, 0, remainder);
mappedFile.position(currentEndOfTagsPosition + lengthDiff);
mappedFile.put(buffer, 0, remainder);
}
DirectByteBufferUtils.release(mappedFile);
/* Now overwrite the tag */
writeTags(tag, fc, blockInfo, flacStream);
}
示例10: LoadPathNodeFile
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
private void LoadPathNodeFile(byte rx,byte ry)
{
String fname = "./data/pathnode/"+rx+"_"+ry+".pn";
short regionoffset = getRegionOffset(rx,ry);
_log.info("PathFinding Engine: - Loading: "+fname+" -> region offset: "+regionoffset+"X: "+rx+" Y: "+ry);
File Pn = new File(fname);
int node = 0,size, index = 0;
try {
// Create a read-only memory-mapped file
FileChannel roChannel = new RandomAccessFile(Pn, "r").getChannel();
size = (int)roChannel.size();
MappedByteBuffer nodes;
if (Config.FORCE_GEODATA) //Force O/S to Loads this buffer's content into physical memory.
//it is not guarantee, because the underlying operating system may have paged out some of the buffer's data
nodes = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, size).load();
else
nodes = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, size);
// Indexing pathnode files, so we will know where each block starts
IntBuffer indexs = IntBuffer.allocate(65536);
while(node < 65536)
{
byte layer = nodes.get(index);
indexs.put(node, index);
node++;
index += layer*10+1;
}
_pathNodesIndex.put(regionoffset, indexs);
_pathNodes.put(regionoffset, nodes);
} catch (Exception e)
{
e.printStackTrace();
_log.warning("Failed to Load PathNode File: "+fname+"\n");
}
}
示例11: getBytesFromFile
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
private byte[] getBytesFromFile(String path) throws Throwable {
FileChannel channel = new RandomAccessFile(path, "r").getChannel();
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()).load();
byte[] bytes = new byte[(int) channel.size()];
if (buffer.hasRemaining()) {
buffer.get(bytes, 0, buffer.remaining());
}
channel.close();
return bytes;
}
示例12: mmap_read
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
static void mmap_read() throws Exception {
RandomAccessFile file = new RandomAccessFile("/dev/shm/cache", "rw");
MappedByteBuffer in = file.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1024);
byte[] arr = new byte[128];
in.get(arr);
System.out.println("mmap: " + new String(arr));
file.close();
}
示例13: get
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
synchronized void get(long position, byte[] chars) {
MappedByteBuffer buffer = dumpBuffer[getBufferIndex(position)];
buffer.position(getBufferOffset(position));
buffer.get(chars);
}
示例14: unpackBundleRaw
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* unpack a xxx.lvbundle
*
* @param file
* @param url
* @return
*/
public static ScriptBundle unpackBundleRaw(boolean isBytecode, final String url, final File file) throws IOException {
DebugUtil.tsi("luaviewp-unpackBundle-raw");
if (file == null || url == null) {
return null;
}
final ScriptBundle scriptBundle = new ScriptBundle();
final FileInputStream inputStream = new FileInputStream(file);
final MappedByteBuffer buffer = inputStream.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
final String scriptBundleFolderPath = LuaScriptManager.buildScriptBundleFolderPath(url);
scriptBundle.setUrl(url);
scriptBundle.setBytecode(isBytecode);
scriptBundle.setBaseFilePath(scriptBundleFolderPath);
int fileNameLen = 0;
int fileLen = 0;
String fileNameStr = null;
byte[] fileName, fileData, signData;
int count = buffer.getInt();
for (int i = 0; i < count; i++) {//all files
fileNameLen = buffer.getInt();// get file name
fileName = new byte[fileNameLen];
buffer.get(fileName);
fileLen = buffer.getInt();// get file data
fileData = new byte[fileLen];
buffer.get(fileData);
if (!isBytecode) {//非二进制的还有sign的data
fileLen = buffer.getInt();
signData = new byte[fileLen];
buffer.get(signData);
} else {
signData = null;
}
fileNameStr = new String(fileName);
if (fileNameStr == null || fileNameStr.indexOf("../") != -1) {
return null;
}
scriptBundle.addScript(new ScriptFile(url, scriptBundleFolderPath, fileNameStr, fileData, signData));
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
DebugUtil.tei("luaviewp-unpackBundle-raw");
return scriptBundle;
}
示例15: LoadPathNodeFile
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
private void LoadPathNodeFile(byte rx, byte ry)
{
if ((rx < L2World.TILE_X_MIN) || (rx > L2World.TILE_X_MAX) || (ry < L2World.TILE_Y_MIN) || (ry > L2World.TILE_Y_MAX))
{
_log.warning("Failed to Load PathNode File: invalid region " + rx + "," + ry + Config.EOL);
return;
}
final short regionoffset = getRegionOffset(rx, ry);
final File file = new File(Config.PATHNODE_DIR, rx + "_" + ry + ".pn");
_log.info("Path Engine: - Loading: " + file.getName() + " -> region offset: " + regionoffset + " X: " + rx + " Y: " + ry);
int node = 0, size, index = 0;
// Create a read-only memory-mapped file
try (RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel roChannel = raf.getChannel())
{
size = (int) roChannel.size();
MappedByteBuffer nodes;
if (Config.FORCE_GEODATA)
{
// it is not guarantee, because the underlying operating system may have paged out some of the buffer's data
nodes = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, size).load();
}
else
{
nodes = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, size);
}
// Indexing pathnode files, so we will know where each block starts
final IntBuffer indexs = IntBuffer.allocate(65536);
while (node < 65536)
{
final byte layer = nodes.get(index);
indexs.put(node++, index);
index += (layer * 10) + 1;
}
_pathNodesIndex.put(regionoffset, indexs);
_pathNodes.put(regionoffset, nodes);
}
catch (Exception e)
{
_log.log(Level.WARNING, "Failed to Load PathNode File: " + file.getAbsolutePath() + " : " + e.getMessage(), e);
}
}