TeeInputStream

本文介绍了TeeInputStream,一个自定义的InputStream类,用于在读取文件流的同时备份资源,避免了ApacheCommonsTeeInputStream可能存在的数据丢失问题,通过示例展示了其在ApachePOIXSSFWorkbook处理Excel文件时的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用TeeInputStream复用InputStream的读实现处理流的同时实现资源备份,区别于apachecommons-ioorg.apache.commons.io.input.TeeInputStream会存在如果处理资源未完成文件读取操作时导致剩余部分内容丢失的情况。

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;

public class TeeInputStream extends InputStream {

    private final InputStream inputStream;
    private final OutputStream outputStream;
    private byte[] bytes;

    public TeeInputStream(InputStream inputStream, OutputStream outputStream) {
        this.inputStream = inputStream;
        this.outputStream = outputStream;
    }

    @Override
    public long skip(long n) throws IOException {
        if (n > 0 && bytes == null) {
            bytes = new byte[1024];
        }
        long r = 0;
        while (n > 0) {
            int l = inputStream.read(bytes, 0, (int) (bytes.length > n ? n : bytes.length));
            if (l > 0) {
                outputStream.write(bytes, 0, l);
            } else {
                break;
            }
            r += l;
            n -= l;
        }
        return r;
    }

    @Override
    public int read(byte[] b) throws IOException {
        int size = inputStream.read(b);
        if (size > 0) {
            outputStream.write(b, 0, size);
        }
        return size;
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        int size = inputStream.read(b, off, len);
        if (size > 0) {
            outputStream.write(b, off, size);
        }
        return size;
    }

    @Override
    public int read() throws IOException {
        int b = inputStream.read();
        if (b != -1) {
            outputStream.write(b);
        }
        return b;
    }

    @Override
    public void close() throws IOException {
        if (inputStream.available() > 0) {
            int l;
            if (bytes == null) {
                bytes = new byte[1024];
            }
            while ((l = inputStream.read(bytes)) > 0) {
                outputStream.write(bytes, 0, l);
            }
        }
        inputStream.close();
    }

    public static void main(String[] args) throws IOException {
        InputStream inputStream = new FileInputStream("test.xlsx");
        OutputStream outputStream = new FileOutputStream("upload.xlsx");
        InputStream teeInputStream = new TeeInputStream(inputStream, outputStream);
        Workbook workbook = new XSSFWorkbook(teeInputStream);
        Sheet sheet = workbook.getSheetAt(0);
        int start = sheet.getFirstRowNum();
        int end = sheet.getLastRowNum();
        while (start <= end) {
            Row row = sheet.getRow(start);
            System.out.println("[" + start + "]" + row.getCell(0).getStringCellValue());
            start++;
        }
        outputStream.close();
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值