Skip to content

Commit 94f1058

Browse files
committed
Return a copy of the ArrayPoolBufferWriter buffer in Ascii85, AsciiHex and RunLength filters and fix #964
1 parent 366fc4f commit 94f1058

File tree

4 files changed

+67
-9
lines changed

4 files changed

+67
-9
lines changed

src/UglyToad.PdfPig.Tests/Filters/Ascii85FilterTests.cs

+63-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ public void DecodesWikipediaExample()
2222

2323
var result = filter.Decode(bytes, dictionary, 0);
2424

25-
var text = Encoding.ASCII.GetString(result.ToArray());
25+
#if !NET
26+
string text = Encoding.ASCII.GetString(result.ToArray());
27+
#else
28+
string text = Encoding.ASCII.GetString(result.Span);
29+
#endif
2630

2731
Assert.Equal("Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, " +
2832
"that by a perseverance of delight in the continued and indefatigable generation of knowledge, " +
@@ -37,7 +41,11 @@ public void ReplacesZWithEmptyBytes()
3741

3842
var result = filter.Decode(bytes, dictionary, 1);
3943

40-
var text = Encoding.ASCII.GetString(result.ToArray());
44+
#if !NET
45+
string text = Encoding.ASCII.GetString(result.ToArray());
46+
#else
47+
string text = Encoding.ASCII.GetString(result.Span);
48+
#endif
4149

4250
Assert.Equal("Man \0\0\0\0is d", text);
4351
}
@@ -104,8 +112,12 @@ public void DecodesEncodedPdfContent()
104112

105113
var result = filter.Decode(Encoding.ASCII.GetBytes(input), dictionary, 0);
106114

107-
var text = Encoding.ASCII.GetString(result.ToArray());
108-
115+
#if !NET
116+
string text = Encoding.ASCII.GetString(result.ToArray());
117+
#else
118+
string text = Encoding.ASCII.GetString(result.Span);
119+
#endif
120+
109121
Assert.Equal(PdfContent.Replace("\r\n", "\n"), text);
110122
}
111123

@@ -124,11 +136,57 @@ public void DecodesEncodedPdfContentMissingEndOfDataSymbol()
124136

125137
var result = filter.Decode(Encoding.ASCII.GetBytes(input), dictionary, 0);
126138

127-
var text = Encoding.ASCII.GetString(result.ToArray());
139+
#if !NET
140+
string text = Encoding.ASCII.GetString(result.ToArray());
141+
#else
142+
string text = Encoding.ASCII.GetString(result.Span);
143+
#endif
128144

129145
Assert.Equal(PdfContent.Replace("\r\n", "\n"), text);
130146
}
131147

148+
[Fact]
149+
public void DecodeParallel()
150+
{
151+
Parallel.For(0, 100_000, i =>
152+
{
153+
if (i % 2 == 0)
154+
{
155+
var bytes = Encoding.ASCII.GetBytes("9jqo^zBlbD-");
156+
157+
var result = filter.Decode(bytes, dictionary, 1);
158+
159+
#if !NET
160+
string text = Encoding.ASCII.GetString(result.ToArray());
161+
#else
162+
string text = Encoding.ASCII.GetString(result.Span);
163+
#endif
164+
165+
Assert.Equal("Man \0\0\0\0is d", text);
166+
}
167+
else
168+
{
169+
const string input =
170+
@"0d&.mDdmGg4?O`>9P&*SFD)dS2E2gC4pl@QEb/Zr$8N_r$:7]!01IZ=0eskNAdU47<+?7h+B3Ol2_m!C+?)#1+B1
171+
`9>:<KhASu!rA7]9oF*)G6@;U'.@ps6t@V$[&ART*lARTXoCj@HP2DlU*/0HBI+B1r?0H_r%1a#ac$<nof.3LB""+=MAS+D58'ATD3qCj@.
172+
F@;@;70ea^uAKYi.Eb-A7E+*6f+EV:*DBN1?0ek+_+B1r?<%9""=ASu!rA7]9oF*)G6@;U'<.3MT)$8<SS1,pCU6jd-H;e7
173+
C#1,U1&Ft""Og2'=;YEa`c,ASu!rA8,po+Dk\3BQ%F&+CT;%+CQ]A1,'h!Ft""Oh2'=;UBl%3eCh4`'DBMbD7O]H>0H_br.:""&q8d[6p/M
174+
T()<(%'A;f?Ma+CT;%+E_a:A0>K&EZek1D/aN,F)u&6DBNA*A0>f4BOu4*+EM76E,9eK+B3(_<%9""p.!0AMEb031ATMF#F<G%,DIIR2+Cno
175+
&@3B9%+CT.1.3LK*+=KNS6V0ilAoD^,@<=+N>p**=$</Jt-rY&$AKYo'+EV:.+Cf>,E,oN2F(oQ1+D#G#De*R""B-;&&FD,T'F!+n3AKY4b
176+
F*22=@:F%a+=SF4C'moi+=Li?EZeh0FD)e-@<>p#@;]TuBl.9kATKCFGA(],AKYo5BOu4*+CT;%+C#7pF_Pr+@VfTuDf0B:+=SF4C'moi+=
177+
Li?EZek1DKKT1F`2DD/TboKAKY](@:s.m/h%oBC'mC/$>""*cF*)G6@;Q?_DIdZpC&~>";
132178

179+
var result = filter.Decode(Encoding.ASCII.GetBytes(input), dictionary, 0);
180+
181+
#if !NET
182+
string text = Encoding.ASCII.GetString(result.ToArray());
183+
#else
184+
string text = Encoding.ASCII.GetString(result.Span);
185+
#endif
186+
187+
Assert.Equal(PdfContent.Replace("\r\n", "\n"), text);
188+
}
189+
});
190+
}
133191
}
134192
}

src/UglyToad.PdfPig/Filters/Ascii85Filter.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public sealed class Ascii85Filter : IFilter
2929
/// <inheritdoc />
3030
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, int filterIndex)
3131
{
32-
var asciiBuffer = new byte[5];
32+
Span<byte> asciiBuffer = stackalloc byte[5];
3333

3434
var index = 0;
3535

@@ -96,7 +96,7 @@ public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken str
9696
WriteData(asciiBuffer, index, writer);
9797
}
9898

99-
return writer.WrittenMemory;
99+
return writer.WrittenMemory.ToArray();
100100
}
101101

102102
private static void WriteData(Span<byte> ascii, int index, ArrayPoolBufferWriter<byte> writer)

src/UglyToad.PdfPig/Filters/AsciiHexDecodeFilter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken str
6868
WriteHexToByte(pair, writer);
6969
}
7070

71-
return writer.WrittenMemory;
71+
return writer.WrittenMemory.ToArray();
7272
}
7373

7474
private static void WriteHexToByte(ReadOnlySpan<byte> hexBytes, ArrayPoolBufferWriter<byte> writer)

src/UglyToad.PdfPig/Filters/RunLengthFilter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken str
6262
}
6363
}
6464

65-
return output.WrittenMemory;
65+
return output.WrittenMemory.ToArray();
6666
}
6767
}
6868
}

0 commit comments

Comments
 (0)