本文整理匯總了Java中java.nio.MappedByteBuffer類的典型用法代碼示例。如果您正苦於以下問題:Java MappedByteBuffer類的具體用法?Java MappedByteBuffer怎麽用?Java MappedByteBuffer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
MappedByteBuffer類屬於java.nio包,在下文中一共展示了MappedByteBuffer類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: 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: call
import java.nio.MappedByteBuffer; //導入依賴的package包/類
public ByteBuffer call() throws Exception {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
buff = ByteBuffer.allocate(bufferSize);
serverSocketChannel.socket().bind(new InetSocketAddress(port));
while (!stop.isLocked()) {
RandomAccessFile temp = new RandomAccessFile(tempName, "rw");
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.read(buff);
FileChannel channel = temp.getChannel();
channel.write(buff);
if (!pause.isLocked()) {
MappedByteBuffer b = channel.map(MapMode.READ_WRITE, 0, (long) bufferSize);
b.clear();
}
temp.close();
buff.clear();
}
return null;
}
示例4: call
import java.nio.MappedByteBuffer; //導入依賴的package包/類
public ByteBuffer call() throws Exception {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
buff = ByteBuffer.allocate(bufferSize);
serverSocketChannel.socket().bind(new InetSocketAddress(port));
RandomAccessFile temp = new RandomAccessFile(tempName, "rw");
MappedByteBuffer b;
while (!stop.isLocked()) {
sync=0;
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.read(buff);
FileChannel channel = temp.getChannel();
channel.write(buff);
if (!pause.isLocked()) {
b = channel.map(MapMode.READ_WRITE, 0, (long) bufferSize);
sync = 1;
if(sync==2){
b.clear();
}
}
buff.clear();
}
temp.close();
return null;
}
示例5: copy
import java.nio.MappedByteBuffer; //導入依賴的package包/類
private void copy(File source, File destination) throws IOException {
FileChannel input = null, output = null;
try {
input = new FileInputStream(source).getChannel();
output = new FileOutputStream(destination).getChannel();
long size = input.size();
MappedByteBuffer buffer = input.map(FileChannel.MapMode.READ_ONLY, 0, size);
output.write(buffer);
} finally {
if (input != null)
input.close();
if (output != null)
output.close();
}
}
示例6: 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);
}
}
示例7: 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);
}
}
示例8: getBytes
import java.nio.MappedByteBuffer; //導入依賴的package包/類
/**
* 緩存中讀取字節數組
*
* @param key 鍵
* @return 字節數組
*/
public byte[] getBytes(String key) {
File file = mCacheManager.getFile(key);
if (!file.exists()) return null;
FileChannel fc = null;
try {
fc = new RandomAccessFile(file, "r").getChannel();
int size = (int) fc.size();
MappedByteBuffer byteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, size).load();
byte[] data = new byte[size];
byteBuffer.get(data, 0, size);
if (!CacheHelper.isDue(data)) {
return CacheHelper.getDataWithoutDueTime(data);
} else {
mCacheManager.remove(key);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
CloseUtils.closeIO(fc);
}
return null;
}
示例9: 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;
}
示例10: load
import java.nio.MappedByteBuffer; //導入依賴的package包/類
public void load() throws IOException {
logger.info("Loading page index...");
File idxFile = new File(dir, FILE_PREFIX.concat(INDEX_SUFFIX));
this.indexFile = new RandomAccessFile(idxFile, "rw");
this.indexFileChannel = indexFile.getChannel();
this.enqueueIndex = indexFileChannel.map(FileChannel.MapMode.READ_WRITE, 0, INDEX_SIZE);
this.dequeueIndex = (MappedByteBuffer) enqueueIndex.duplicate();
enqueueIndex.position(EN_NUM_OFFSET);
this.enqueuePageNumber = enqueueIndex.getInt();
enqueueIndex.position(EN_POS_OFFSET);
this.enqueuePosition = enqueueIndex.getInt();
enqueueIndex.position(EN_CNT_OFFSET);
this.enqueueCount = enqueueIndex.getInt();
dequeueIndex.position(DE_NUM_OFFSET);
this.dequeuePageNumber = enqueueIndex.getInt();
dequeueIndex.position(DE_POS_OFFSET);
this.dequeuePosition = enqueueIndex.getInt();
dequeueIndex.position(DE_CNT_OFFSET);
this.dequeueCount = enqueueIndex.getInt();
logger.info("Page index [{}] has successfully loaded.", idxFile.getPath());
// load en/dequeue page file
loadEnqueuePage(getPageFile(getPagePath(getEnqueuePageNumber())));
loadDequeuePage(getPageFile(getPagePath(getDequeuePageNumber())));
}
示例11: checkVersion
import java.nio.MappedByteBuffer; //導入依賴的package包/類
private boolean checkVersion(FileChannel channel) throws IOException
{
if (channel.size() > 0)
{
channel.position(0);
ByteBuffer buffer;
if (useNIOMemoryMapping)
{
MappedByteBuffer mbb = channel.map(MapMode.READ_ONLY, 0, 8);
mbb.load();
buffer = mbb;
}
else
{
buffer = ByteBuffer.wrap(new byte[8]);
channel.read(buffer);
buffer.position(0);
}
buffer.position(0);
long onDiskVersion = buffer.getLong();
return (version == onDiskVersion);
}
return (version == 0);
}
示例12: processRegions
import java.nio.MappedByteBuffer; //導入依賴的package包/類
private void processRegions(File file, RegionProcessor regionProcessor, String randomAccessFileMode, FileChannel.MapMode mapMode) throws IOException {
try (
RandomAccessFile randomAccessFile = new RandomAccessFile(file.getAbsolutePath().toFile(), randomAccessFileMode);
FileChannel channel = randomAccessFile.getChannel()
) {
RegionCalculator.calculateForSize(file, file.getSize());
for (Region region : file.getRegions().values()) {
Hasher hasher = Hashing.sha256().newHasher();
MappedByteBuffer mappedByteBuffer = channel.map(mapMode, region.getOffset(), region.getSize());
int sum = regionProcessor.processRegion(region, hasher, mappedByteBuffer);
region.setQuickDigest(sum);
byte[] slowDigest = hasher.hash().asBytes();
region.setSlowDigest(slowDigest);
clientMessageHandler.submitClientRegionMessage(clientId, file, region.getOffset(), region.getSize(), sum, slowDigest);
}
}
}
示例13: testMap
import java.nio.MappedByteBuffer; //導入依賴的package包/類
public void testMap() throws IOException {
// Test data
int size = 1024;
byte[] bytes = newPreFilledByteArray(size);
// Setup
File file = createTempFile();
Files.write(bytes, file);
// Test
MappedByteBuffer actual = Files.map(file);
// Verify
ByteBuffer expected = ByteBuffer.wrap(bytes);
assertTrue("ByteBuffers should be equal.", expected.equals(actual));
}
示例14: testMap_readWrite
import java.nio.MappedByteBuffer; //導入依賴的package包/類
public void testMap_readWrite() throws IOException {
// Test data
int size = 1024;
byte[] expectedBytes = new byte[size];
byte[] bytes = newPreFilledByteArray(1024);
// Setup
File file = createTempFile();
Files.write(bytes, file);
Random random = new Random();
random.nextBytes(expectedBytes);
// Test
MappedByteBuffer map = Files.map(file, MapMode.READ_WRITE);
map.put(expectedBytes);
// Verify
byte[] actualBytes = Files.toByteArray(file);
assertTrue(Arrays.equals(expectedBytes, actualBytes));
}
示例15: testMap_readWrite_creates
import java.nio.MappedByteBuffer; //導入依賴的package包/類
public void testMap_readWrite_creates() throws IOException {
// Test data
int size = 1024;
byte[] expectedBytes = newPreFilledByteArray(1024);
// Setup
File file = createTempFile();
boolean deleted = file.delete();
assertTrue(deleted);
assertFalse(file.exists());
// Test
MappedByteBuffer map = Files.map(file, MapMode.READ_WRITE, size);
map.put(expectedBytes);
// Verify
assertTrue(file.exists());
assertTrue(file.isFile());
assertEquals(size, file.length());
byte[] actualBytes = Files.toByteArray(file);
assertTrue(Arrays.equals(expectedBytes, actualBytes));
}