首页 > 解决方案 > 如何在不知道总流大小的情况下将 InputStream 拆分为多个 BoundedInputStream?

问题描述

我正在研究一个开源 Swift 库,它能够将 InputStream 拆分为多个 BoundedInputStream 对象,给定总流大小(知道何时停止创建有界输入流)。我不明白为什么一旦关闭初始 InputStream 就没有选项可以自动停止 BoundedInputStream 创建。

代码看起来像这样:

protected Long segmentationSize = 5368709120L;
protected Long currentSegment = 0L;
private InputStream inputStream; // supplied externally
private long inputStreamSize; // supplied externally

public void uploadSegmentedObjects() {
    InputStream segmentStream = getNextSegment();
    while (segmentStream != null) {
        // do something
    }
}

public InputStream getNextSegment() {
    if (done()) {
        return null;
    }
    InputStream segment = createSegment();
    currentSegment++;
    return segment;
}

protected boolean done() {
    return currentSegment * segmentationSize > inputStreamSize;
}

@Override
protected InputStream createSegment() throws IOException {
    BoundedInputStream stream = new BoundedInputStream(inputStream, segmentationSize);
    stream.setPropagateClose(false);
    return stream;
}

本质上,我需要知道如何重写 done() 方法,使其不依赖于 inputStreamSize 变量,而是在流关闭时返回 null。

标签: javainputstreamopenstack-swift

解决方案


推荐阅读