当前位置: 首页>>代码示例>>Java>>正文


Java MapMode.READ_ONLY属性代码示例

本文整理汇总了Java中java.nio.channels.FileChannel.MapMode.READ_ONLY属性的典型用法代码示例。如果您正苦于以下问题:Java MapMode.READ_ONLY属性的具体用法?Java MapMode.READ_ONLY怎么用?Java MapMode.READ_ONLY使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在java.nio.channels.FileChannel.MapMode的用法示例。


在下文中一共展示了MapMode.READ_ONLY属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: MemoryMappedFile

public MemoryMappedFile(File file, String mode, long offset, long size) throws IOException {
    if (size >= Integer.MAX_VALUE) {
        throw new IllegalArgumentException("jvm doesn't support mapped file more than 2g");
    }
    MapMode mapmode = null;
    if (mode.equals("r")) {
        mapmode = MapMode.READ_ONLY;
    }
    else if (mode.equals("rw")) {
        mapmode = MapMode.READ_WRITE;
    }
    else {
        throw new IllegalArgumentException();
    }
    
    if ((mapmode == MapMode.READ_WRITE) && !file.exists()) {
        File parent = file.getAbsoluteFile().getParentFile();
        long free = parent.getUsableSpace();
        if ((size * 4) > free) {
            throw new HumpbackException("out of storage space: " + file.toString() + ' ' + free);
        }
    }
    
    this.file = file;
    boolean exist = this.file.exists();
    this.raf = new RandomAccessFile(file, mode);
    this.channel = raf.getChannel();
    this.buf = channel.map(mapmode, offset, size);
    this.buf.order(ByteOrder.nativeOrder());
    this.addr = UberUtil.getAddress(buf);
    _log.debug(String.format("mounted %s %s %s at 0x%016x with length 0x%08x",
            exist ? "exist" : "new",
            file.toString(), 
            mode, 
            addr, 
            size));
}
 
开发者ID:waterguo,项目名称:antsdb,代码行数:37,代码来源:MemoryMappedFile.java

示例2: map

/**
 * Note: copied from Google Guava under Apache License v2.
 * 
 * Maps a file in to memory as per
 * {@link FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long)}
 * using the requested {@link MapMode}.
 *
 * <p>Files are mapped from offset 0 to {@code size}.
 *
 * <p>If the mode is {@link MapMode#READ_WRITE} and the file does not exist,
 * it will be created with the requested {@code size}. Thus this method is
 * useful for creating memory mapped files which do not yet exist.
 *
 * <p>This only works for files <= {@link Integer#MAX_VALUE} bytes.
 *
 * @param file the file to map
 * @param mode the mode to use when mapping {@code file}
 * @param offset
 * @param len
 * @return a buffer reflecting {@code file}
 *
 * @see FileChannel#map(MapMode, long, long)
 * @since 2.0
 */
public static MappedByteBuffer map(File file, MapMode mode, long offset, long len) {
    N.requireNonNull(file);
    N.requireNonNull(mode);

    RandomAccessFile raf = null;

    try {
        raf = new RandomAccessFile(file, mode == MapMode.READ_ONLY ? "r" : "rw");
        return raf.getChannel().map(mode, offset, len);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        IOUtil.closeQuietly(raf);
    }
}
 
开发者ID:landawn,项目名称:AbacusUtil,代码行数:39,代码来源:IOUtil.java


注:本文中的java.nio.channels.FileChannel.MapMode.READ_ONLY属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。