IOUtils.toByteArray(inputStream)方法是否在内部关闭inputStream对象? [英] Does IOUtils.toByteArray(inputStream) method internally close inputStream object?

查看:460
本文介绍了IOUtils.toByteArray(inputStream)方法是否在内部关闭inputStream对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码流,丢失了文件内容,我认为可能是IOUtils.toByteArray()行有问题,请在这里指导实际出了什么问题.

Here is my code flow for which file content is getting lost and I think may be IOUtils.toByteArray() line is problem, please guide what is actually going wrong here.

文件内容丢失:

InputStream stream = someClient.downloadApi(fileId);
byte[] bytes = IOUtils.toByteArray(stream); 
String mimeType = CommonUtils.fileTypeFromByteArray(bytes);
String fileExtension=FormatToExtensionMapping.getByFormat(mimeType).getExtension();
String filePath = configuration.getDownloadFolder() + "/" ;
String fileName = UUID.randomUUID() + fileExtension;    
File file = new File(filePath+fileName);    
file.createNewFile();    
FileUtils.copyInputStreamToFile(stream,file);    
int length = (int)file.length();

现在的长度值是 0 ,基本上没有内容.让我告诉您,从downloadApi()接收到的inputStream具有确定的内容.但是,如果我尝试在代码中进行以下修改,那么我将得到文件的长度.

Now length value here is 0 basically no content. Let me tell you that inputStream received from downloadApi() has content for sure thats given. But if I try below modification in code then I'm getting length of file.

文件内容不会丢失:

InputStream stream = someClient.downloadApi(fileId);
byte[] bytes = IOUtils.toByteArray(stream);
String mimeType = CommonUtils.fileTypeFromByteArray(bytes);
String fileExtension=FormatToExtensionMapping.getByFormat(mimeType).getExtension();
String filePath = configuration.getDownloadFolder() + "/" ;
String fileName = UUID.randomUUID() + fileExtension;
stream = new ByteArrayInputStream(bytes); //Again converted bytes to stream    
File file = new File(filePath+fileName);    
file.createNewFile();    
FileUtils.copyInputStreamToFile(stream,file);    
int length = (int)file.length();

现在,我正在获取文件内容.某些机构可以在第一个代码段中分辨出技术上的错误吗?

Now here I'm getting file content. Can some body tell what is technically wrong here in first code snippet ?

TIA

推荐答案

不是.

代码的第一个版本(在下面复制并添加了一些注释)失败,因为您正在从已经位于流位置末尾的流中进行读取.

The first version of your code (reproduced below with some added commentary) fails because you are reading from a stream that is already at the end of stream position.

InputStream stream = someClient.downloadApi(fileId);

// This reads the entire stream to the end of stream.
byte[] bytes = IOUtils.toByteArray(stream);

String mimeType = CommonUtils.fileTypeFromByteArray(bytes);
String fileExtension = 
        FormatToExtensionMapping.getByFormat(mimeType).getExtension();
String filePath = configuration.getDownloadFolder() + "/" ;
String fileName = UUID.randomUUID() + fileExtension;    
File file = new File(filePath+fileName);    
file.createNewFile();    

// Now you attempt to read more data from the stream.
FileUtils.copyInputStreamToFile(stream,file);    

int length = (int)file.length();

当您尝试从流末尾的流中进行复制时,您将获得零字节.这意味着您将得到一个空的输出文件.

When you try to copy from a stream that is at the end of stream, you get ... zero bytes. And that means you get an empty output file.

这篇关于IOUtils.toByteArray(inputStream)方法是否在内部关闭inputStream对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆