Created attachment 34228 [details] Word docx file failed to write docx XWPFDocument opened from FileInputStream failed to write out to a new FileOutputStream Here's the stacktrace -- org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Rule M2.4 exception : this error should NEVER happen! Please raise a bug at https://bz.apache.org/bugzilla/enter_bug.cgi?product=POI and attach a file that triggers it, thanks! at org.apache.poi.openxml4j.opc.internal.ContentTypeManager.getContentType(ContentTypeManager.java:343) at org.apache.poi.openxml4j.opc.internal.ContentTypeManager.removeContentType(ContentTypeManager.java:256) at org.apache.poi.openxml4j.opc.OPCPackage.removePart(OPCPackage.java:958) at org.apache.poi.openxml4j.opc.PackagePart.getOutputStream(PackagePart.java:522) at org.apache.poi.xwpf.usermodel.XWPFDocument.commit(XWPFDocument.java:716) at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:464) at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:211)
I am unable to reproduce this in the latest nightly build of POI (r1755847). Could you either check this against 3.15-beta2 or later a nightly, or produce a unit test that produces the error seen in comment 0? Apache commons collections was recently added as a dependency and will need to be added to your class path. https://builds.apache.org/job/POI/lastSuccessfulBuild/artifact/build/dist/ Here's my test case: @Test public void test60102() throws Exception { // read in from a java.io.File //XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("60102.docx"); // or read in from a java.io.FileInputStream File infile = POIDataSamples.getDocumentInstance().getFile("60102.docx"); InputStream fis = new FileInputStream(infile); XWPFDocument doc = new XWPFDocument(fis); // Write out to a file File outfile = TempFile.createTempFile("60102-resaved", "docx"); OutputStream fos = new FileOutputStream(outfile); doc.write(fos); fos.close(); // Write out to a byte array output stream XWPFDocument doc2 = XWPFTestDataSamples.writeOutAndReadBack(doc); doc2.close(); doc.close(); }
I've debugged into the issue further. The problem is reproduced after the OutputStream is closed by mistake after the first write. Change test case test60102() to following -- // Write out to a file File outfile = TempFile.createTempFile("60102-resaved", "docx"); OutputStream fos = new FileOutputStream(outfile); doc.write(fos); fos.close(); doc.write(fos); The second doc.write(fos) failed because OutputStream was closed by mistake. The exception message was misleading. However, I've consider this is a usage error.
Throw IOException when writing a closed document implemented in r1760206 OutputStream out; doc.close(); doc.write(out); I have not yet implemented throwing an exception when writing an open document to a closed output stream. OutputStream out; out.close(); doc.write(out);
Created attachment 34231 [details] Replace OpenXML4J return boolean with throws OpenXML4JException (In reply to Javen O'Neal from comment #3) > I have not yet implemented throwing an exception when writing an open > document to a closed output stream. > OutputStream out; > out.close(); > doc.write(out); To get this one closed, we're going to have to make quite a few changes to methods in org.apache.poi.openxml4j.opc.internal. These methods currently catch exceptions, occasionally log the error to the POILogger, and then return a boolean success value. These will need to be replaced with void-returning functions that raise an exception. This will eliminate the need to check a return code, enable the POI logger, and check the logs. A quick glance at the source code history reveals that the OpenXML4j classes have returned boolean success rather than throwing exceptions. Perhaps this is because this library originated from a C project rewritten in Java without replacing the C idioms with Java idioms. https://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/marshallers/DefaultMarshaller.java?revision=738842&view=markup#l41
*** Bug 59559 has been marked as a duplicate of this bug. ***
*** Bug 60967 has been marked as a duplicate of this bug. ***
*** Bug 61987 has been marked as a duplicate of this bug. ***
*** Bug 59806 has been marked as a duplicate of this bug. ***
*** Bug 59158 has been marked as a duplicate of this bug. ***