Skip to content

Instantly share code, notes, and snippets.

@fitzyyf
Created March 28, 2013 03:16
Show Gist options
  • Save fitzyyf/5260276 to your computer and use it in GitHub Desktop.
Save fitzyyf/5260276 to your computer and use it in GitHub Desktop.
Google Guava Stream to String
// FSDataInputStream,支持随机访问
final FSDataInputStream in = fs.open(new Path(hdfs_file_uri));
try {
// 设置从第八行开始读取
InputSupplier<? extends InputStream> supplier = new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
return in;
}
};
final InputSupplier<InputStreamReader> readerSupplier = CharStreams.newReaderSupplier(supplier, Charsets.UTF_8);
String text = CharStreams.toString(readerSupplier);
System.out.println(text);
Assert.assertTrue(text.startsWith("nginx是一个高性能的Http和反向代理服务"));
} finally {
IOUtils.closeStream(in);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment