Linda
Linda

Reputation: 1233

why am i not able to reuse InputStream?

public static String getTextOf(String xsl) throws Exception {
      DocXHandler docxh1 = new DocXHandler(ACE.getInputFilePath());
      InputStream inputDocumentXMLStream = docxh1.getInputDocumentXMLZERO();
    return new Cache().getXSLOutput(inputDocumentXMLStream, xsl);
}

The above method will be called more than 100 times with different xsl arguments. Now each time InputStream inputDocumentXMLStream has been assigned value( yeah more than 100 times). As i am trying to refactor this code, in such a way that only one time InputStream will be assigned value.My code became like this now,

  public static String getTextOf(String xsl) throws Exception {
     return new Cache().getXSLOutput(inputDocumentXMLStream, xsl);
   }

by changing inputDocumentXMLStream as global property. First time i am getting the correct result BUT for the second time i get the below error,

Unexpected end of ZLIB input stream

so tel me how to read it again?

Upvotes: 0

Views: 2615

Answers (5)

Yogendra Singh
Yogendra Singh

Reputation: 34367

You can't restart reading the input stream after reach to the end of the steam as it moves in single direction only.

I think the best idea to solve your problem is to fully parse your XML file once and place the values in the some java objects. Once done, you can simply use the map to to retrieve the required values. This will be much efficient too.

e.g.

    Object parseValueMap = paseXML(inputDocumentXMLStream);//use appropriate object
    //^ called only once, you may use some libraries such as JAXB to parse the xml

Assuming xsl is a path expression, you may want to apply the path on Java collection created in above step. You may use libraries such as JXPath as :

     String value = JXPathEvaluator(parseValueMap, xsl);

Hope this helps.

Upvotes: 1

ramsinb
ramsinb

Reputation: 2005

I'm not sure what you are doing in getXSLOutput and why you need to read the same inputstream 100 times however to avoid EOF you can use mark(int) to mark the start of the stream in yourgetXSLOutput method and call reset at the end of the same method. Seems like the wrong thing to do though...

For better results try reading it once and storing the contents in a datatype inJava then working on that 100 times.

Upvotes: 1

Talha Ahmed Khan
Talha Ahmed Khan

Reputation: 15453

for each xls file you have to parse the file and create a Excel Document instance of Java. That Java instance will give you InputStream. So there is no escape for you in this case.

But for the same file you can use the same InputStream. And that would be a single unit or work.

Upvotes: 0

Farhan Syed
Farhan Syed

Reputation: 326

You can't reuse an input stream across file with out closing it. All file I/O functions rely on EOF to stop.

Upvotes: 0

user207421
user207421

Reputation: 310985

You can only read a stream once, then you are at EOF, as the error message says. What you're trying to do doesn't make sense.

Upvotes: 2

Related Questions