需求:
程序仅实现对InputStream的中转,传入InputStream,传出依然是InputStream。
思路:
一般情况下,InputStream负责读,OutputStream负责写。为了不在本地产生任何数据文件,只能使用两个InputStream对接,从A读出后,再读到B里去,同时为了程序的高效,不能一次性将A的流数据全部读出,否则容易导致内存溢出,因此,需要分段读取。
实现:
import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class Splitter { private InputStream inputStream; private long size; private int realBlocksize=1024*1024; //1M private long curLocation = 0; private byte[] buffer; /** * * @param size 数据流总大小 * @param inputStream 数据流对象 * @param blocksize 分块大小 * @param mincount 最小拆分数 */ public Splitter(long size, InputStream inputStream, int blocksize,int mincount) { this.size = size; this.inputStream = inputStream; if (size < mincount*blocksize) { realBlocksize = (int) (size / mincount); } if (realBlocksize == 0) { realBlocksize = (int) blocksize; } buffer = new byte[realBlocksize]; } public byte[] getBuffer() { return buffer; } public int splite() { int re = 0; if (curLocation >= size) { return re; } try { re = inputStream.read(buffer); curLocation += re; } catch (IOException e) { e.printStackTrace(); } return re; } public static void main(String[] args) throws Exception { File f = new File("F:\\test.zip"); FileInputStream in = new FileInputStream(f); Splitter splitter = new Splitter(f.length(), in, 1024*1024,2); int blockIndex=0; while (true){ int readSize = splitter.splite(); if (readSize == 0) { break; } byte[] readBuffer = splitter.getBuffer(); InputStream readinput=new ByteArrayInputStream(readBuffer,0,readSize); } } }
相关推荐
关于InputStream和OutputStream关于InputStream和OutputStream关于InputStream和OutputStream关于InputStream和OutputStream
springboot 解决InputStream只能读取一次的问题
Java中InputStream类.pdf 学习资料 复习资料 教学资源
InputStream与OutputStream及File间互转
NULL 博文链接:https://cuisuqiang.iteye.com/blog/1434416
读取网络字节流 php stream 二进制
Blob、InputStream、byte 互转
深入剖析tomcat第三章SocketInputStream源码 * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/StringManager.java,v 1.2 2001/07/22 20:25:14 pier Exp $ * $Revision: 1.2...
Java SE程序 文件的拷贝InputStream类Java SE程序 文件的拷贝InputStream类Java SE程序 文件的拷贝InputStream类Java SE程序 文件的拷贝InputStream类Java SE程序 文件的拷贝InputStream类Java SE程序 文件的拷贝...
inputstream读取数据的问题,对你的file的帮组是很大的。
原理讲解-ServletInputStream.readLine(byte[] b, int off, int len) 方法原理讲解-ServletInputStream.readLine(byte[] b, int off, int len) 方法
主要介绍了Java实现inputstream流的复制代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
NULL 博文链接:https://shihuan830619.iteye.com/blog/2094068
InputStream的用法2 --- 马克-to-win java视频 输入流
InputStream的用法1 ---马克-to-win java视频 输入流
NULL 博文链接:https://superherosk123.iteye.com/blog/611933
通过jsoup爬取网上图片,其中包括输入流InputStream的用法
IO流文档,相关IO流的说明文档.............
java.io.Reader 和 java.io.InputStream 的区别
android-volley-inputstream-as-response 代码不是我的! 在这个问题中对 StackOverlow 所有功劳: : 从 Volley 作为 InputStream 获得响应非常困难。 有些人需要它来进行消息包编码,我需要它来获取作为 ...