[bug-62768] synchronize OPCPackage#close()

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1842142 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2018-09-27 19:06:06 +00:00
parent 19baa76652
commit 5c1a036de2

View File

@ -28,7 +28,6 @@ import java.io.OutputStream;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.*; import java.util.*;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -420,7 +419,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
* If an IO exception occur during the saving process. * If an IO exception occur during the saving process.
*/ */
@Override @Override
public void close() throws IOException { public synchronized void close() throws IOException {
if (this.packageAccess == PackageAccess.READ) { if (this.packageAccess == PackageAccess.READ) {
logger.log(POILogger.WARN, 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 !"); "The close() method is intended to SAVE a package. This package is open in READ ONLY mode, use the revert() method instead !");
@ -434,27 +433,20 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
return; return;
} }
// Save the content if (this.originalPackagePath != null
ReentrantReadWriteLock l = new ReentrantReadWriteLock(); && !this.originalPackagePath.trim().isEmpty()) {
try { File targetFile = new File(this.originalPackagePath);
l.writeLock().lock(); if (!targetFile.exists()
if (this.originalPackagePath != null || !(this.originalPackagePath
&& !this.originalPackagePath.trim().isEmpty()) { .equalsIgnoreCase(targetFile.getAbsolutePath()))) {
File targetFile = new File(this.originalPackagePath); // Case of a package created from scratch
if (!targetFile.exists() save(targetFile);
|| !(this.originalPackagePath } else {
.equalsIgnoreCase(targetFile.getAbsolutePath()))) { closeImpl();
// Case of a package created from scratch
save(targetFile);
} else {
closeImpl();
}
} else if (this.output != null) {
save(this.output);
output.close();
} }
} finally { } else if (this.output != null) {
l.writeLock().unlock(); save(this.output);
output.close();
} }
// Clear // Clear