com.google.common.io.Files.readFile()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(2.7k)|赞(0)|评价(0)|浏览(159)

本文整理了Java中com.google.common.io.Files.readFile()方法的一些代码示例,展示了Files.readFile()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Files.readFile()方法的具体详情如下:
包路径:com.google.common.io.Files
类名称:Files
方法名:readFile

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();
 }
}

相关文章