@@ -232,7 +232,7 @@ public static RandomAccessRead decode(InputStream encoded, List<Filter> filterLi
232232 COSDictionary parameters , DecodeOptions options , List <DecodeResult > results )
233233 throws IOException
234234 {
235- int length = parameters .getInt (COSName .LENGTH ,
235+ long length = parameters .getLong (COSName .LENGTH ,
236236 RandomAccessReadBuffer .DEFAULT_CHUNK_SIZE_4KB );
237237 if (filterList .isEmpty ())
238238 {
@@ -266,17 +266,19 @@ public static RandomAccessRead decode(InputStream encoded, List<Filter> filterLi
266266 {
267267 randomAccessWriteBuffer .seek (0 );
268268 input = new RandomAccessInputStream (randomAccessWriteBuffer );
269- length = ( int ) randomAccessWriteBuffer .length ();
269+ length = randomAccessWriteBuffer .length ();
270270 }
271- // avoid invalid values
272- length = length <= 0 ? RandomAccessReadBuffer .DEFAULT_CHUNK_SIZE_4KB : length ;
273271 // we don't know the size of the decoded stream, just estimate a 4 times bigger size than the encoded stream
274272 // use the estimated stream size as chunk size, use the default chunk size as limit to avoid to big values
275- length <<= 2 ;
276- length = length <= 0 ? // PDFBOX-5908 avoid invalid values (again)
277- RandomAccessReadBuffer .DEFAULT_CHUNK_SIZE_4KB :
278- Math .min (length , RandomAccessReadBuffer .DEFAULT_CHUNK_SIZE_4KB );
279- randomAccessWriteBuffer = new RandomAccessReadWriteBuffer (length );
273+ if (length >= RandomAccessReadBuffer .DEFAULT_CHUNK_SIZE_4KB / 4 )
274+ {
275+ length = RandomAccessReadBuffer .DEFAULT_CHUNK_SIZE_4KB ;
276+ }
277+ else
278+ {
279+ length = Math .min (length << 2 , RandomAccessReadBuffer .DEFAULT_CHUNK_SIZE_4KB );
280+ }
281+ randomAccessWriteBuffer = new RandomAccessReadWriteBuffer ((int ) length );
280282 output = new RandomAccessOutputStream (randomAccessWriteBuffer );
281283 try
282284 {
0 commit comments