Skip to content

Commit 4de2469

Browse files
committed
Fixed the case where FastByteArrayOutputStream was throwing a RuntimeException for empty Strings
1 parent e94f3b0 commit 4de2469

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/main/java/org/sejda/sambox/cos/PDFDocEncoding.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public static String toString(byte[] bytes)
144144
*/
145145
public static byte[] getBytes(String text)
146146
{
147-
if (nonNull(text))
147+
if (nonNull(text) && !text.isEmpty())
148148
{
149149
try (var out = new FastByteArrayOutputStream(text.length()))
150150
{

src/test/java/org/sejda/sambox/cos/PDFDocEncodingTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void testDeviations()
9393
}
9494

9595
@Test
96-
public void nullSafe() throws IOException
96+
public void nullSafe()
9797
{
9898
var sb = new StringBuilder("Chuck");
9999
sb.append((char) 0x18);
@@ -103,6 +103,18 @@ public void nullSafe() throws IOException
103103
assertNull(PDFDocEncoding.getBytes(sb.toString()));
104104
}
105105

106+
@Test
107+
public void nullText()
108+
{
109+
assertNull(PDFDocEncoding.getBytes(null));
110+
}
111+
112+
@Test
113+
public void blankSafe()
114+
{
115+
assertNull(PDFDocEncoding.getBytes(""));
116+
}
117+
106118
/**
107119
* PDFBOX-3864: Test that chars smaller than 256 which are NOT part of PDFDocEncoding are
108120
* handled correctly.

0 commit comments

Comments
 (0)