Skip to content

Commit e6fd4e3

Browse files
committed
PDFBOX-5996: Set size for ByteArrayOutputStreams, as suggested by Axel Howind
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1925301 13f79535-47bb-0310-9956-ffa450edef68
1 parent cd24abd commit e6fd4e3

File tree

5 files changed

+418
-419
lines changed

5 files changed

+418
-419
lines changed

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ private void writeTableBody(OutputStream os, byte[] bytes) throws IOException
235235

236236
private byte[] buildHeadTable() throws IOException
237237
{
238-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
238+
ByteArrayOutputStream bos = new ByteArrayOutputStream(54);
239239
DataOutputStream out = new DataOutputStream(bos);
240240

241241
HeaderTable h = ttf.getHeader();
@@ -264,7 +264,7 @@ private byte[] buildHeadTable() throws IOException
264264

265265
private byte[] buildHheaTable() throws IOException
266266
{
267-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
267+
ByteArrayOutputStream bos = new ByteArrayOutputStream(36);
268268
DataOutputStream out = new DataOutputStream(bos);
269269

270270
HorizontalHeaderTable h = ttf.getHorizontalHeader();
@@ -308,7 +308,7 @@ private boolean shouldCopyNameRecord(NameRecord nr)
308308

309309
private byte[] buildNameTable() throws IOException
310310
{
311-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
311+
ByteArrayOutputStream bos = new ByteArrayOutputStream(512);
312312
DataOutputStream out = new DataOutputStream(bos);
313313

314314
NamingTable name = ttf.getNaming();
@@ -393,7 +393,7 @@ else if (encoding == 1) // ISO 10646=
393393

394394
private byte[] buildMaxpTable() throws IOException
395395
{
396-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
396+
ByteArrayOutputStream bos = new ByteArrayOutputStream(32);
397397
DataOutputStream out = new DataOutputStream(bos);
398398

399399
MaximumProfileTable p = ttf.getMaximumProfile();
@@ -428,7 +428,7 @@ private byte[] buildOS2Table() throws IOException
428428
return null;
429429
}
430430

431-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
431+
ByteArrayOutputStream bos = new ByteArrayOutputStream(78);
432432
DataOutputStream out = new DataOutputStream(bos);
433433

434434
writeUint16(out, os2.getVersion());
@@ -476,7 +476,7 @@ private byte[] buildOS2Table() throws IOException
476476
// never returns null
477477
private byte[] buildLocaTable(long[] newOffsets) throws IOException
478478
{
479-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
479+
ByteArrayOutputStream bos = new ByteArrayOutputStream(newOffsets.length * 4);
480480
DataOutputStream out = new DataOutputStream(bos);
481481

482482
for (long offset : newOffsets)
@@ -598,7 +598,7 @@ else if ((flags & 1 << 3) != 0)
598598
// never returns null
599599
private byte[] buildGlyfTable(long[] newOffsets) throws IOException
600600
{
601-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
601+
ByteArrayOutputStream bos = new ByteArrayOutputStream(512);
602602

603603
GlyphTable g = ttf.getGlyph();
604604
long[] offsets = ttf.getIndexToLocation().getOffsets();
@@ -749,7 +749,7 @@ private byte[] buildCmapTable() throws IOException
749749
return null;
750750
}
751751

752-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
752+
ByteArrayOutputStream bos = new ByteArrayOutputStream(64);
753753
DataOutputStream out = new DataOutputStream(bos);
754754

755755
// cmap header
@@ -869,7 +869,7 @@ private byte[] buildPostTable() throws IOException
869869
return null;
870870
}
871871

872-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
872+
ByteArrayOutputStream bos = new ByteArrayOutputStream(64);
873873
DataOutputStream out = new DataOutputStream(bos);
874874

875875
writeFixed(out, 2.0); // version

pdfbox/src/main/java/org/apache/pdfbox/cos/COSString.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ public COSString(String text, boolean forceHex)
135135
*/
136136
public static COSString parseHex(String hex) throws IOException
137137
{
138-
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
139138
StringBuilder hexBuffer = new StringBuilder(hex.trim());
140139

141140
// if odd number then the last hex digit is assumed to be 0
@@ -145,6 +144,7 @@ public static COSString parseHex(String hex) throws IOException
145144
}
146145

147146
int length = hexBuffer.length();
147+
ByteArrayOutputStream bytes = new ByteArrayOutputStream((length + 1) / 2);
148148
for (int i = 0; i < length; i += 2)
149149
{
150150
try

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ public float getWidth(int code) throws IOException
328328
*/
329329
public final byte[] encode(String text) throws IOException
330330
{
331-
ByteArrayOutputStream out = new ByteArrayOutputStream();
331+
ByteArrayOutputStream out = new ByteArrayOutputStream(Math.max(32, text.length()));
332332
int offset = 0;
333333
while (offset < text.length())
334334
{

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/CCITTFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static PDImageXObject createFromImage(PDDocument document, BufferedImage
6868
int height = image.getHeight();
6969
int width = image.getWidth();
7070

71-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
71+
ByteArrayOutputStream bos = new ByteArrayOutputStream(Math.max(32, (width + 1) * height));
7272
try (MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(bos))
7373
{
7474
for (int y = 0; y < height; ++y)

0 commit comments

Comments
 (0)