说明“重置为无效标记“对于大文件,在标记inputStream并将其重置时出现. [英] Exception stating " Resetting to invalid mark " comes while marking an inputStream and resetting it, for Large files.?

查看:112
本文介绍了说明“重置为无效标记“对于大文件,在标记inputStream并将其重置时出现.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用InputStream对象来计算某些文件的Md5. 我标记了流 稍后我重置流.但是,对于大文件,以下例外情况会出现...

I am using InputStream object to calculate Md5 of some file. I mark the stream Later I reset the stream. However for large files the following exception comes...

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)


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


I have tried closing the stream and reopening it this way.. just to get some exceptions as follows::

 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时,出现以下异常.

However the following exception comes when I try to resuse the newly retrived InputStream.

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

推荐答案

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

I'm going to assume you are using a BufferedInputStream because its source code for reset() method is

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

If you have no control over it, just reopen the stream. If you can't reopen the stream, ie. you're retrieving the same instance every time, consider using a ByteArrayOutputStream

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

You can read the original InputStream into a ByteArrayOutputStream. Copy the bytes in that stream into a new ByteArrayInputStream. Pass that to the MD5 calculator. Then create a new ByteArrayInputStream again with the same bytes and pass that to whatever else you need.

这篇关于说明“重置为无效标记“对于大文件,在标记inputStream并将其重置时出现.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆