本文整理了Java中com.google.common.io.Files.map()
方法的一些代码示例,展示了Files.map()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Files.map()
方法的具体详情如下:
包路径:com.google.common.io.Files
类名称:Files
方法名:map
[英]Fully maps a file read-only in to memory as per FileChannel#map(java.nio.channels.FileChannel.MapMode,long,long).
Files are mapped from offset 0 to its length.
This only works for files ≤ Integer#MAX_VALUE bytes.
[中]按照FileChannel#map(java.nio.channels.FileChannel.MapMode,long,long)将只读文件完全映射到内存中。
文件从偏移量0映射到其长度。
这只适用于文件≤ 整数#最大值字节。
代码示例来源:origin: google/guava
/**
* Fully maps a file read-only in to memory as per {@link
* FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long)}.
*
* <p>Files are mapped from offset 0 to its length.
*
* <p>This only works for files ≤ {@link Integer#MAX_VALUE} bytes.
*
* @param file the file to map
* @return a read-only buffer reflecting {@code file}
* @throws FileNotFoundException if the {@code file} does not exist
* @throws IOException if an I/O error occurs
* @see FileChannel#map(MapMode, long, long)
* @since 2.0
*/
public static MappedByteBuffer map(File file) throws IOException {
checkNotNull(file);
return map(file, MapMode.READ_ONLY);
}
代码示例来源:origin: google/j2objc
/**
* Fully maps a file read-only in to memory as per {@link
* FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long)}.
*
* <p>Files are mapped from offset 0 to its length.
*
* <p>This only works for files ≤ {@link Integer#MAX_VALUE} bytes.
*
* @param file the file to map
* @return a read-only buffer reflecting {@code file}
* @throws FileNotFoundException if the {@code file} does not exist
* @throws IOException if an I/O error occurs
* @see FileChannel#map(MapMode, long, long)
* @since 2.0
*/
public static MappedByteBuffer map(File file) throws IOException {
checkNotNull(file);
return map(file, MapMode.READ_ONLY);
}
代码示例来源:origin: wildfly/wildfly
/**
* Fully maps a file read-only in to memory as per {@link
* FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long)}.
*
* <p>Files are mapped from offset 0 to its length.
*
* <p>This only works for files ≤ {@link Integer#MAX_VALUE} bytes.
*
* @param file the file to map
* @return a read-only buffer reflecting {@code file}
* @throws FileNotFoundException if the {@code file} does not exist
* @throws IOException if an I/O error occurs
* @see FileChannel#map(MapMode, long, long)
* @since 2.0
*/
public static MappedByteBuffer map(File file) throws IOException {
checkNotNull(file);
return map(file, MapMode.READ_ONLY);
}
代码示例来源:origin: google/j2objc
/**
* Fully 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 its length.
*
* <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}
* @return a buffer reflecting {@code file}
* @throws FileNotFoundException if the {@code file} does not exist
* @throws IOException if an I/O error occurs
* @see FileChannel#map(MapMode, long, long)
* @since 2.0
*/
public static MappedByteBuffer map(File file, MapMode mode) throws IOException {
checkNotNull(file);
checkNotNull(mode);
if (!file.exists()) {
throw new FileNotFoundException(file.toString());
}
return map(file, mode, file.length());
}
代码示例来源:origin: apache/incubator-druid
/**
* Fully maps a file read-only in to memory as per
* {@link FileChannel#map(FileChannel.MapMode, long, long)}.
*
* <p>Files are mapped from offset 0 to its length.
*
* <p>This only works for files <= {@link Integer#MAX_VALUE} bytes.
*
* <p>Similar to {@link Files#map(File)}, but returns {@link MappedByteBufferHandler}, that makes it easier to unmap
* the buffer within try-with-resources pattern:
* <pre>{@code
* try (MappedByteBufferHandler fileMappingHandler = FileUtils.map(file)) {
* ByteBuffer fileMapping = fileMappingHandler.get();
* // use mapped buffer
* }}</pre>
*
* @param file the file to map
*
* @return a {@link MappedByteBufferHandler}, wrapping a read-only buffer reflecting {@code file}
*
* @throws FileNotFoundException if the {@code file} does not exist
* @throws IOException if an I/O error occurs
* @see FileChannel#map(FileChannel.MapMode, long, long)
*/
public static MappedByteBufferHandler map(File file) throws IOException
{
MappedByteBuffer mappedByteBuffer = Files.map(file);
return new MappedByteBufferHandler(mappedByteBuffer);
}
代码示例来源:origin: wildfly/wildfly
/**
* Fully 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 its length.
*
* <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}
* @return a buffer reflecting {@code file}
* @throws FileNotFoundException if the {@code file} does not exist
* @throws IOException if an I/O error occurs
* @see FileChannel#map(MapMode, long, long)
* @since 2.0
*/
public static MappedByteBuffer map(File file, MapMode mode) throws IOException {
checkNotNull(file);
checkNotNull(mode);
if (!file.exists()) {
throw new FileNotFoundException(file.toString());
}
return map(file, mode, file.length());
}
代码示例来源:origin: google/j2objc
RandomAccessFile raf =
closer.register(new RandomAccessFile(file, mode == MapMode.READ_ONLY ? "r" : "rw"));
return map(raf, mode, size);
} catch (Throwable e) {
throw closer.rethrow(e);
代码示例来源:origin: apache/incubator-druid
/**
* Returns a mapped buffer of the smooshed file with the given name. Buffer's contents from 0 to capacity() are the
* whole mapped file contents, limit() is equal to capacity().
*/
public ByteBuffer mapFile(String name) throws IOException
{
final Metadata metadata = internalFiles.get(name);
if (metadata == null) {
return null;
}
final int fileNum = metadata.getFileNum();
while (buffersList.size() <= fileNum) {
buffersList.add(null);
}
MappedByteBuffer mappedBuffer = buffersList.get(fileNum);
if (mappedBuffer == null) {
mappedBuffer = Files.map(outFiles.get(fileNum));
buffersList.set(fileNum, mappedBuffer);
}
ByteBuffer retVal = mappedBuffer.duplicate();
retVal.position(metadata.getStartOffset()).limit(metadata.getEndOffset());
return retVal.slice();
}
代码示例来源:origin: wildfly/wildfly
RandomAccessFile raf =
closer.register(new RandomAccessFile(file, mode == MapMode.READ_ONLY ? "r" : "rw"));
return map(raf, mode, size);
} catch (Throwable e) {
throw closer.rethrow(e);
代码示例来源:origin: google/guava
public void testMap_readWrite_max_value_plus_1() throws IOException {
// Setup
File file = createTempFile();
// Test
try {
Files.map(file, MapMode.READ_WRITE, (long) Integer.MAX_VALUE + 1);
fail("Should throw when size exceeds Integer.MAX_VALUE");
} catch (IllegalArgumentException expected) {
}
}
代码示例来源:origin: google/guava
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));
}
代码示例来源:origin: google/guava
public void testMap_noSuchFile() throws IOException {
// Setup
File file = createTempFile();
boolean deleted = file.delete();
assertTrue(deleted);
// Test
try {
Files.map(file);
fail("Should have thrown FileNotFoundException.");
} catch (FileNotFoundException expected) {
}
}
代码示例来源:origin: google/guava
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));
}
代码示例来源:origin: apache/incubator-druid
@Test
public void testUnmapDoesntCrashJVM() throws Exception
{
final File file = temporaryFolder.newFile("some_mmap_file");
try (final OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
final byte[] data = new byte[4096];
Arrays.fill(data, (byte) 0x5A);
os.write(data);
}
final MappedByteBuffer mappedByteBuffer = Files.map(file);
Assert.assertEquals((byte) 0x5A, mappedByteBuffer.get(0));
ByteBufferUtils.unmap(mappedByteBuffer);
ByteBufferUtils.unmap(mappedByteBuffer);
}
代码示例来源:origin: google/guava
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));
}
代码示例来源:origin: bedatadriven/activityinfo
private void updateHash(MessageDigest digest, String extension) throws IOException {
File file = getFile(extension);
if (file.exists()) {
digest.update(Files.map(file, MapMode.READ_ONLY));
}
}
代码示例来源:origin: com.facebook.hive/hive-dwrf-shims
public static Slice mapFileReadOnly(File file)
throws IOException
{
return Slice.toUnsafeSlice(Files.map(file));
}
代码示例来源:origin: zhongl/iPage
private DirectBuffer mapFrom(File file) throws IOException {
ByteBuffer toClean = byteBuffer;
byteBuffer = Files.map(file, PRIVATE);
DirectByteBufferCleaner.clean(toClean);
return this;
}
代码示例来源:origin: org.anarres.tftp/tftp-protocol
@Override
public TftpByteBufferData load(File key) throws Exception {
MappedByteBuffer buffer = Files.map(key, FileChannel.MapMode.READ_ONLY);
return new TftpByteBufferData(buffer);
}
});
代码示例来源:origin: com.google.guava/guava-tests
public void testMap_noSuchFile() throws IOException {
// Setup
File file = createTempFile();
boolean deleted = file.delete();
assertTrue(deleted);
// Test
try {
Files.map(file);
fail("Should have thrown FileNotFoundException.");
} catch (FileNotFoundException expected) {
}
}
内容来源于网络,如有侵权,请联系作者删除!