如何在最短的时间内在java中克隆输入流
有人可以告诉我如何克隆输入流,尽可能少的创建时间?我需要多次克隆输入流以便多种方法来处理IS.我尝试了三种方法,因为某种原因,事情不起作用.
方法#1: How to clone an InputStream? 但是,使用此代码最多可能需要一分钟(对于10MB文件)来创建克隆的输入流,并且我的程序需要尽可能快. int read = 0; byte[] bytes = new byte[1024*1024*2]; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while ((read = is.read(bytes)) != -1) bos.write(bytes,read); byte[] ba = bos.toByteArray(); InputStream is1 = new ByteArrayInputStream(ba); InputStream is2 = new ByteArrayInputStream(ba); InputStream is3 = new ByteArrayInputStream(ba); 方法#2: is = getFileFromBucket(path,filename); ... ... InputStream is1 = new BufferedInputStream(is); InputStream is2 = new BufferedInputStream(is); InputStream is3 = new BufferedInputStream(is); 方法#3: is1.mark() is1.reset() 要不就 is1.reset(); 在将IS传递给我各自的方法之前.在上面的两个例子中,我收到一个错误,说它是无效标记. 我现在没有想法,所以提前感谢你能给我的任何帮助. 附:从我收到的人们的评论中,我需要澄清一些与我的情况有关的事情: 解决方法
您可以创建某种自定义的ReusableInputStream类,其中您在第一次完整读取时立即写入内部 这是一个基本的启动示例: public class ReusableInputStream extends InputStream { private InputStream input; private ByteArrayOutputStream output; private ByteBuffer buffer; public ReusableInputStream(InputStream input) throws IOException { this.input = input; this.output = new ByteArrayOutputStream(input.available()); // Note: it's resizable anyway. } @Override public int read() throws IOException { byte[] b = new byte[1]; read(b,1); return b[0]; } @Override public int read(byte[] bytes) throws IOException { return read(bytes,bytes.length); } @Override public int read(byte[] bytes,int offset,int length) throws IOException { if (buffer == null) { int read = input.read(bytes,offset,length); if (read <= 0) { input.close(); input = null; buffer = ByteBuffer.wrap(output.toByteArray()); output = null; return -1; } else { output.write(bytes,read); return read; } } else { int read = Math.min(length,buffer.remaining()); if (read <= 0) { buffer.flip(); return -1; } else { buffer.get(bytes,read); return read; } } } // You might want to @Override flush(),close(),etc to delegate to input. } (请注意,实际作业是在int read(byte [],int,int)中执行而不是在int read()中执行,因此当调用者本身也使用byte []缓冲区进行流式处理时,预期会更快. 您可以按如下方式使用它: InputStream input = new ReusableInputStream(getFileFromBucket(path,filename)); IOUtils.copy(input,new FileOutputStream("/copy1.ext")); IOUtils.copy(input,new FileOutputStream("/copy2.ext")); IOUtils.copy(input,new FileOutputStream("/copy3.ext")); 至于性能,每10MB 1分钟更可能是硬件问题,而不是软件问题.我的7200rpm笔记本电脑硬盘在不到1秒的时间内完成. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |