Skip to content

Commit 3cfd7a4

Browse files
committed
PDFBOX-5845: avoid memory leak, by Mykola Bohdiuk
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/branches/3.0@1918648 13f79535-47bb-0310-9956-ffa450edef68
1 parent b744727 commit 3cfd7a4

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.IOException;
2323
import java.io.InputStream;
2424

25+
import org.apache.pdfbox.io.IOUtils;
2526
import org.apache.pdfbox.io.RandomAccessRead;
2627
import org.apache.pdfbox.io.RandomAccessReadBuffer;
2728
import org.apache.pdfbox.io.RandomAccessReadBufferedFile;
@@ -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,18 +58,29 @@ 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
/**
6465
* Creates a new TrueTypeCollection from a RandomAccessRead.
6566
*
66-
* @param randomAccessRead
67+
* @param randomAccessRead
68+
* @param closeAfterReading {@code true} to close randomAccessRead
6769
* @throws IOException If the font could not be parsed.
6870
*/
69-
TrueTypeCollection(RandomAccessRead randomAccessRead) throws IOException
71+
private TrueTypeCollection(RandomAccessRead randomAccessRead, boolean closeAfterReading) throws IOException
7072
{
71-
this.stream = new RandomAccessReadDataStream(randomAccessRead);
73+
try
74+
{
75+
this.stream = new RandomAccessReadDataStream(randomAccessRead);
76+
}
77+
finally
78+
{
79+
if (closeAfterReading)
80+
{
81+
IOUtils.closeQuietly(randomAccessRead);
82+
}
83+
}
7284

7385
// TTC header
7486
String tag = stream.readTag();

0 commit comments

Comments
 (0)