close package parts when closing OPCPackage (#880)

* close package parts when closing OPCPackage

* Update ZipPackagePart.java
This commit is contained in:
PJ Fanning 2025-08-14 10:58:46 +01:00 committed by GitHub
parent e98a5991e7
commit c0e6a9bed0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 21 additions and 8 deletions

View File

@ -681,6 +681,9 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
// ensure all held resources are freed
revert();
// ensure resources associated with package parts are closed
closeParts();
// Clear
this.contentTypeManager.clearAll();
}
@ -1870,6 +1873,10 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
*/
public abstract boolean isClosed();
protected void closeParts() {
partList.closeParts();
}
@Override
public String toString() {
return "OPCPackage{" +

View File

@ -112,8 +112,6 @@ public final class PackagePartCollection implements Serializable {
return packagePartLookup.size();
}
/**
* Get an unused part index based on the namePattern, which doesn't exist yet
* and has the lowest positive index
@ -141,4 +139,11 @@ public final class PackagePartCollection implements Serializable {
.mapToInt(indexFromName)
.collect(SparseBitSet::new, SparseBitSet::set, (s1,s2) -> s1.or(s2)).nextClearBit(1);
}
// used to ensure resources are closed when they are no longer needed
void closeParts() {
for (PackagePart part : packagePartLookup.values()) {
part.close();
}
}
}

View File

@ -595,12 +595,15 @@ public final class ZipPackage extends OPCPackage {
}
}
}
// ensure resources associated with package parts are closed
closeParts();
}
/**
* Create a unique identifier to be use as a temp file name.
* Create a unique identifier to be used as a temp file name.
*
* @return A unique identifier use to be use as a temp file name.
* @return A unique identifier to be used as a temp file name.
*/
private synchronized String generateTempFileName(File directory) {
File tmpFilename;

View File

@ -134,14 +134,12 @@ public class ZipPackagePart extends PackagePart {
}
@Override
@NotImplemented
public void close() {
throw new InvalidOperationException("Method not implemented !");
// do nothing
}
@Override
@NotImplemented
public void flush() {
throw new InvalidOperationException("Method not implemented !");
// do nothing
}
}