gpt4 book ai didi

java - 异常说明 "Resetting to invalid mark "在标记 inputStream 并重置它时出现,对于大文件。?

转载 作者:搜寻专家 更新时间:2023-11-01 02:47:35 30 4
gpt4 key购买 nike

我正在使用 InputStream 对象来计算某个文件的 Md5。我标记流后来我重置了流。但是,对于大文件,会出现以下异常...

inStreamLatestFile.mark(0);
checkSumCalculated = MD5CheckSumCalculator.calculateMD5CheckSum(inStreamLatestFile);
inStreamLatestFile.reset();

异常

.Md5ValidationAggrStrat ||**Error in calculating checksum:: java.io.IOException: Resetting to invalid mark**
||java.io.IOException: Resetting to invalid mark
||at java.io.BufferedInputStream.reset(BufferedInputStream.java:437)
||at com.amadeus.apt.ib.modules.func.map.camel.strategy.Md5ValidationAggrStrategy.aggregate(Md5ValidationAggrStrategy.java:81)
||at org.apache.camel.processor.aggregate.AggregateProcessor.onAggregation(AggregateProcessor.java:365)
||at org.apache.camel.processor.aggregate.AggregateProcessor.doAggregation(AggregateProcessor.java:245)
||at org.apache.camel.processor.aggregate.AggregateProcessor.process(AggregateProcessor.java:201)
||at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
||at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)

我试过关闭流并以这种方式重新打开它..只是为了得到一些异常如下::

 try {
inStreamLatestFile= ExchangeHelper.getMandatoryInBody(
oldExchange, InputStream.class);

//inStreamLatestFile.mark(0);
checkSumCalculated = MD5CheckSumCalculator.calculateMD5CheckSum(inStreamLatestFile);

//closing the inputStream of the latest file
if(inStreamLatestFile != null){
try {
inStreamLatestFile.close();
} catch (IOException e) {
logger.error("Error occurred in closing the stream :: "+ e.getMessage());
}
}


tempInputStream= ExchangeHelper.getMandatoryInBody(
oldExchange, InputStream.class);
oldExchange.getIn().setBody(tempInputStream);

但是,当我尝试重新使用新检索的 InputStream 时,出现以下异常。

 caught: java.io.IOException: Stream closed: java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:162)

最佳答案

我假设您正在使用 BufferedInputStream,因为它的 reset() 方法的源代码是

public synchronized void reset() throws IOException {
getBufIfOpen(); // Cause exception if closed
if (markpos < 0)
throw new IOException("Resetting to invalid mark"); // exception you are getting
pos = markpos;
}

下面的调用

MD5CheckSumCalculator.calculateMD5CheckSum(inStreamLatestFile);

必须对 markPos 做些什么。

如果您无法控制它,只需重新打开流。如果您无法重新打开流,即。您每次都检索相同的实例,请考虑使用 ByteArrayOutputStream

您可以将原始的 InputStream 读取到 ByteArrayOutputStream 中。将该流中的字节复制到新的 ByteArrayInputStream 中。将其传递给 MD5 计算器。然后使用相同的字节再次创建一个新的 ByteArrayInputStream 并将其传递给您需要的任何其他内容。

关于java - 异常说明 "Resetting to invalid mark "在标记 inputStream 并重置它时出现,对于大文件。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18573767/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com