#64301 - Allow try-with-resources with OPCPackage.revert()

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1876018 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2020-04-01 23:23:55 +00:00
parent d383fea7bd
commit efd9c7c277
3 changed files with 34 additions and 16 deletions

View File

@ -32,11 +32,16 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.poi.ooxml.util.PackageHelper;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException;
@ -432,6 +437,10 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
*/
@Override
public void close() throws IOException {
if (isClosed()) {
return;
}
if (this.packageAccess == PackageAccess.READ) {
logger.log(POILogger.WARN,
"The close() method is intended to SAVE a package. This package is open in READ ONLY mode, use the revert() method instead !");
@ -1654,4 +1663,9 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
public int getUnusedPartIndex(final String nameTemplate) throws InvalidFormatException {
return partList.getUnusedPartIndex(nameTemplate);
}
/**
* Has close been called already?
*/
public abstract boolean isClosed();
}

View File

@ -564,4 +564,11 @@ public final class ZipPackage extends OPCPackage {
public ZipEntrySource getZipArchive() {
return zipArchive;
}
@Override
public boolean isClosed() {
// if zipArchive == null, it might be created on the fly
// so only return true, if a zip archive was initialized before
return zipArchive != null && zipArchive.isClosed();
}
}

View File

@ -78,7 +78,6 @@ import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.POIDataSamples;
import org.apache.poi.ooxml.util.DocumentHelper;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
@ -431,8 +430,6 @@ public class TestSignatureInfo {
boolean b = si.verifySignature();
assertFalse("signature should be broken", b);
}
thrown.expectMessage("Fail to save: an error occurs while saving the package : Zip File is close");
thrown.expect(OpenXML4JRuntimeException.class);
}
}