本文整理了Java中com.google.common.io.Files.readFile()
方法的一些代码示例,展示了Files.readFile()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Files.readFile()
方法的具体详情如下:
包路径:com.google.common.io.Files
类名称:Files
方法名:readFile
[英]Reads a file of the given expected size from the given input stream, if it will fit into a byte array. This method handles the case where the file size changes between when the size is read and when the contents are read from the stream.
[中]
代码示例来源:origin: Nextdoor/bender
@Override
public byte[] read() throws IOException {
Closer closer = Closer.create();
try {
FileInputStream in = closer.register(openStream());
return readFile(in, in.getChannel().size());
} catch (Throwable e) {
throw closer.rethrow(e);
} finally {
closer.close();
}
}
代码示例来源:origin: com.google.guava/guava-jdk5
@Override
public byte[] read() throws IOException {
Closer closer = Closer.create();
try {
FileInputStream in = closer.register(openStream());
return readFile(in, in.getChannel().size());
} catch (Throwable e) {
throw closer.rethrow(e);
} finally {
closer.close();
}
}
代码示例来源:origin: com.google.guava/guava-tests
public void testReadFile_withCorrectSize() throws IOException {
File asciiFile = getTestFile("ascii.txt");
Closer closer = Closer.create();
try {
InputStream in = closer.register(new FileInputStream(asciiFile));
byte[] bytes = Files.readFile(in, asciiFile.length());
assertTrue(Arrays.equals(ASCII.getBytes(Charsets.US_ASCII), bytes));
} catch (Throwable e) {
throw closer.rethrow(e);
} finally {
closer.close();
}
}
代码示例来源:origin: com.google.guava/guava-tests
public void testReadFile_withSmallerSize() throws IOException {
File asciiFile = getTestFile("ascii.txt");
Closer closer = Closer.create();
try {
InputStream in = closer.register(new FileInputStream(asciiFile));
byte[] bytes = Files.readFile(in, 10);
assertTrue(Arrays.equals(ASCII.getBytes(Charsets.US_ASCII), bytes));
} catch (Throwable e) {
throw closer.rethrow(e);
} finally {
closer.close();
}
}
代码示例来源:origin: com.google.guava/guava-tests
public void testReadFile_withSizeZero() throws IOException {
File asciiFile = getTestFile("ascii.txt");
Closer closer = Closer.create();
try {
InputStream in = closer.register(new FileInputStream(asciiFile));
byte[] bytes = Files.readFile(in, 0);
assertTrue(Arrays.equals(ASCII.getBytes(Charsets.US_ASCII), bytes));
} catch (Throwable e) {
throw closer.rethrow(e);
} finally {
closer.close();
}
}
代码示例来源:origin: com.google.guava/guava-tests
public void testReadFile_withLargerSize() throws IOException {
File asciiFile = getTestFile("ascii.txt");
Closer closer = Closer.create();
try {
InputStream in = closer.register(new FileInputStream(asciiFile));
byte[] bytes = Files.readFile(in, 500);
assertTrue(Arrays.equals(ASCII.getBytes(Charsets.US_ASCII), bytes));
} catch (Throwable e) {
throw closer.rethrow(e);
} finally {
closer.close();
}
}
内容来源于网络,如有侵权,请联系作者删除!