Skip to content

Commit 37c6266

Browse files
committed
[FIX] Leak: TrueTypeCollection didn't close FileChannel
1 parent 93c0331 commit 37c6266

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

fontbox/src/main/java/org/apache/fontbox/ttf/TrueTypeCollection.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.File;
2222
import java.io.IOException;
2323
import java.io.InputStream;
24+
import org.apache.pdfbox.io.IOUtils;
2425

2526
import org.apache.pdfbox.io.RandomAccessRead;
2627
import org.apache.pdfbox.io.RandomAccessReadBuffer;
@@ -46,7 +47,7 @@ public class TrueTypeCollection implements Closeable
4647
*/
4748
public TrueTypeCollection(File file) throws IOException
4849
{
49-
this(new RandomAccessReadBufferedFile(file));
50+
this(new RandomAccessReadBufferedFile(file), true);
5051
}
5152

5253
/**
@@ -57,7 +58,7 @@ public TrueTypeCollection(File file) throws IOException
5758
*/
5859
public TrueTypeCollection(InputStream stream) throws IOException
5960
{
60-
this(new RandomAccessReadBuffer(stream));
61+
this(new RandomAccessReadBuffer(stream), false);
6162
}
6263

6364
/**
@@ -68,7 +69,29 @@ public TrueTypeCollection(InputStream stream) throws IOException
6869
*/
6970
TrueTypeCollection(RandomAccessRead randomAccessRead) throws IOException
7071
{
71-
this.stream = new RandomAccessReadDataStream(randomAccessRead);
72+
this(randomAccessRead, false);
73+
}
74+
75+
/**
76+
* Creates a new TrueTypeCollection from a RandomAccessRead.
77+
*
78+
* @param randomAccessRead
79+
* @param closeAfterReading {@code true} to close randomAccessRead
80+
* @throws IOException If the font could not be parsed.
81+
*/
82+
private TrueTypeCollection(RandomAccessRead randomAccessRead, boolean closeAfterReading) throws IOException
83+
{
84+
try
85+
{
86+
this.stream = new RandomAccessReadDataStream(randomAccessRead);
87+
}
88+
finally
89+
{
90+
if (closeAfterReading)
91+
{
92+
IOUtils.closeQuietly(randomAccessRead);
93+
}
94+
}
7295

7396
// TTC header
7497
String tag = stream.readTag();

0 commit comments

Comments
 (0)