日期:2014-05-20 浏览次数:20817 次
package com.core.common.file.stream; import java.io.IOException; import java.io.OutputStream; import java.util.List; import com.core.common.log.Log; /** * 文件名称: MultiOutputStream.java * 类名: MultiOutputStream * 日期: 2012-12-22 * * 功能描述:多目的源输出流,该对象同时将一个输入流写入多个输出流 */ public class MultiOutputStream extends OutputStream { /** 多目的源输出流的保存对象*/ private List<OutputStream> outputStreamList = null; /** * 构造方法 * 功能描述:初始化多目的源输出流 * * @param outputStreamList * @throws IOException */ public MultiOutputStream(final List<OutputStream> outputStreamList) throws IOException { if(outputStreamList == null || outputStreamList.isEmpty()) { final String msg = "多源输出流对象为null,不能进行初始化操作,请检查!"; Log.logDebug(msg); throw new IOException(msg); } int outputStreamListSize = outputStreamList.size(); for (int i = 0; i < outputStreamListSize; i++) { OutputStream outputStream = outputStreamList.get(i); if(outputStream == null) { outputStreamList.remove(i); Log.logDebug("多源输出流对象中发现第[{}]个是null,该输出源已从输出列表中删除,请检查!", i); } } this.outputStreamList = outputStreamList; } /** * 重写write方法 * 重载方法(non-Javadoc) * @see java.io.OutputStream#write(int) */ @Override public void write(final int b) throws IOException { if(this.outputStreamList != null) { for (OutputStream outputStream : this.outputStreamList) { outputStream.write(b); } } } /** * 重写write方法 * 重载方法(non-Javadoc) * @see java.io.OutputStream#write(byte[], int, int) */ @Override public void write(final byte[] data, final int offset, final int length) throws IOException { if(this.outputStreamList != null) { for (OutputStream outputStream : this.outputStreamList) { outputStream.write(data, offset, length); } } } /** * 重写flush方法 * 重载方法(non-Javadoc) * @see java.io.OutputStream#flush() */ @Override public void flush() throws IOException { if(this.outputStreamList != null) { for (OutputStream outputStream : this.outputStreamList) { outputStream.flush(); } } } /** * 重写close方法 * 重载方法(non-Javadoc) * @see java.io.OutputStream#close() */ @Override public void close() throws IOException { if(this.outputStreamList != null) { for (OutputStream outputStream : this.outputStreamList) { outputStream.close(); } } } }
/** * 方法名称: copyToMultiOutput * 功能描述: 将输入流拷贝至多输出源 * * @version 1.0 * * @param inputStream InputStream 输入流 * @param outputStream OutputStream 多目的源输出流 * @throws IOException * * @update [日期YYYY-MM-DD] [更改人姓名] [变更描述] */ public void copyToMultiOutput(final InputStream inputStream, final List<OutputStream> outputStreamList) throws IOException { MultiOutputStream multiOut = new MultiOutputStream(outputStreamList); copy(inputStream, multiOut); } /** * 方法名称: copy * 功能描述: 该方法实现输出源输出 * * @version 1.0 * * @param inputStream InputStream 输入流 * @param outputStream OutputStream 多目的源输出流 * @throws IOException * * @update [日期YYYY-MM-DD] [更改人姓名] [变更描述] */ private static void copy(final InputStream inputStream, final OutputStream outputStream) throws IOException { if(inputStream == null) { throw new IOException("没有输入流,拷贝操作停止!"); } if(outputStream == null) { throw new IOException("没有输出源,拷贝操作停止!"); } // 缓冲流 BufferedInputStream bin = new BufferedInputStream(inputStream); BufferedOutputStream bout = new BufferedOutputStream(outputStream); while (true) { int datum = bin.read(); if(datum == -1) { break; } bout.write(datum); } // 刷新缓冲区 bout.flush(); bout.close(); return; }