Stream closed exception in java -
please @ have done
private inputsource getcontent(string fname) throws saxexception, ioexception, serviceexception { // code here if(currentnoderef != null) { contentreader reader = contentservice.getreader(currentnoderef,contentmodel.prop_content); inputstream inputstream = null; try { inputstream = new bufferedinputstream(reader.getcontentinputstream(),16384); return new inputsource(inputstream); } { if(inputstream!=null) try { inputstream.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } } return new inputsource(); }
in parsedocument method called above method.
parsedocroot(getcontent(fname),path);
in parsedocroot
public void parsedocroot (inputsource ins, string path) throws saxexception, ioexception, parserconfigurationexception, serviceexception { documentbuilderfactory factory = documentbuilderfactory.newinstance(); documentbuilder builder = factory.newdocumentbuilder(); builder.setentityresolver(new entityresolver() { public inputsource resolveentity(string publicid, string systemid) throws saxexception, ioexception { return new inputsource(new bytearrayinputstream(new byte[0])); } }); document doc = builder.parse(ins); nodelist list = doc.getchildnodes(); parsedocument(list,path); }
i got error saying stream not closed , while debugging above code found error on line
document doc = builder.parse(ins);
please me finding solution.
i believe error stream is closed. reason have block:
try { inputstream = new bufferedinputstream(reader.getcontentinputstream(),16384); return new inputsource(inputstream); } { if(inputstream!=null) try { inputstream.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } }
you close stream in block. code gets executed before returning method. importantly, inputstream isn't consumed until after method returns , processed parse
method.
you should instead put block around entire block of code -- closing stream when done. easiest way inline getcontent
method , put block after calling parse
. after that, might able figure out way encapsulate logic, it'll kind of tricky since need keep handle the inputstream until you're done parsing can close it.
another far simpler option make getcontent
return document
instead moving parsedocroot(getcontent(fname),path);
inside method.
Comments
Post a Comment