java - 如何在不知道总流大小的情况下将 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。
解决方案
推荐阅读
- wpf - 如何在 WPF 中添加、保存和加载项目到列表视图
- cordova-plugins - 科尔多瓦 Firebase 分析插件不起作用
- swift - 如何将图像获取到另一个 Viewcontroller 中的某个 imageView,但尚未创建(为零)
- php - 插入了重复数据
- wordpress - Wordpress 作者页面链接重定向到首页
- sql - 如何使用 Spark 从 S3 数据集中高效地查找最新分区
- javafx - TableView 单元格请求焦点
- .net - 如何在我的 ASP.NET MVC 单体应用中使用独立的 ASP.NET Core 导航微服务?
- oracle - 我应该如何构造一个 Oracle 函数或其他代码来测量两条记录之间业务流程的时间
- java - 如何在多个 Activity 中使用 ViewModel 和 LiveData 观察者?