Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernization: Replacing Paths.get with Path.of #438

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/org/apache/xml/security/utils/JavaUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.lang.System.Logger.Level;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.security.SecurityPermission;

/**
Expand Down Expand Up @@ -56,7 +56,7 @@ private JavaUtils() {
@Deprecated(forRemoval = true, since = "4.0.0")
public static byte[] getBytesFromFile(String filePath)
throws FileNotFoundException, IOException {
try (InputStream inputStream = Files.newInputStream(Paths.get(filePath));
try (InputStream inputStream = Files.newInputStream(Path.of(filePath));
UnsyncByteArrayOutputStream baos = new UnsyncByteArrayOutputStream()) {
byte[] buf = new byte[8_192];
int len;
Expand All @@ -77,7 +77,7 @@ public static byte[] getBytesFromFile(String filePath)
*/
public static void writeBytesToFilename(String filename, byte[] bytes) {
if (filename != null && bytes != null) {
try (OutputStream outputStream = Files.newOutputStream(Paths.get(filename))) {
try (OutputStream outputStream = Files.newOutputStream(Path.of(filename))) {
outputStream.write(bytes);
} catch (IOException ex) {
LOG.log(Level.DEBUG, ex.getMessage(), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ResolverAnonymous extends ResourceResolverSpi {
* @throws IOException
*/
public ResolverAnonymous(String filename) throws IOException {
this(Paths.get(filename));
this(Path.of(filename));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.lang.System.Logger.Level;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.nio.file.Path;

import org.apache.xml.security.signature.XMLSignatureInput;
import org.apache.xml.security.signature.XMLSignatureFileInput;
Expand All @@ -46,7 +46,7 @@ public XMLSignatureInput engineResolveURI(ResourceResolverContext context)
try {
// calculate new URI
URI uriNew = getNewURI(context.uriToResolve, context.baseUri);
XMLSignatureInput result = new XMLSignatureFileInput(Paths.get(uriNew));
XMLSignatureInput result = new XMLSignatureFileInput(Path.of(uriNew));
result.setSecureValidation(context.secureValidation);
result.setSourceURI(uriNew.toString());
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.lang.reflect.Constructor;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.security.Key;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -208,7 +208,7 @@ void testAES128ElementEcdhEsKWCipher(KeyUtils.KeyType keyType) throws Exception

ed = cipherEncData.doFinal(d, e);

Files.write(Paths.get("target","test-enc-"+keyType.name()+".xml"), toString(ed).getBytes());
Files.write(Path.of("target","test-enc-"+keyType.name()+".xml"), toString(ed).getBytes());

//decrypt
ee = (Element) ed.getElementsByTagName("xenc:EncryptedData").item(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.lang.System.Logger.Level;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.security.*;
import java.security.spec.AlgorithmParameterSpec;

Expand Down Expand Up @@ -377,7 +377,7 @@ void testAES128ElementEcdhEsKWCipher(KeyUtils.KeyType keyType) throws Exception

ed = cipherEncData.doFinal(d, e);

Files.write(Paths.get("target","test-enc-"+keyType.name()+".xml"), toString(ed).getBytes());
Files.write(Path.of("target","test-enc-"+keyType.name()+".xml"), toString(ed).getBytes());

//decrypt
ee = (Element) ed.getElementsByTagName("xenc:EncryptedData").item(0);
Expand Down Expand Up @@ -478,7 +478,7 @@ void testAES128ElementEcdhEsKWCipherHKDF(KeyUtils.KeyType keyType) throws Except

org.apache.xml.security.test.javax.xml.crypto.dsig.TestUtils.validateSecurityOrEncryptionElement(ed.getDocumentElement());

Files.write(Paths.get("target", "test-ka-dh-hkdf-" + keyType.name() + ".xml"), toString(ed).getBytes());
Files.write(Path.of("target", "test-ka-dh-hkdf-" + keyType.name() + ".xml"), toString(ed).getBytes());

//decrypt
ee = (Element) ed.getElementsByTagName("xenc:EncryptedData").item(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import java.io.ByteArrayInputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.security.Key;
import java.security.KeyStore;
import java.security.PrivateKey;
Expand Down Expand Up @@ -145,7 +145,7 @@ void testAgreementKeyEncryptDecryptDataWithBrainpool(String w3cTag,
);

if (LOG.isLoggable(System.Logger.Level.DEBUG)) {
Files.write(Paths.get("target","test-enc-"+w3cTag+".xml"), toString(doc.getFirstChild()).getBytes());
Files.write(Path.of("target","test-enc-"+w3cTag+".xml"), toString(doc.getFirstChild()).getBytes());
XMLUtils.outputDOM(doc.getFirstChild(), System.out);
}
// Perform decryption
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.io.*;
import java.lang.System.Logger.Level;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.security.Key;
import java.security.KeyStore;
import java.security.PrivateKey;
Expand Down Expand Up @@ -522,7 +522,7 @@ void testAgreementKeyEncryptDecryptDocument(String w3cTag,
encryptionAlgorithm
);

Files.write(Paths.get("target","test-enc-"+w3cTag+".xml"), toString(doc.getFirstChild()).getBytes());
Files.write(Path.of("target","test-enc-"+w3cTag+".xml"), toString(doc.getFirstChild()).getBytes());
// XMLUtils.outputDOM(doc.getFirstChild(), System.out);

// Perform decryption
Expand Down Expand Up @@ -627,7 +627,7 @@ void testAgreementKeyEncryptDecryptData(String w3cTag,
new ByteArrayInputStream(testData)
);

Files.write(Paths.get("target","test-enc-"+w3cTag+".xml"), toString(doc.getFirstChild()).getBytes());
Files.write(Path.of("target","test-enc-"+w3cTag+".xml"), toString(doc.getFirstChild()).getBytes());
// Perform decryption
byte[] result = decryptData(doc, ecKey, (X509Certificate)cert);
// XMLUtils.outputDOM(dd.getFirstChild(), System.out);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
Expand Down Expand Up @@ -63,7 +63,7 @@ class EDDSASignatureTest extends EdDSATestAbstract {
void testEd22519() throws Exception {
Assumptions.assumeTrue(isEdDSASupported());
KeyStore keyStore = KeyStore.getInstance(EDDSA_KS_TYPE);
keyStore.load(Files.newInputStream(Paths.get(EDDSA_KS)), EDDSA_KS_PASSWORD.toCharArray());
keyStore.load(Files.newInputStream(Path.of(EDDSA_KS)), EDDSA_KS_PASSWORD.toCharArray());

PrivateKey privateKey =
(PrivateKey) keyStore.getKey("Ed25519", EDDSA_KS_PASSWORD.toCharArray());
Expand Down Expand Up @@ -93,7 +93,7 @@ void testEd448VerifyXML() throws Exception {
void testEd448() throws Exception {
Assumptions.assumeTrue(isEdDSASupported());
KeyStore keyStore = KeyStore.getInstance(EDDSA_KS_TYPE);
keyStore.load(Files.newInputStream(Paths.get(EDDSA_KS)), EDDSA_KS_PASSWORD.toCharArray());
keyStore.load(Files.newInputStream(Path.of(EDDSA_KS)), EDDSA_KS_PASSWORD.toCharArray());

PrivateKey privateKey =
(PrivateKey) keyStore.getKey("Ed448", EDDSA_KS_PASSWORD.toCharArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package org.apache.xml.security.test.javax.xml.crypto.dsig;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.security.KeyStore;
import java.security.Provider;
import java.security.Security;
Expand Down Expand Up @@ -83,7 +83,7 @@ public static void afterAll() {
@Override
KeyStore getKeyStore() throws Exception {
KeyStore keyStore = KeyStore.getInstance(EDDSA_KS_TYPE);
keyStore.load(Files.newInputStream(Paths.get(EDDSA_KS)), EDDSA_KS_PASSWORD.toCharArray());
keyStore.load(Files.newInputStream(Path.of(EDDSA_KS)), EDDSA_KS_PASSWORD.toCharArray());
return keyStore;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package org.apache.xml.security.test.javax.xml.crypto.dsig;


import java.nio.file.Paths;
import java.nio.file.Path;

import javax.xml.crypto.dsig.dom.DOMValidateContext;

Expand All @@ -43,7 +43,7 @@ class SignatureValidatorEdDSATest extends EdDSATestAbstract {
public void before() {
String base = System.getProperty("basedir", "./");
testInstance = new SignatureValidator(
Paths.get(base, "src", "test", "resources", "javax", "xml", "crypto", "dsig", "eddsa").toFile());
Path.of(base, "src", "test", "resources", "javax", "xml", "crypto", "dsig", "eddsa").toFile());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -72,7 +72,7 @@ static void removeProvider() {
@Override
KeyStore getKeyStore() throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException {
KeyStore keyStore = KeyStore.getInstance(ECDSA_KS_TYPE);
keyStore.load(Files.newInputStream(Paths.get(ECDSA_KS_PATH)), ECDSA_KS_PASSWORD.toCharArray());
keyStore.load(Files.newInputStream(Path.of(ECDSA_KS_PATH)), ECDSA_KS_PASSWORD.toCharArray());
return keyStore;
}

Expand All @@ -89,7 +89,7 @@ char[] getKeyPassword() {
void createEdDSASignatureTest(String signatureAlgorithm, String alias) throws Exception {
byte[] buff = doSignWithJcpApi(signatureAlgorithm, alias, true);
if (LOG.isLoggable(System.Logger.Level.DEBUG)) {
Files.write(Paths.get("target","test-sign-"+alias+".xml"), buff);
Files.write(Path.of("target","test-sign-"+alias+".xml"), buff);
}
Assertions.assertNotNull(buff);
assertValidSignatureWithJcpApi(buff, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -75,7 +75,7 @@ static void removeProvider() {
@Override
KeyStore getKeyStore() throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException {
KeyStore keyStore = KeyStore.getInstance(KEYSTORE_TYPE);
keyStore.load(Files.newInputStream(Paths.get(KEYSTORE_PATH)), KEYSTORE_AND_KEY_PASSWORD.toCharArray());
keyStore.load(Files.newInputStream(Path.of(KEYSTORE_PATH)), KEYSTORE_AND_KEY_PASSWORD.toCharArray());
return keyStore;
}

Expand Down Expand Up @@ -108,7 +108,7 @@ void createECDSASignatureTest(String signatureAlgorithmURI, String alias) throws

byte[] buff = doSignWithJcpApi(signatureAlgorithmURI, alias, false);
if (LOG.isLoggable(System.Logger.Level.DEBUG)) {
Files.write(Paths.get("target", "test-ecdsa-" + jceAlg + ".xml"), buff);
Files.write(Path.of("target", "test-ecdsa-" + jceAlg + ".xml"), buff);
}
Assertions.assertNotNull(buff);
assertValidSignatureWithJcpApi(buff, false);
Expand Down
Loading