File.createTempFile(prefix, suffix),创建一个临时文件,再使用完之后清理即可。
但是遇到如下两个坑:

String prefix = "temp";
String suffix = ".txt";
File tempFile = File.createTempFile(prefix, suffix);

以上代码中,需要注意的两个地方:
1、prefix必须大于3个字符
2、suffix需要带上 . , 比如:.png、.zip

问题来源:

1.系统生成二维码,需要不同的图片格式来适应客户端要求

2.图片通过接口模式给客户端,最终使用base64来传递

平常思考模式:

1.BufferedImage首先通过工具把数据生成出来。

2.我绝对不会把这个BufferedImage写磁盘,直接放内存ByteArrayOutputstream后转base64岂不是更快?

3.ImageIO.write正好有个write(BufferedImage img,String format,OutputStream output)

4.真的舒服,我就用它了!

实际情况:

1.Linux环境centos6.8 虚拟化环境

2.JRE1.8

3.接口工作流程:
(1) 生成BufferedImage
(2) BufferedImage通过

ImageIO.write(BufferedImage,"png",ByteArrayOutputStream out) 

(3)将ByteArrayOutputStream转化为base64
(4) 接口返回

4.一个普通的链接,生成二维码并返回base64,接口耗时1.7S

5.png图片大小16K

分析问题&尝试更换接口:

1.一个图片生成16K,不大

2.一次请求1.7s,又是手机端应用,太慢了!不能接受

3.根据代码跟踪分析得出速度慢在 ImageIO.write这里

4.网上搜索信息也有相关的反馈说ImageIO.write png的时候奇慢无比,但是没有找到实际解决方法

5.尝试更换write的ByteArrayOutputStream为File,因为 ImageIO.write正好支持写文件ImageIO.write(BufferedImage,"png",File out)

6.测试结果:write到file后,接口响应时间在400ms!!!

查看源代码:

1.对比write到Byte和File的源代码发现,使用ByteArrayOutputStream的底层写数据的时候使用了FileCacheImageOutputStream,而使用File的底层写数据的时候使用了FileImageOutputStream。

2.查看FileCacheImageOutputStream的初始化方式、和写数据相关代码

//初始化代码
public FileCacheImageOutputStream(OutputStream stream, File cacheDir)
throws IOException {
if (stream == null) {
throw new IllegalArgumentException("stream == null!");
}
if ((cacheDir != null) && !(cacheDir.isDirectory())) {
throw new IllegalArgumentException("Not a directory!");
}
this.stream = stream;
//这里竟然创建了临时文件
if (cacheDir == null)
this.cacheFile = Files.createTempFile("imageio", ".tmp").toFile();
else
this.cacheFile = Files.createTempFile(cacheDir.toPath(), "imageio", ".tmp")
.toFile();
this.cache = new RandomAccessFile(cacheFile, "rw"); this.closeAction = StreamCloser.createCloseAction(this);
StreamCloser.addToQueue(closeAction);
} // 写数据,没什么特殊
public void write(int b) throws IOException {
flushBits(); // this will call checkClosed() for us
cache.write(b);
++streamPos;
maxStreamPos = Math.max(maxStreamPos, streamPos);
} //关闭
public void close() throws IOException {
maxStreamPos = cache.length(); seek(maxStreamPos);
//注意这里!!!!!
flushBefore(maxStreamPos);
super.close();
cache.close();
cache = null;
cacheFile.delete();
cacheFile = null;
stream.flush();
stream = null;
StreamCloser.removeFromQueue(closeAction);
} //把数据写入ByteArrayOutputStream
public void flushBefore(long pos) throws IOException {
long oFlushedPos = flushedPos;
super.flushBefore(pos); // this will call checkClosed() for us long flushBytes = flushedPos - oFlushedPos;
if (flushBytes > 0) {
// 这里使用了一个逻辑每次只读512个字节到stream里面!!然后循环
int bufLen = 512;
byte[] buf = new byte[bufLen];
cache.seek(oFlushedPos);
while (flushBytes > 0) {
int len = (int)Math.min(flushBytes, bufLen);
cache.readFully(buf, 0, len);
stream.write(buf, 0, len);
flushBytes -= len;
}
stream.flush();
}
}

3.而FileImageOutputStream 的相关代码如下,都很中规中矩没有什么特殊

//初始化
public FileImageOutputStream(File f)
throws FileNotFoundException, IOException {
this(f == null ? null : new RandomAccessFile(f, "rw"));
} //写数据
public void write(int b) throws IOException {
flushBits(); // this will call checkClosed() for us
raf.write(b);
++streamPos;
} //关闭
public void close() throws IOException {
super.close();
disposerRecord.dispose(); // this closes the RandomAccessFile
raf = null;
}

分析源代码:

1.使用了cache的方式对数据读取和写入做了优化,为了防止内存溢出他已512字节读取然后写入输出流。但是当写到ByteArrayOutputStream的时候反而显得笨拙,一个16k的图片走cache的方式需要反复读取32次。

2.使用了普通模式的读取写入数据中规中矩,而读取因为了解文件大小都在16k左右,我采用了一次性读取到内存,所以将File类型的读取到内存再转化base64的时候,只发生了1次磁盘IO

结论:

1. 我们不能被代码外表所欺骗,乍一眼觉得写内存肯定比写File要快。

2.FileCacheImageOutputStream的出发点是好的,分批次数据读取然后写输出流

3.ImageIO.write 下面这出代码针对ByteArrayOutputStream的策略选择有失误:

 while (iter.hasNext()) {
ImageOutputStreamSpi spi = (ImageOutputStreamSpi)iter.next();
if (spi.getOutputClass().isInstance(output)) {
try {
//针对ByteArrayOutputStream输出流选择 ImageOutputStream的实现不理想
return spi.createOutputStreamInstance(output,
usecache,
getCacheDirectory());
} catch (IOException e) {
throw new IIOException("Can't create cache file!", e);
}
}
}

磁盘缓存对ByteArray输出流没有效果,该溢出的还是会溢出,还不如直接不使用cache

4.最终我们采用了先写图片到磁盘文件,然后读取文件转base64再返回,接口稳定在了 400ms内

https://my.oschina.net/u/2461727/blog/3024892?from=timeline&isappinstalled=0

我的ImageIO.write ByteArrayOutputStream为什么这么慢?的更多相关文章

  1. 从BufferedImage到InputStream,实现绘图后进行下载(生成二维码图片并下载)

    @SuppressWarnings("resource") public void download() throws Exception{ String filename = & ...

  2. Java 二维码生成工具类

    /** * 二维码 工具 * * @author Rubekid * */ public class QRcodeUtils { /** * 默认version */ public static fi ...

  3. java 压缩图片(只缩小体积,不更改图片尺寸)

      1.情景展示 在调用腾讯身份证OCR接口的时候,由于要求图片大小只能限制在1MB以内,这样,就必须使用到图片压缩技术 2.代码展示 /** * 图片处理工具类 * @explain * @auth ...

  4. 在线图片压缩后以ImageIO 流的形式 设置大小显示指定页面

    1.Servlet   代码 public class ZoomImgServlet extends HttpServlet implements Servlet { public void init ...

  5. ImageIO(图像处理)

    1.通过ImageIO的read和writer,对图像文件进行处理. BufferedImage buffImage = ImageIO.read(file); // 将图像输出到Servlet输出流 ...

  6. Java使用imageio、awt生成图片验证码

    1.生成验证码工具类 public class CheckCodeTool { private Integer width = 80; private Integer height = 38; pub ...

  7. 用java imageio调整图片DPI,例如从96调整为300

    因项目需求把图片的DPI值提升到300,否则OCR识别产生错乱:直接上源码:1.图片处理接口: package util.image.dpi; import java.awt.image.Buffer ...

  8. Java 利用 ByteArrayOutputStream 和 ByteArrayInputStream 避免重复读取配置文件

    最近参与了github上的一个开源项目 Mycat,是一个mysql的分库分表的中间件.发现其中读取配置文件的代码,存在频繁多次重复打开,读取,关闭的问题,代码写的很初级,稍微看过一些框架源码的人,是 ...

  9. Java字节流:ByteArrayInputStream ByteArrayOutputStream

    ----------------------------------------------------------------------------------- ByteArrayInputSt ...

随机推荐

  1. SSM学习系列

    Spring+SpringMVC+MyBatis Spring+SpringMVC+MyBatis深入学习及搭建(一)——MyBatis的基础知识 Spring+SpringMVC+MyBatis深入 ...

  2. 21、Cursorを使う

    例: (詳しい内容が後で追加) declare @tempTB table ( PEファンドコード ) ) --1.データ格納用の変数を声明 ) --2.Cursorを声明.内容を定義 declare ...

  3. C# vb .net实现圆角矩形特效滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的圆角矩形效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第 ...

  4. C# vb .net实现焦距灰度特效滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的焦距灰度效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第 ...

  5. 五 查询数据SELECT   一、单表查询

    一 单表查询的语法 二 关键字的执行优先级 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER BY 八 限制查询的记录数:LIMIT 九 ...

  6. Django:表多对多查询、聚合分组、FQ查询、事务

    1表多对多的关系查询 准备工作创建表结构 from django.db import models # Create your models here. class Publisher(models. ...

  7. 英语caement水泥

    水泥石  又称净浆硬化体.是指 硬化后的水泥浆体,称为水泥石,在英语里是cement有时写作caement [1] ,是由胶凝体.未水化的水泥颗粒内核.毛细孔等组 成的非均质体. 中文名:水泥石 外 ...

  8. 软工作业 wc-java

    项目要求: 实现一个统计程序,它能正确统计程序文件中的字符数.单词数.行数,以及还具备其他扩展功能,并能够快速地处理多个文件. 具体功能 -c 返回文件字符数 -w 返回词的数目 -l 返回行数 扩展 ...

  9. MySQL NULL--三值逻辑(Three Value Logic)

    三值逻辑(Three Value Logic) 在关系型数据库中,由于NULL值的存在,导致逻辑表达式存在三种值:TRUE/FALSE/UNKNOW. SELECT '=NULL AS C1, ' A ...

  10. python(练习题)

    1.请写出冒泡排序将list1进行排序? List1 = [1,2,34,12,33,25,12,33,90,28] 2.使用python语言打印出name=”I love python”的长度 3. ...