0

BufferedInputStream を拡張する 'ResetOnCloseInputStream' を作成しました。これを WorkbookFactory.create(InputStream) に渡しているためです。ワークブックを読み取った後にストリームを閉じますが、ストリームを再度使用する必要があります。ResetOnInputStream は次のようになります。

public class ResetOnCloseInputStream extends BufferedInputStream {

private final InputStream decorated;

public ResetOnCloseInputStream(InputStream anInputStream) {
    super(anInputStream);
    if (!anInputStream.markSupported()) {
        throw new IllegalArgumentException("marking not supported");
    }

    anInputStream.mark( 1 << 24); // magic constant: BEWARE
    decorated = anInputStream;
}

@Override
public void close() throws IOException {
    decorated.reset();
}
public void realClose() throws IOException {
    decorated.close();
}

@Override
public int read() throws IOException {
    return decorated.read();
}
}

しかし、それが渡されると

workbook = WorkbookFactory.create(stream);

このエラーが発生します-

Caused by: java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:145)
at java.io.BufferedInputStream.read(BufferedInputStream.java:308)
4

1 に答える 1