Java 在Observable.from()中抛出时未捕获RuntimeException
为了在下载文件时更新进度,我定制了一个InputStreamToFileRable类来使用rxjava:Java 在Observable.from()中抛出时未捕获RuntimeException,java,android,rx-java,rx-android,Java,Android,Rx Java,Rx Android,为了在下载文件时更新进度,我定制了一个InputStreamToFileRable类来使用rxjava: public class InputStreamToFileIterable implements Iterable<Integer> { private InputStream inputStream; private FileOutputStream outputStream; private byte[] buffer; public I
public class InputStreamToFileIterable implements Iterable<Integer> {
private InputStream inputStream;
private FileOutputStream outputStream;
private byte[] buffer;
public InputStreamToFileIterable(InputStream inputStream, FileOutputStream outputStream) {
this.inputStream = inputStream;
this.outputStream = outputStream;
this.buffer = new byte[512];
}
public Iterator<Integer> iterator() {
return new InputStreamIterator();
}
class InputStreamIterator implements Iterator<Integer> {
int data;
boolean isClosed;
int read;
public InputStreamIterator() {
read = 0;
try {
data = inputStream.read(buffer);
} catch (IOException e) {
throw new RuntimeException(e);
}
isClosed = false;
}
@Override
public boolean hasNext() {
return data != -1;
}
@Override
public Integer next() {
read += data;
try {
if (isClosed){
throw new NoSuchElementException();
}
if (!hasNext()) {
inputStream.close();
outputStream.close();
isClosed = true;
} else {
outputStream.write(buffer, 0, data);
data = inputStream.read(buffer);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return read;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
}
我没有忘记在observer中实现onError()方法
public Observer getObserverDownloadPicture() {
return Observers.create(
new Action1<Integer>() {
@Override
public void call(Integer integer) {
onProgressChanged(integer * 100 / total);
}
}
, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
Log.e(TAG, throwable.getMessage());
try {
fileOutputStream.close();
isDownloading = false;
file.delete();
} catch (Exception e) {
ExceptionUtils.saveLog(e);
}
progressTextView.setText("载入失败");
subscriptionList.clear();
}
}
, new Action0() {
@Override
public void call() {
try {
fileOutputStream.close();
} catch (Exception e) {
Log.d(TAG, e.getMessage());
}
if (isDownloading) {
setupPicture();
}
isDownloading = false;
subscriptionList.clear();
}
}
);
}
public Observer getobserver下载图片(){
return.create(
新行动1(){
@凌驾
公共无效调用(整数){
onProgressChanged(整数*100/总计);
}
}
,新行动1(){
@凌驾
公共无效呼叫(可丢弃可丢弃){
Log.e(标记,throwable.getMessage());
试一试{
fileOutputStream.close();
isDownloading=false;
delete();
}捕获(例外e){
例外事项。保存日志(e);
}
progressTextView.setText(“载入失败");
subscriptionList.clear();
}
}
,新操作0(){
@凌驾
公开作废通知(){
试一试{
fileOutputStream.close();
}捕获(例外e){
Log.d(标记,例如getMessage());
}
如果(正在下载){
设置图片();
}
isDownloading=false;
subscriptionList.clear();
}
}
);
}
我如何处理异常?请帮我解决这个问题。尝试用具体的异常替换catch块中的
Exception
。类似如下:
catch (IOException e) {
Log.d(TAG, e.getMessage());
}
根据堆栈跟踪,
IlleagalStateException
将在next()中抛出
方法。但是,对于IOException
,我只能看到一个catch
。您希望捕获异常的确切位置。这是可观察到的一个缺点。从开始,让迭代器方法的异常失效,而不是向下游发出异常。我将发布一个解决方案。
public Observer getObserverDownloadPicture() {
return Observers.create(
new Action1<Integer>() {
@Override
public void call(Integer integer) {
onProgressChanged(integer * 100 / total);
}
}
, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
Log.e(TAG, throwable.getMessage());
try {
fileOutputStream.close();
isDownloading = false;
file.delete();
} catch (Exception e) {
ExceptionUtils.saveLog(e);
}
progressTextView.setText("载入失败");
subscriptionList.clear();
}
}
, new Action0() {
@Override
public void call() {
try {
fileOutputStream.close();
} catch (Exception e) {
Log.d(TAG, e.getMessage());
}
if (isDownloading) {
setupPicture();
}
isDownloading = false;
subscriptionList.clear();
}
}
);
}
catch (IOException e) {
Log.d(TAG, e.getMessage());
}