Guava Files 源码分析(一)
Files中的工厂
Files类中对InputStream, OutputStream以及Reader,Writer的操作封装了抽象工厂模式,抽象工厂是InputSupplier与OutputSupplier,具体工厂是Files中的newInputStreamSupplier(), newOutputStreamSupplier()等方法
而InputStream, OutputStream以及Reader,Writer则是抽象产品, 他们的各种实现和装饰器包装则为具体产品
Input与Output工厂
Files中将Input与Output(包括InputStream,OutputStream和Reader,Writer两大IO派系)的生成使用抽象工厂来实现,其中代表抽象工厂的两个类为
public interface InputSupplier<T> {
T getInput() throws IOException;
}
public interface InputSupplier<T> {
T getInput() throws IOException;
}
在某些类的对Input和Output操作的工具方法则会使用这个Supplier作为形参,例如
CharStreams.newReaderSupplier(
com.google.common.io.InputSupplier<? extends java.io.InputStream> in,
java.nio.charset.Charset charset
)
这样调用CharStreams.newReaderSupplier()便可以通过传入不同的Supplier具体实现来达到多态和解耦
Files类中的具体工厂实现
Files中工厂的具体实现抽象为了一个方法,如下:
public static InputSupplier<FileInputStream> newInputStreamSupplier(
final File file) {
Preconditions.checkNotNull(file);
return new InputSupplier<FileInputStream>() {
@Override
public FileInputStream getInput() throws IOException {
return new FileInputStream(file);
}
};
} public static OutputSupplier<FileOutputStream> newOutputStreamSupplier(
File file) {
return newOutputStreamSupplier(file, false);
} public static OutputSupplier<FileOutputStream> newOutputStreamSupplier(
final File file, final boolean append) {
Preconditions.checkNotNull(file);
return new OutputSupplier<FileOutputStream>() {
@Override
public FileOutputStream getOutput() throws IOException {
return new FileOutputStream(file, append);
}
};
} public static InputSupplier<InputStreamReader> newReaderSupplier(File file,
Charset charset) {
return CharStreams.newReaderSupplier(newInputStreamSupplier(file), charset);
} public static OutputSupplier<OutputStreamWriter> newWriterSupplier(File file,
Charset charset) {
return newWriterSupplier(file, charset, false);
} public static OutputSupplier<OutputStreamWriter> newWriterSupplier(File file,
Charset charset) {
return newWriterSupplier(file, charset, false);
}
这些工厂返回的产品大致包括了FileInputStream, FileOutputStream, Reader, Writer
文件读取与写入函数
/**
* 这里有意思的有些特殊文件是file.length() == 0,但是文件却有实际内容
* 所以不能直接开辟一个file.length()大小的byte[] buff来读取文件内容
* Guava的解决方法是通过一个buff[0x1000]大小的buff来逐步读取这个特殊的文件
* 将其写入到一个OutputStream以后再一次性将它out.toByteArray()并返回
*
* 而对于file.length() != 0 的情况,则是直接开辟一个byte[] buff[file.length()]大小的buff
* 一次性将file里的内容读取到buff中并返回,从而避免额外像读取上面说的特殊文件那样频繁的开辟小的byte[] buff
*/
public static byte[] toByteArray(File file) throws IOException {
Preconditions.checkArgument(file.length() <= Integer.MAX_VALUE);
if (file.length() == 0) {
// Some special files are length 0 but have content nonetheless.
return ByteStreams.toByteArray(newInputStreamSupplier(file));
} else {
// Avoid an extra allocation and copy.
byte[] b = new byte[(int) file.length()];
boolean threw = true;
InputStream in = new FileInputStream(file);
try {
ByteStreams.readFully(in, b);
threw = false;
} finally {
Closeables.close(in, threw);
}
return b;
}
} /**
* 通过toByteArray()方法将文件内容包装成字符串
*/
public static String toString(File file, Charset charset) throws IOException {
return new String(toByteArray(file), charset.name());
} /**
* 使用小buff byte[0x1000] 的方法来copy文件
* 因为直接使用InputStream不像File.length()那样可以直接获得长度
*/
public static void copy(InputSupplier<? extends InputStream> from, File to)
throws IOException {
ByteStreams.copy(from, newOutputStreamSupplier(to));
}
另外还有很多关于文件读取和写入函数的重载方法,实现方式大同小异,只是参数变化了一下
文件比较方法
/**
* 依旧是那个问题,有些特殊文件显示的文件长度为0,所以必须通过读取文件的byte内容去比较是否相等
*/
public static boolean equal(File file1, File file2) throws IOException {
if (file1 == file2 || file1.equals(file2)) {
return true;
} /*
* Some operating systems may return zero as the length for files
* denoting system-dependent entities such as devices or pipes, in
* which case we must fall back on comparing the bytes directly.
*/
long len1 = file1.length();
long len2 = file2.length();
if (len1 != 0 && len2 != 0 && len1 != len2) {
return false;
}
return ByteStreams.equal(newInputStreamSupplier(file1),
newInputStreamSupplier(file2));
}
临时目录的创建
/**
* Atomically creates a new directory somewhere beneath the system's
* temporary directory (as defined by the {@code java.io.tmpdir} system
* property), and returns its name.
*
* <p>Use this method instead of {@link File#createTempFile(String, String)}
* when you wish to create a directory, not a regular file. A common pitfall
* is to call {@code createTempFile}, delete the file and create a
* directory in its place, but this leads a race condition which can be
* exploited to create security vulnerabilities, especially when executable
* files are to be written into the directory.
*
* <p>This method assumes that the temporary volume is writable, has free
* inodes and free blocks, and that it will not be called thousands of times
* per second.
*
* @return the newly-created directory
* @throws IllegalStateException if the directory could not be created
*/
public static File createTempDir() {
File baseDir = new File(System.getProperty("java.io.tmpdir"));
String baseName = System.currentTimeMillis() + "-"; for (int counter = 0; counter < TEMP_DIR_ATTEMPTS; counter++) {
File tempDir = new File(baseDir, baseName + counter);
if (tempDir.mkdir()) {
return tempDir;
}
}
throw new IllegalStateException("Failed to create directory within "
+ TEMP_DIR_ATTEMPTS + " attempts (tried "
+ baseName + "0 to " + baseName + (TEMP_DIR_ATTEMPTS - 1) + ')');
}
Guava Doc中说到,如果直接使用File.createTempFile()会有安全问题,先介绍一下createTempFile()
public static java.io.File createTempFile(java.lang.String prefix,
java.lang.String suffix,
java.io.File directory)
throws java.io.IOException
它的功能是创建临时文件,提供prefix和suffix以及创建的dir,文件名会使用prefix+random+suffix的形式构成,而中间的random随机数则是使LazyInitialization.random.nextLong()生成,最后使用递归的方式生成这个临时文件
createTempFile()方法的返回值是生成文件以后的抽象路径File对象
根据doc的提示,应该使用 deleteOnExit()方法来删除临时文件,deleteOnExit()的意思是在JVM停止时才删除这个文件.相当于缓存了删除命令,如果有多个文件有deleteOnExit(),他们在JVM停止时会逆序开始删除(最后调用deleteOnExit()方法的文件最先删除,类似于栈)
所谓安全问题, StackOverflow上有人提到了,加入使用createTempFile()来创建临时目录,会这么写
public static File createTempDirectory()
throws IOException
{
final File temp; temp = File.createTempFile("temp", Long.toString(System.nanoTime())); if(!(temp.delete()))
{
throw new IOException("Could not delete temp file: " + temp.getAbsolutePath());
} if(!(temp.mkdir()))
{
throw new IOException("Could not create temp directory: " + temp.getAbsolutePath());
} return (temp);
}
然后参见几位网友的讨论

按照Sarel Botha的说法,使用了 file.mkdir() 的返回值做判断后抛出异常是不会有问题的
而如果没有使用file.mkdir()的返回值做判断, 潜在的问题是在于Linux的tmp目录使用了sticky bit(只能删除用户自己创建的文件), 在多线程情况下会存在file.delete()和file.mkdir()操作的线程竞争问题.具体是什么我也没搞清楚
原帖地址: http://stackoverflow.com/questions/617414/create-a-temporary-directory-in-java
随意总而言之, 就是使用Guava提供的createTmpDir()更安全
未完待续 ...
Guava Files 源码分析(一)的更多相关文章
- Guava Files 源码分析(二)
createTempDir()之后就没有什么有意思的函数了,基本上都是对Java IO函数操作的聚合,只看一个simplifyPath() /** * Returns the lexically cl ...
- Guava 源码分析(Cache 原理 对象引用、事件回调)
前言 在上文「Guava 源码分析(Cache 原理)」中分析了 Guava Cache 的相关原理. 文末提到了回收机制.移除时间通知等内容,许多朋友也挺感兴趣,这次就这两个内容再来分析分析. 在开 ...
- Guava cacha 机制及源码分析
1.ehcahce 什么时候用比较好:2.问题:当有个消息的key不在guava里面的话,如果大量的消息过来,会同时请求数据库吗?还是只有一个请求数据库,其他的等待第一个把数据从DB加载到Guava中 ...
- Guava 源码分析之Cache的实现原理
Guava 源码分析之Cache的实现原理 前言 Google 出的 Guava 是 Java 核心增强的库,应用非常广泛. 我平时用的也挺频繁,这次就借助日常使用的 Cache 组件来看看 Goog ...
- 通过 spark.files 传入spark任务依赖的文件源码分析
版本:spak2.3 相关源码:org.apache.spark.SparkContext 在创建spark任务时候,往往会指定一些依赖文件,通常我们可以在spark-submit脚本使用--file ...
- guava eventbus 原理+源码分析
前言: guava提供的eventbus可以很方便的处理一对多的事件问题, 最近正好使用到了,做个小结,使用的demo网上已经很多了,不再赘述,本文主要是源码分析+使用注意点+新老版本eventbus ...
- Java源码分析:Guava之不可变集合ImmutableMap的源码分析
一.案例场景 遇到过这样的场景,在定义一个static修饰的Map时,使用了大量的put()方法赋值,就类似这样-- public static final Map<String,String& ...
- [Guava源码分析]ImmutableCollection:不可变集合
摘要: 我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3888557.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的 ...
- [Guava源码分析]Ordering:排序
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3876466.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
随机推荐
- 【LOJ】#2562. 「SDOI2018」战略游戏
题解 圆方树建好之后点是原来的两倍,而st表求lca也要开到点的两倍,所以是四倍 我并没有开小,然而= =,我的预处理log2,写成了200000,而不是400000 我是不是折翼啊= = 很可写,我 ...
- 黑马程序员_java基础笔记(11)...反射
—————————— ASP.Net+Android+IOS开发..Net培训.期待与您交流! —————————— 1,字节码.2,Constructor类.3,Field类.4,Method类.5 ...
- Mysql安装(绿色版安装)
一:下载 1.官网 https://dev.mysql.com/ 2.下载 3.下载 二:安装 1.将官网上下载的包进行解压 2.以管理员的身份运行DOS安装 进入mysql的bin目录 运行mysq ...
- 在Ubuntu18.04中QT编程的环境构建(转)
在Ubuntu18.04中QT编程的环境构建 原点分析 百家号06-2110:14 如果说QT大家觉得陌生的话,那么 Windows 早年推出的C++图形用户界面的应用程序开发框架MFC,应该是耳熟能 ...
- TI科学家谈浮点DSP未来发展
自十多年前浮点数字信号处理器(DSP)诞生以来,便为实时信号处理提供了算术上更为先进的备选方案.不过,定点器件至今仍是业界的主流.当然低成本是主要原因.定点DSP每器件产品的价格很低,这对大规 ...
- python3之Django内置模板标签和过滤器
一.模板标签 内置标签: 1.autoescape 控制当前的自动转义行为,此标记采用on或者off作为参数,并确定自动转义是否在块内有效.该块以endautoescape结束标签关闭. views: ...
- oneDay
难受过 迷茫过 失望过 耍脾气过 开心过 伤心过 疼过 走了这么久的路: 我只想说 程序的道路上 很难走: 本来准备都放弃了: 自己逼自己了很久想明白了: 不能什么时候都想着靠外力 自己的不足就是自己 ...
- [leetcode tree]102. Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- BZOJ 4726 POI 2017 Sabota? 树形DP
4726: [POI2017]Sabota? Time Limit: 20 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 128 Solved ...
- CentOS7.0安装Nginx-1.12.0
一.安装准备 首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库,这些依赖库主要有g++.gcc.openssl-devel.pcre-devel和zlib ...