本文整理汇总了Java中java.nio.channels.FileChannel.MapMode类的典型用法代码示例。如果您正苦于以下问题:Java MapMode类的具体用法?Java MapMode怎么用?Java MapMode使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MapMode类属于java.nio.channels.FileChannel包,在下文中一共展示了MapMode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: SharedSystemData
import java.nio.channels.FileChannel.MapMode; //导入依赖的package包/类
/**
* Constructor.
* @throws IOException
*/
public SharedSystemData(String path, boolean create) throws IOException {
File f = new File(path);
if (create) {
if (f.exists()) {
System.out.println("Existing system detected, deleting");
f.delete(); // Delete if present.
}
} else {
if (!f.exists()) {
System.err.println("ERROR, system dont exist");
System.exit(-1);
}
}
channel = FileChannel.open(f.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
buffer = channel.map(MapMode.READ_WRITE, 0, 4000);
if (create) {
setNextTaskId(0);
setShutdownSignal(false);
}
}
示例2: call
import java.nio.channels.FileChannel.MapMode; //导入依赖的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;
}
示例3: call
import java.nio.channels.FileChannel.MapMode; //导入依赖的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;
}
示例4: map
import java.nio.channels.FileChannel.MapMode; //导入依赖的package包/类
/**
* Maps the heapPointer and code sections of the boot image in a given file into memory.
*
* @param bootImageFile the file containing the heapPointer and code sections to map into memory
* @return a {@link DataAccess} object that can be used to access the mapped sections
* @throws IOException if an IO error occurs while performing the memory mapping
*/
private DataAccess map(File bootImageFile, BootImage bootImage) throws IOException {
final RandomAccessFile randomAccessFile = new RandomAccessFile(bootImageFile, "rwd");
final Header header = bootImage.header;
int heapOffset = bootImage.heapOffset();
int heapAndCodeSize = header.heapSize + header.codeSize;
final MappedByteBuffer bootImageBuffer = randomAccessFile.getChannel().map(MapMode.PRIVATE, heapOffset, heapAndCodeSize);
bootImageBuffer.order(platform().endianness().asByteOrder());
randomAccessFile.close();
if (heapPointer.isNotZero()) {
long address = (Long) WithoutAccessCheck.getInstanceField(bootImageBuffer, "address");
bootImage.relocate(address, heapPointer);
}
return new MappedByteBufferDataAccess(bootImageBuffer, heapPointer, header.wordWidth());
}
示例5: checkVersion
import java.nio.channels.FileChannel.MapMode; //导入依赖的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);
}
示例6: StoreCheckpoint
import java.nio.channels.FileChannel.MapMode; //导入依赖的package包/类
public StoreCheckpoint(final String scpPath) throws IOException {
File file = new File(scpPath);
MappedFile.ensureDirOK(file.getParent());
boolean fileExists = file.exists();
this.randomAccessFile = new RandomAccessFile(file, "rw");
this.fileChannel = this.randomAccessFile.getChannel();
this.mappedByteBuffer = fileChannel.map(MapMode.READ_WRITE, 0, MappedFile.OS_PAGE_SIZE);
if (fileExists) {
log.info("store checkpoint file exists, " + scpPath);
this.physicMsgTimestamp = this.mappedByteBuffer.getLong(0);
this.logicsMsgTimestamp = this.mappedByteBuffer.getLong(8);
this.indexMsgTimestamp = this.mappedByteBuffer.getLong(16);
log.info("store checkpoint file physicMsgTimestamp " + this.physicMsgTimestamp + ", "
+ UtilAll.timeMillisToHumanString(this.physicMsgTimestamp));
log.info("store checkpoint file logicsMsgTimestamp " + this.logicsMsgTimestamp + ", "
+ UtilAll.timeMillisToHumanString(this.logicsMsgTimestamp));
log.info("store checkpoint file indexMsgTimestamp " + this.indexMsgTimestamp + ", "
+ UtilAll.timeMillisToHumanString(this.indexMsgTimestamp));
} else {
log.info("store checkpoint file not exists, " + scpPath);
}
}
示例7: testMap_readWrite
import java.nio.channels.FileChannel.MapMode; //导入依赖的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));
}
示例8: testMap_readWrite_creates
import java.nio.channels.FileChannel.MapMode; //导入依赖的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));
}
示例9: StoreCheckpoint
import java.nio.channels.FileChannel.MapMode; //导入依赖的package包/类
public StoreCheckpoint(final String scpPath) throws IOException {
File file = new File(scpPath);
MapedFile.ensureDirOK(file.getParent());
boolean fileExists = file.exists();
this.randomAccessFile = new RandomAccessFile(file, "rw");
this.fileChannel = this.randomAccessFile.getChannel();
this.mappedByteBuffer = fileChannel.map(MapMode.READ_WRITE, 0, MapedFile.OS_PAGE_SIZE);
if (fileExists) {
log.info("store checkpoint file exists, " + scpPath);
this.physicMsgTimestamp = this.mappedByteBuffer.getLong(0);
this.logicsMsgTimestamp = this.mappedByteBuffer.getLong(8);
this.indexMsgTimestamp = this.mappedByteBuffer.getLong(16);
log.info("store checkpoint file physicMsgTimestamp " + this.physicMsgTimestamp + ", "
+ UtilAll.timeMillisToHumanString(this.physicMsgTimestamp));
log.info("store checkpoint file logicsMsgTimestamp " + this.logicsMsgTimestamp + ", "
+ UtilAll.timeMillisToHumanString(this.logicsMsgTimestamp));
log.info("store checkpoint file indexMsgTimestamp " + this.indexMsgTimestamp + ", "
+ UtilAll.timeMillisToHumanString(this.indexMsgTimestamp));
}
else {
log.info("store checkpoint file not exists, " + scpPath);
}
}
示例10: insertTagAndShift
import java.nio.channels.FileChannel.MapMode; //导入依赖的package包/类
/**
* Insert new metadata into file by using memory mapped file, and if fails write in chunks
* <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 since better to go straight to using chunks
*
* @param tag
* @param fc
* @param blockInfo
* @param flacStream
* @param neededRoom
* @param availableRoom
* @throws IOException
* @throws UnsupportedEncodingException
*/
private void insertTagAndShift(File file, Tag tag, FileChannel fc, MetadataBlockInfo blockInfo, FlacStreamReader flacStream, int neededRoom, int availableRoom) throws IOException, UnsupportedEncodingException {
int headerLength = flacStream.getStartOfFlacInFile() + FlacStreamReader.FLAC_STREAM_IDENTIFIER_LENGTH + MetadataBlockHeader.HEADER_LENGTH // this should be the length of the block header for the stream info
+ MetadataBlockDataStreamInfo.STREAM_INFO_DATA_LENGTH;
long targetSizeBeforeAudioData = headerLength + neededRoom + FlacTagCreator.DEFAULT_PADDING;
long remainderTargetSize = fc.size() - (headerLength + availableRoom);
long totalTargetSize = targetSizeBeforeAudioData + remainderTargetSize;
MappedByteBuffer mappedFile = null;
try {
//Use ByteBuffer
mappedFile = fc.map(MapMode.READ_WRITE, 0, totalTargetSize);
insertTagAndShiftViaMappedByteBuffer(tag, mappedFile, fc, targetSizeBeforeAudioData, totalTargetSize, blockInfo, flacStream, neededRoom, availableRoom);
} catch (IOException ioe) {
//#175: Flac Map error on write
if (mappedFile == null) {
insertUsingChunks(file, tag, fc, blockInfo, flacStream, neededRoom + FlacTagCreator.DEFAULT_PADDING, availableRoom);
} else {
logger.log(Level.SEVERE, ioe.getMessage(), ioe);
throw ioe;
}
}
}
示例11: load
import java.nio.channels.FileChannel.MapMode; //导入依赖的package包/类
/**
* Load the block.
*
* mmap and mlock the block, and then verify its checksum.
*
* @param length The current length of the block.
* @param blockIn The block input stream. Should be positioned at the
* start. The caller must close this.
* @param metaIn The meta file input stream. Should be positioned at
* the start. The caller must close this.
* @param blockFileName The block file name, for logging purposes.
*
* @return The Mappable block.
*/
public static MappableBlock load(long length,
FileInputStream blockIn, FileInputStream metaIn,
String blockFileName) throws IOException {
MappableBlock mappableBlock = null;
MappedByteBuffer mmap = null;
FileChannel blockChannel = null;
try {
blockChannel = blockIn.getChannel();
if (blockChannel == null) {
throw new IOException("Block InputStream has no FileChannel.");
}
mmap = blockChannel.map(MapMode.READ_ONLY, 0, length);
NativeIO.POSIX.getCacheManipulator().mlock(blockFileName, mmap, length);
verifyChecksum(length, metaIn, blockChannel, blockFileName);
mappableBlock = new MappableBlock(mmap, length);
} finally {
IOUtils.closeQuietly(blockChannel);
if (mappableBlock == null) {
if (mmap != null) {
NativeIO.POSIX.munmap(mmap); // unmapping also unlocks
}
}
}
return mappableBlock;
}
示例12: testWrite
import java.nio.channels.FileChannel.MapMode; //导入依赖的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");
}
}
}
示例13: loadInformation
import java.nio.channels.FileChannel.MapMode; //导入依赖的package包/类
/**
* load bio information;egg : chromosome or dbsnp
*/
protected void loadInformation(String path) throws IOException {
RandomAccessFile raf = new RandomAccessFile(path, "r");
FileChannel fc = raf.getChannel();
fcSize = (int) (fc.size() & 0xffffffff);
int blocks = (int) ((fcSize / Integer.MAX_VALUE) + 1);
byteBuffer = new MappedByteBuffer[blocks];
int start = 0;
long remain = 0;
int size = 0;
for (int i = 0; i < blocks; i++) {
start = Integer.MAX_VALUE * i;
remain = (long) (fc.size() - start);
size = (int) ((remain > Integer.MAX_VALUE) ? Integer.MAX_VALUE : remain);
MappedByteBuffer mapedBB = fc.map(MapMode.READ_ONLY, start, size);
byteBuffer[i] = mapedBB;
}
raf.close();
}
示例14: allocate
import java.nio.channels.FileChannel.MapMode; //导入依赖的package包/类
/**
* on posix systems: allocates disk-backed bytebuffer and immediately unlinks the file
* on others: simply returns a direct bytebuffer
*/
public static ByteBuffer allocate(int size) {
if(MAP_AND_UNLINK_SUPPORTED) {
try {
Path p = Files.createTempFile("anon-mapping", ".tmp");
ByteBuffer mapped;
FileChannel chan = FileChannel.open(p, StandardOpenOption.READ, StandardOpenOption.WRITE);
chan.position(size);
chan.write(ByteBuffer.allocate(1));
mapped = chan.map(MapMode.READ_WRITE, 0, size);
chan.close();
Files.delete(p);
return mapped;
} catch (IOException e) {
e.printStackTrace();
}
}
return ByteBuffer.allocateDirect(size);
}
示例15: BootFile
import java.nio.channels.FileChannel.MapMode; //导入依赖的package包/类
BootFile(Path bootPath)
throws IOException
{
Objects.requireNonNull(bootPath);
_bootPath = bootPath;
long bootSize = Files.size(bootPath);
if (bootSize <= 0) {
throw new IllegalStateException("Unexpected boot size for " + bootPath);
}
if (bootSize >= Integer.MAX_VALUE - 1) {
throw new IllegalStateException("Mmapped file is too large for " + bootPath + " " + _bootSize);
}
_bootSize = (int) bootSize;
_bootChannel = (FileChannel) Files.newByteChannel(_bootPath, StandardOpenOption.READ);
_bootMap = _bootChannel.map(MapMode.READ_ONLY, 0, _bootSize);
readJar();
readManifest();
}