2121import java .io .File ;
2222import java .io .IOException ;
2323import java .io .InputStream ;
24+ import org .apache .pdfbox .io .IOUtils ;
2425
2526import org .apache .pdfbox .io .RandomAccessRead ;
2627import 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