@@ -92,9 +92,13 @@ public class PdfReader implements PdfViewerPreferences, Closeable {
9292 static final PdfName [] pageInhCandidates = {PdfName .MEDIABOX ,
9393 PdfName .ROTATE , PdfName .RESOURCES , PdfName .CROPBOX };
9494
95- private static final byte [] endstream = PdfEncodings
96- .convertToBytes ("endstream" , null );
95+ private static final String ENDSTREAM = "endstream" ;
96+ private static final byte [] endstreamBytes = PdfEncodings
97+ .convertToBytes (ENDSTREAM , null );
9798 private static final byte [] endobj = PdfEncodings .convertToBytes ("endobj" , null );
99+ private static final String FALSE = "false" ;
100+ private static final String ILLEGAL_LENGTH_VALUE = "illegal.length.value" ;
101+ public static final String BAD_USER_PASSWORD = "bad.user.password" ;
98102 private final PdfViewerPreferencesImp viewerPreferences = new PdfViewerPreferencesImp ();
99103 protected PRTokeniser tokens ;
100104 // Each xref pair is a position
@@ -1482,12 +1486,12 @@ private void readDecryptedDocObj() throws IOException {
14821486 o = enc .get (PdfName .LENGTH );
14831487 if (!o .isNumber ()) {
14841488 throw new InvalidPdfException (
1485- MessageLocalization .getComposedMessage ("illegal.length.value" ));
1489+ MessageLocalization .getComposedMessage (ILLEGAL_LENGTH_VALUE ));
14861490 }
14871491 lengthValue = ((PdfNumber ) o ).intValue ();
14881492 if (lengthValue > 128 || lengthValue < 40 || lengthValue % 8 != 0 ) {
14891493 throw new InvalidPdfException (
1490- MessageLocalization .getComposedMessage ("illegal.length.value" ));
1494+ MessageLocalization .getComposedMessage (ILLEGAL_LENGTH_VALUE ));
14911495 }
14921496 cryptoMode = PdfWriter .STANDARD_ENCRYPTION_128 ;
14931497 break ;
@@ -1513,14 +1517,14 @@ private void readDecryptedDocObj() throws IOException {
15131517 .getComposedMessage ("no.compatible.encryption.found" ));
15141518 }
15151519 PdfObject em = enc .get (PdfName .ENCRYPTMETADATA );
1516- if (em != null && em .toString ().equals ("false" )) {
1520+ if (em != null && em .toString ().equals (FALSE )) {
15171521 cryptoMode |= PdfWriter .DO_NOT_ENCRYPT_METADATA ;
15181522 }
15191523 break ;
15201524 case 6 :
15211525 cryptoMode = PdfWriter .ENCRYPTION_AES_256_V3 ;
15221526 em = enc .get (PdfName .ENCRYPTMETADATA );
1523- if (em != null && em .toString ().equals ("false" )) {
1527+ if (em != null && em .toString ().equals (FALSE )) {
15241528 cryptoMode |= PdfWriter .DO_NOT_ENCRYPT_METADATA ;
15251529 }
15261530 break ;
@@ -1548,12 +1552,12 @@ private void readDecryptedDocObj() throws IOException {
15481552 o = enc .get (PdfName .LENGTH );
15491553 if (!o .isNumber ()) {
15501554 throw new InvalidPdfException (
1551- MessageLocalization .getComposedMessage ("illegal.length.value" ));
1555+ MessageLocalization .getComposedMessage (ILLEGAL_LENGTH_VALUE ));
15521556 }
15531557 lengthValue = ((PdfNumber ) o ).intValue ();
15541558 if (lengthValue > 128 || lengthValue < 40 || lengthValue % 8 != 0 ) {
15551559 throw new InvalidPdfException (
1556- MessageLocalization .getComposedMessage ("illegal.length.value" ));
1560+ MessageLocalization .getComposedMessage (ILLEGAL_LENGTH_VALUE ));
15571561 }
15581562 cryptoMode = PdfWriter .STANDARD_ENCRYPTION_128 ;
15591563 recipients = (PdfArray ) enc .get (PdfName .RECIPIENTS );
@@ -1582,7 +1586,7 @@ private void readDecryptedDocObj() throws IOException {
15821586 .getComposedMessage ("no.compatible.encryption.found" ));
15831587 }
15841588 PdfObject em = dic .get (PdfName .ENCRYPTMETADATA );
1585- if (em != null && em .toString ().equals ("false" )) {
1589+ if (em != null && em .toString ().equals (FALSE )) {
15861590 cryptoMode |= PdfWriter .DO_NOT_ENCRYPT_METADATA ;
15871591 }
15881592
@@ -1634,7 +1638,7 @@ private void readDecryptedDocObj() throws IOException {
16341638 if (!equalsArray (uValue , decrypt .userKey ,
16351639 (rValue == 3 || rValue == 4 ) ? 16 : 32 )) {
16361640 throw new BadPasswordException (
1637- MessageLocalization .getComposedMessage ("bad.user.password" ));
1641+ MessageLocalization .getComposedMessage (BAD_USER_PASSWORD ));
16381642 }
16391643 } else {
16401644 ownerPasswordUsed = true ;
@@ -1684,13 +1688,13 @@ decrypt it (revision 6 and later) - ISO 32000-2 section 7.6.4.3.3 */
16841688 // analog of step c of Algorithm 2.A for user password
16851689 hashAlg2B = decrypt .hashAlg2B (password , Arrays .copyOfRange (uValue , 32 , 40 ), null );
16861690 if (!equalsArray (hashAlg2B , uValue , 32 )) {
1687- throw new BadPasswordException (MessageLocalization .getComposedMessage ("bad.user.password" ));
1691+ throw new BadPasswordException (MessageLocalization .getComposedMessage (BAD_USER_PASSWORD ));
16881692 }
16891693 // step e of Algorithm 2.A
16901694 decrypt .setupByUserPassword (documentID , password , uValue , ueValue , oValue , oeValue , pValue );
16911695 // step f of Algorithm 2.A
16921696 if (!decrypt .decryptAndCheckPerms (permsValue )) {
1693- throw new BadPasswordException (MessageLocalization .getComposedMessage ("bad.user.password" ));
1697+ throw new BadPasswordException (MessageLocalization .getComposedMessage (BAD_USER_PASSWORD ));
16941698 }
16951699 }
16961700 pValue = decrypt .permissions ;
@@ -1981,7 +1985,7 @@ private void checkPRStreamLength(PRStream stream) throws IOException {
19811985 String line = tokens .readString (20 );
19821986 if (!line .startsWith ("\n endstream" )
19831987 && !line .startsWith ("\r \n endstream" )
1984- && !line .startsWith ("\r endstream" ) && !line .startsWith ("endstream" )) {
1988+ && !line .startsWith ("\r endstream" ) && !line .startsWith (ENDSTREAM )) {
19851989 calc = true ;
19861990 }
19871991 }
@@ -1996,14 +2000,14 @@ private void checkPRStreamLength(PRStream stream) throws IOException {
19962000 if (!tokens .readLineSegment (tline )) {
19972001 break ;
19982002 }
1999- if (equalsn (tline , endstream )) {
2003+ if (equalsn (tline , endstreamBytes )) {
20002004 streamLength = pos - start ;
20012005 break ;
20022006 }
20032007 if (equalsn (tline , endobj )) {
20042008 tokens .seek (pos - 16 );
20052009 String s = tokens .readString (16 );
2006- int index = s .indexOf ("endstream" );
2010+ int index = s .indexOf (ENDSTREAM );
20072011 if (index >= 0 ) {
20082012 pos = pos - 16 + index ;
20092013 }
@@ -2523,7 +2527,7 @@ protected PdfObject readPRObject() throws IOException {
25232527 return new PdfBoolean (true );
25242528 } // else
25252529 return PdfBoolean .PDFTRUE ;
2526- } else if ("false" .equals (sv )) {
2530+ } else if (FALSE .equals (sv )) {
25272531 if (readDepth == 0 ) {
25282532 return new PdfBoolean (false );
25292533 } // else
@@ -2985,6 +2989,7 @@ public int shuffleSubsetNames() {
29852989 *
29862990 * @return the subset prefix
29872991 */
2992+ @ SuppressWarnings ("java:S2245" ) // weak random is here ok
29882993 private String createRandomSubsetPrefix () {
29892994 String s = "" ;
29902995 for (int k = 0 ; k < 6 ; ++k ) {
0 commit comments