java.nio.channels.FileChannel.MapMode源码实例Demo

类java.nio.channels.FileChannel.MapMode源码实例Demo

下面列出了java.nio.channels.FileChannel.MapMode 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: bt   文件: AnonAllocator.java
/**
 * 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);
}
 
源代码2 项目: DDMQ   文件: StoreCheckpoint.java
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);
    }
}
 
源代码3 项目: j2objc   文件: FileChannelTest.java
/**
 * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
 */
public void test_map_ReadWrite_NonZeroPosition() throws IOException {
    // test position non-zero
    writeDataToFile(fileOfReadWriteFileChannel);
    MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.READ_WRITE,
            10, CONTENT_LENGTH - 10);
    assertEquals(CONTENT_LENGTH - 10, mapped.limit());
    assertEquals(CONTENT.length() - 10, mapped.capacity());
    assertEquals(0, mapped.position());
    mapped.put(TEST_BYTES);
    ByteBuffer checkBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
    readWriteFileChannel.read(checkBuffer);
    String expected = CONTENT.substring(0, 10) + "test"
            + CONTENT.substring(10 + "test".length());
    assertEquals(expected, new String(checkBuffer.array(), "iso8859-1"));
}
 
源代码4 项目: fqueue   文件: FileRunner.java
private boolean create(String path) throws IOException {
    File file = new File(path);
    if (file.exists() == false) {
        if (file.createNewFile() == false) {
            return false;
        }
        RandomAccessFile raFile = new RandomAccessFile(file, "rwd");
        FileChannel fc = raFile.getChannel();
        MappedByteBuffer mappedByteBuffer = fc.map(MapMode.READ_WRITE, 0, this.fileLimitLength);
        mappedByteBuffer.put(LogEntity.MAGIC.getBytes());
        mappedByteBuffer.putInt(1);// 8 version
        mappedByteBuffer.putInt(-1);// 12next fileindex
        mappedByteBuffer.putInt(-2);// 16
        mappedByteBuffer.force();
        MappedByteBufferUtil.clean(mappedByteBuffer);
        fc.close();
        raFile.close();
        return true;
    } else {
        return false;
    }
}
 
源代码5 项目: offheap-store   文件: MappedPageSource.java
@Override
public synchronized MappedPage allocate(int size, boolean thief, boolean victim, OffHeapStorageArea owner) {
  Long address = allocateRegion(size);
  if (address == null) {
    return null;
  }

  try {
    MappedByteBuffer buffer = channel.map(MapMode.READ_WRITE, address, size);
    MappedPage page = new MappedPage(buffer);
    pages.put(page, address);
    return page;
  } catch (IOException e) {
    freeRegion(address);
    LOGGER.warn("Mapping a new file section failed", e);
    return null;
  }
}
 
源代码6 项目: j2objc   文件: FileChannelTest.java
/**
 * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
 */
public void test_map_ReadWrite() throws IOException {
    MappedByteBuffer mapped = null;
    writeDataToFile(fileOfReadWriteFileChannel);
    mapped = readWriteFileChannel.map(MapMode.READ_WRITE, 0, CONTENT
            .length());

    // put something will change its channel
    ByteBuffer returnByPut = mapped.put(TEST_BYTES);
    assertSame(returnByPut, mapped);
    String checkString = "test" + CONTENT.substring(4);
    ByteBuffer checkBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
    mapped.force();
    readWriteFileChannel.position(0);
    readWriteFileChannel.read(checkBuffer);
    assertEquals(checkString, new String(checkBuffer.array(), "iso8859-1"));

    try {
        mapped.put(("test" + CONTENT).getBytes("iso8859-1"));
        fail("should throw BufferOverflowException.");
    } catch (BufferOverflowException ex) {
        // expected;
    }
}
 
源代码7 项目: rocketmq   文件: StoreCheckpoint.java
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);
    }
}
 
源代码8 项目: hadoop   文件: MappableBlock.java
/**
 * 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;
}
 
源代码9 项目: proxyee-down   文件: X64HttpDownBootstrap.java
@Override
public Closeable[] initFileWriter(ChunkInfo chunkInfo) throws Exception {
  Closeable[] fileChannels;
  FileChannel fileChannel = new RandomAccessFile(
      getHttpDownInfo().getTaskInfo().buildTaskFilePath(), "rw")
      .getChannel();
  if (getHttpDownInfo().getTaskInfo().getConnections() > 1) {
    LargeMappedByteBuffer mappedBuffer = new LargeMappedByteBuffer(fileChannel,
        MapMode.READ_WRITE, chunkInfo.getNowStartPosition(),
        chunkInfo.getEndPosition() - chunkInfo.getNowStartPosition() + 1);
    fileChannels = new Closeable[]{fileChannel, mappedBuffer};
  } else {
    fileChannels = new Closeable[]{fileChannel};
  }
  setAttr(chunkInfo, ATTR_FILE_CHANNELS, fileChannels);
  return fileChannels;
}
 
源代码10 项目: DDMQ   文件: StoreCheckpoint.java
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);
    }
}
 
源代码11 项目: incubator-crail   文件: MappedBufferCache.java
public CrailBuffer allocateRegion() throws IOException {
	if (currentRegion >= allocationCount){
		return null;
	}

	String path = directory + "/" + currentRegion++;
	RandomAccessFile randomFile = new RandomAccessFile(path, "rw");
	randomFile.setLength(CrailConstants.REGION_SIZE);
	FileChannel channel = randomFile.getChannel();
	MappedByteBuffer _mappedBuffer = channel.map(MapMode.READ_WRITE, 0,
			CrailConstants.REGION_SIZE);
	CrailBuffer mappedBuffer = OffHeapBuffer.wrap(_mappedBuffer);
	randomFile.close();
	channel.close();

	CrailBuffer firstBuffer = slice(mappedBuffer, 0);

	for (int j = 1; j < bufferCount; j++) {
		int position = j * CrailConstants.BUFFER_SIZE;
		CrailBuffer sliceBuffer = slice(mappedBuffer, position);
		this.putBufferInternal(sliceBuffer);
	}
	mappedBuffer.clear();

	return firstBuffer;
}
 
源代码12 项目: alfresco-repository   文件: IndexInfo.java
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) buffer).position(0);
        }

        ((Buffer) buffer).position(0);
        long onDiskVersion = buffer.getLong();
        return (version == onDiskVersion);
    }
    return (version == 0);
}
 
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    CoyoteOutputStream os = (CoyoteOutputStream) resp.getOutputStream();
    File file = new File("test/org/apache/catalina/connector/test_content.txt");
    try (RandomAccessFile raf = new RandomAccessFile(file, "r");) {
        os.write(raf.getChannel().map(MapMode.READ_ONLY, 0, file.length()));
    }
}
 
源代码14 项目: joshua   文件: PackedGrammar.java
private ByteBuffer associateMemoryMappedFile(File file) throws IOException {
  try(FileInputStream fileInputStream = new FileInputStream(file)) {
    FileChannel fileChannel = fileInputStream.getChannel();
    int size = (int) fileChannel.size();
    return fileChannel.map(MapMode.READ_ONLY, 0, size);
  }
}
 
源代码15 项目: diozero   文件: SysFsPerfTest.java
private static void testMmapRead(int gpio, int iterations) throws IOException {
	try (FileChannel fc = FileChannel.open(Paths.get("/sys/class/gpio/gpio" + gpio + "/value"),
			StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.SYNC)) {
		MappedByteBuffer mbb = fc.map(MapMode.READ_WRITE, 0, 1);
		long start = System.currentTimeMillis();
		for (int i=0; i<iterations; i++) {
			mbb.get(0);
		}
		long duration = System.currentTimeMillis() - start;
		Logger.info("mmap read: {0.000} ms per iteration", Double.valueOf(duration*1000 / (double) iterations));
	}
}
 
源代码16 项目: antsdb   文件: AntiCrashCrimeScene.java
private void open(boolean write) throws IOException {
    if (this.raf != null) {
        return;
    }
    
    // rename the existing file so we dont accidently lose previous crash scene
    if (write && file.exists()) {
        String filename = file.getName();
        filename = System.currentTimeMillis() + "-" + filename;
        File backup = new File(file.getParentFile(), filename);
        file.renameTo(backup);
    }
    
    // open it
    this.raf = new RandomAccessFile(file, write ? "rw" : "r");
    this.ch = this.raf.getChannel();
    
    // map units if this is read only
    if (!write) {
        int nbuffers = (int)(this.file.length() / Unit.SIZE); 
        MappedByteBuffer mmf = raf.getChannel().map(MapMode.READ_ONLY, 0, Unit.SIZE * nbuffers);
        this.mmfs.add(mmf);
        mmf.order(ByteOrder.nativeOrder());
        for (int i=0; i<nbuffers; i++) {
            Unit ii = new Unit();
            mmf.position(i * Unit.SIZE);
            mmf.limit(mmf.position() + Unit.SIZE);
            ii.buf = mmf.slice();
            ii.addr = UberUtil.getAddress(ii.buf);
            this.units.add(ii);
        }
        this.ch.close();
        this.raf.close();
    }
}
 
源代码17 项目: j2objc   文件: FileChannelTest.java
/**
 * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
 */
public void test_map_ReadOnly() throws IOException {
    MappedByteBuffer mapped = null;
    // try put something to readonly map
    writeDataToFile(fileOfReadOnlyFileChannel);
    mapped = readOnlyFileChannel.map(MapMode.READ_ONLY, 0, CONTENT_LENGTH);
    try {
        mapped.put(TEST_BYTES);
        fail("should throw ReadOnlyBufferException.");
    } catch (ReadOnlyBufferException ex) {
        // expected;
    }
    assertEquals(CONTENT_LENGTH, mapped.limit());
    assertEquals(CONTENT_LENGTH, mapped.capacity());
    assertEquals(0, mapped.position());

    // try to get a readonly map from read/write channel
    writeDataToFile(fileOfReadWriteFileChannel);
    mapped = readWriteFileChannel.map(MapMode.READ_ONLY, 0, CONTENT
            .length());
    assertEquals(CONTENT_LENGTH, mapped.limit());
    assertEquals(CONTENT_LENGTH, mapped.capacity());
    assertEquals(0, mapped.position());

    // map not change channel's position
    assertEquals(0, readOnlyFileChannel.position());
    assertEquals(0, readWriteFileChannel.position());
}
 
源代码18 项目: flink   文件: FileChannelMemoryMappedBoundedData.java
private void mapRegionAndStartNext() throws IOException {
	final ByteBuffer region = fileChannel.map(MapMode.READ_ONLY, startOfCurrentRegion, pos - startOfCurrentRegion);
	region.order(ByteOrder.nativeOrder());
	memoryMappedRegions.add(region);

	startOfCurrentRegion = pos;
	endOfCurrentRegion = startOfCurrentRegion + maxRegionSize;
}
 
源代码19 项目: flink   文件: BufferReaderWriterUtilTest.java
@Test
public void writeFileReadMemoryBuffer() throws Exception {
	final FileChannel fc = tmpFileChannel();
	final Buffer buffer = createTestBuffer();
	BufferReaderWriterUtil.writeToByteChannel(fc, buffer, BufferReaderWriterUtil.allocatedWriteBufferArray());

	final ByteBuffer bb = fc.map(MapMode.READ_ONLY, 0, fc.position()).order(ByteOrder.nativeOrder());
	BufferReaderWriterUtil.configureByteBuffer(bb);
	fc.close();

	Buffer result = BufferReaderWriterUtil.sliceNextBuffer(bb);

	validateTestBuffer(result);
}
 
源代码20 项目: dragonwell8_jdk   文件: MapTest.java
/**
 * Tests zero size file mapping
 */
private static void testZero() throws Exception {
    try (FileInputStream fis = new FileInputStream(blah)) {
        FileChannel fc = fis.getChannel();
        MappedByteBuffer b = fc.map(MapMode.READ_ONLY, 0, 0);
    }
}
 
源代码21 项目: dragonwell8_jdk   文件: MapTest.java
/**
 * 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");
            }
        }
    }
}
 
源代码22 项目: hottub   文件: MapTest.java
/**
 * 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");
            }
        }
    }
}
 
源代码23 项目: dragonwell8_jdk   文件: MapTest.java
private static void testHighOffset() 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 = 66000;
            MappedByteBuffer b = fc.map(MapMode.READ_WRITE,
                                        offset, 100);
        }
    }
}
 
源代码24 项目: dragonwell8_jdk   文件: MapTest.java
private static void testExceptions(FileChannel fc) throws IOException {
    checkException(fc, null, 0L, fc.size(),
                   NullPointerException.class);

    checkException(fc, MapMode.READ_ONLY, -1L, fc.size(),
                   IllegalArgumentException.class);

    checkException(fc, null, -1L, fc.size(),
                   IllegalArgumentException.class, NullPointerException.class);

    checkException(fc, MapMode.READ_ONLY, 0L, -1L,
                   IllegalArgumentException.class);

    checkException(fc, null, 0L, -1L,
                   IllegalArgumentException.class, NullPointerException.class);

    checkException(fc, MapMode.READ_ONLY, 0L, Integer.MAX_VALUE + 1L,
                   IllegalArgumentException.class);

    checkException(fc, null, 0L, Integer.MAX_VALUE + 1L,
                   IllegalArgumentException.class, NullPointerException.class);

    checkException(fc, MapMode.READ_ONLY, Long.MAX_VALUE, 1L,
                   IllegalArgumentException.class);

    checkException(fc, null, Long.MAX_VALUE, 1L,
                   IllegalArgumentException.class, NullPointerException.class);

}
 
源代码25 项目: openjdk-8   文件: InterruptMapDeadlock.java
@Override
public void run() {
    try {
        gate.acquireUninterruptibly();
        fc.map(MapMode.READ_ONLY, 0, 1);
        throw new Exception("Map succeeded");
    } catch (IOException x) {
        System.out.println(x.getClass() + " (expected)");
    } catch (Exception unexpected) {
        this.exception = unexpected;
    }
}
 
源代码26 项目: TencentKona-8   文件: MapTest.java
/**
 * Tests zero size file mapping
 */
private static void testZero() throws Exception {
    try (FileInputStream fis = new FileInputStream(blah)) {
        FileChannel fc = fis.getChannel();
        MappedByteBuffer b = fc.map(MapMode.READ_ONLY, 0, 0);
    }
}
 
源代码27 项目: TencentKona-8   文件: MapTest.java
/**
 * 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");
            }
        }
    }
}
 
源代码28 项目: codebuff   文件: Files.java
private static MappedByteBuffer map(RandomAccessFile raf, MapMode mode, long size) throws IOException {
  Closer closer = Closer.create();
  try {
    FileChannel channel = closer.register(raf.getChannel());
    return channel.map(mode, 0, size);
  } catch (Throwable e) {
    throw closer.rethrow(e);
  } finally {
    closer.close();
  }
}
 
源代码29 项目: tutorials   文件: NumberOfLineFinder.java
public static int getTotalNumberOfLinesUsingNIOFileChannel(String fileName) {
    int lines = 1;
    try (FileChannel channel = FileChannel.open(Paths.get(fileName), StandardOpenOption.READ)) {
        ByteBuffer byteBuffer = channel.map(MapMode.READ_ONLY, 0, channel.size());
        while (byteBuffer.hasRemaining()) {
            byte currentChar = byteBuffer.get();
            if (currentChar == '\n') {
                lines++;
            }
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    return lines;
}
 
源代码30 项目: TencentKona-8   文件: MapTest.java
private static void testExceptions(FileChannel fc) throws IOException {
    checkException(fc, null, 0L, fc.size(),
                   NullPointerException.class);

    checkException(fc, MapMode.READ_ONLY, -1L, fc.size(),
                   IllegalArgumentException.class);

    checkException(fc, null, -1L, fc.size(),
                   IllegalArgumentException.class, NullPointerException.class);

    checkException(fc, MapMode.READ_ONLY, 0L, -1L,
                   IllegalArgumentException.class);

    checkException(fc, null, 0L, -1L,
                   IllegalArgumentException.class, NullPointerException.class);

    checkException(fc, MapMode.READ_ONLY, 0L, Integer.MAX_VALUE + 1L,
                   IllegalArgumentException.class);

    checkException(fc, null, 0L, Integer.MAX_VALUE + 1L,
                   IllegalArgumentException.class, NullPointerException.class);

    checkException(fc, MapMode.READ_ONLY, Long.MAX_VALUE, 1L,
                   IllegalArgumentException.class);

    checkException(fc, null, Long.MAX_VALUE, 1L,
                   IllegalArgumentException.class, NullPointerException.class);

}
 
源代码评论
动弹
沙发等你来抢
 类所在包
 类方法
 同包方法