Skip to content

Commit d3fe53d

Browse files
committed
PDFBOX-6079: close input stream to avoid a resource leak
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1928805 13f79535-47bb-0310-9956-ffa450edef68
1 parent bbe1c53 commit d3fe53d

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

fontbox/src/main/java/org/apache/fontbox/cmap/CMapParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ private RandomAccessRead getExternalCMap(String name) throws IOException
480480
{
481481
throw new IOException("Error: Could not find referenced cmap stream " + name);
482482
}
483-
return new RandomAccessReadBuffer(is);
483+
return RandomAccessReadBuffer.createBufferFromStream(is);
484484
}
485485

486486
private Object parseNextToken(RandomAccessRead randomAcccessRead) throws IOException

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/FontMapperImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ final class FontMapperImpl implements FontMapper
122122
{
123123
throw new IOException("resource '" + resourceName + "' not found");
124124
}
125-
RandomAccessReadBuffer randomAccessReadBuffer = new RandomAccessReadBuffer(
126-
resourceAsStream);
125+
RandomAccessReadBuffer randomAccessReadBuffer = RandomAccessReadBuffer
126+
.createBufferFromStream(resourceAsStream);
127127
TTFParser ttfParser = new TTFParser();
128128
lastResortFont = ttfParser.parse(randomAccessReadBuffer);
129129
}

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,21 @@ public static PDTrueTypeFont load(PDDocument doc, File file, Encoding encoding)
195195
/**
196196
* Loads a TTF to be embedded into a document as a simple font.
197197
*
198-
* <p><b>Note:</b> Simple fonts only support 256 characters. For Unicode support, use
199-
* {@link PDType0Font#load(PDDocument, InputStream)} instead.</p>
198+
* <p>
199+
* <b>Note:</b> Simple fonts only support 256 characters. For Unicode support, use
200+
* {@link PDType0Font#load(PDDocument, InputStream)} instead.
201+
* </p>
200202
*
201203
* @param doc The PDF document that will hold the embedded font.
202-
* @param input A TTF file stream
204+
* @param input A TTF file stream. It will be closed before returning.
203205
* @param encoding The PostScript encoding vector to be used for embedding.
204206
* @return a PDTrueTypeFont instance.
205207
* @throws IOException If there is an error loading the data.
206208
*/
207209
public static PDTrueTypeFont load(PDDocument doc, InputStream input, Encoding encoding)
208210
throws IOException
209211
{
210-
return load(doc, new RandomAccessReadBuffer(input), encoding);
212+
return load(doc, RandomAccessReadBuffer.createBufferFromStream(input), encoding);
211213
}
212214

213215
/**

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public static PDType0Font load(PDDocument doc, InputStream input) throws IOExcep
182182
public static PDType0Font load(PDDocument doc, InputStream input, boolean embedSubset)
183183
throws IOException
184184
{
185-
return load(doc, new RandomAccessReadBuffer(input), embedSubset, false);
185+
return load(doc, RandomAccessReadBuffer.createBufferFromStream(input), embedSubset, false);
186186
}
187187

188188
/**
@@ -236,28 +236,28 @@ public static PDType0Font loadVertical(PDDocument doc, File file) throws IOExcep
236236
* Loads a TTF to be embedded into a document as a vertical Type 0 font.
237237
*
238238
* @param doc The PDF document that will hold the embedded font.
239-
* @param input A TrueType font.
239+
* @param input A TrueType font. It will be closed before returning.
240240
* @return A Type0 font with a CIDFontType2 descendant.
241241
* @throws IOException If there is an error reading the font stream.
242242
*/
243243
public static PDType0Font loadVertical(PDDocument doc, InputStream input) throws IOException
244244
{
245-
return load(doc, new RandomAccessReadBuffer(input), true, true);
245+
return load(doc, RandomAccessReadBuffer.createBufferFromStream(input), true, true);
246246
}
247247

248248
/**
249249
* Loads a TTF to be embedded into a document as a vertical Type 0 font.
250250
*
251251
* @param doc The PDF document that will hold the embedded font.
252-
* @param input A TrueType font.
252+
* @param input A TrueType font. It will be closed before returning.
253253
* @param embedSubset True if the font will be subset before embedding
254254
* @return A Type0 font with a CIDFontType2 descendant.
255255
* @throws IOException If there is an error reading the font stream.
256256
*/
257257
public static PDType0Font loadVertical(PDDocument doc, InputStream input, boolean embedSubset)
258258
throws IOException
259259
{
260-
return load(doc, new RandomAccessReadBuffer(input), embedSubset, true);
260+
return load(doc, RandomAccessReadBuffer.createBufferFromStream(input), embedSubset, true);
261261
}
262262

263263
/**

0 commit comments

Comments
 (0)