You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by co...@apache.org on 2016/10/17 09:40:26 UTC
svn commit: r1765231 - in
/santuario/xml-security-java/trunk/src/main/java/org/apache:
jcp/xml/dsig/internal/dom/ xml/security/encryption/ xml/security/signature/
xml/security/transforms/implementations/ xml/security/utils/
xml/security/utils/resolver/...
Author: coheigea
Date: Mon Oct 17 09:40:25 2016
New Revision: 1765231
URL: http://svn.apache.org/viewvc?rev=1765231&view=rev
Log:
More try-with-resources
Modified:
santuario/xml-security-java/trunk/src/main/java/org/apache/jcp/xml/dsig/internal/dom/Utils.java
santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/encryption/AbstractSerializer.java
santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/encryption/XMLCipher.java
santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/signature/XMLSignatureInput.java
santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformXSLT.java
santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/JavaUtils.java
santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/resolver/implementations/ResolverDirectHTTP.java
Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/jcp/xml/dsig/internal/dom/Utils.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/jcp/xml/dsig/internal/dom/Utils.java?rev=1765231&r1=1765230&r2=1765231&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/jcp/xml/dsig/internal/dom/Utils.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/jcp/xml/dsig/internal/dom/Utils.java Mon Oct 17 09:40:25 2016
@@ -44,19 +44,20 @@ public final class Utils {
public static byte[] readBytesFromStream(InputStream is)
throws IOException
{
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- byte[] buf = new byte[1024];
- while (true) {
- int read = is.read(buf);
- if (read == -1) { // EOF
- break;
- }
- baos.write(buf, 0, read);
- if (read < 1024) {
- break;
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+ byte[] buf = new byte[1024];
+ while (true) {
+ int read = is.read(buf);
+ if (read == -1) { // EOF
+ break;
+ }
+ baos.write(buf, 0, read);
+ if (read < 1024) {
+ break;
+ }
}
+ return baos.toByteArray();
}
- return baos.toByteArray();
}
/**
Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/encryption/AbstractSerializer.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/encryption/AbstractSerializer.java?rev=1765231&r1=1765230&r2=1765231&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/encryption/AbstractSerializer.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/encryption/AbstractSerializer.java Mon Oct 17 09:40:25 2016
@@ -96,16 +96,17 @@ public abstract class AbstractSerializer
* @throws Exception
*/
public String serialize(NodeList content) throws Exception {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- canon.setSecureValidation(secureValidation);
- canon.setWriter(baos);
- canon.notReset();
- for (int i = 0; i < content.getLength(); i++) {
- canon.canonicalizeSubtree(content.item(i));
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+ canon.setSecureValidation(secureValidation);
+ canon.setWriter(baos);
+ canon.notReset();
+ for (int i = 0; i < content.getLength(); i++) {
+ canon.canonicalizeSubtree(content.item(i));
+ }
+ String ret = baos.toString("UTF-8");
+ baos.reset();
+ return ret;
}
- String ret = baos.toString("UTF-8");
- baos.reset();
- return ret;
}
/**
@@ -118,14 +119,15 @@ public abstract class AbstractSerializer
* @throws Exception
*/
public byte[] serializeToByteArray(NodeList content) throws Exception {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- canon.setSecureValidation(secureValidation);
- canon.setWriter(baos);
- canon.notReset();
- for (int i = 0; i < content.getLength(); i++) {
- canon.canonicalizeSubtree(content.item(i));
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+ canon.setSecureValidation(secureValidation);
+ canon.setWriter(baos);
+ canon.notReset();
+ for (int i = 0; i < content.getLength(); i++) {
+ canon.canonicalizeSubtree(content.item(i));
+ }
+ return baos.toByteArray();
}
- return baos.toByteArray();
}
/**
@@ -135,14 +137,15 @@ public abstract class AbstractSerializer
* @throws Exception
*/
public String canonSerialize(Node node) throws Exception {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- canon.setSecureValidation(secureValidation);
- canon.setWriter(baos);
- canon.notReset();
- canon.canonicalizeSubtree(node);
- String ret = baos.toString("UTF-8");
- baos.reset();
- return ret;
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+ canon.setSecureValidation(secureValidation);
+ canon.setWriter(baos);
+ canon.notReset();
+ canon.canonicalizeSubtree(node);
+ String ret = baos.toString("UTF-8");
+ baos.reset();
+ return ret;
+ }
}
/**
@@ -152,12 +155,13 @@ public abstract class AbstractSerializer
* @throws Exception
*/
public byte[] canonSerializeToByteArray(Node node) throws Exception {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- canon.setSecureValidation(secureValidation);
- canon.setWriter(baos);
- canon.notReset();
- canon.canonicalizeSubtree(node);
- return baos.toByteArray();
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+ canon.setSecureValidation(secureValidation);
+ canon.setWriter(baos);
+ canon.notReset();
+ canon.canonicalizeSubtree(node);
+ return baos.toByteArray();
+ }
}
/**
@@ -178,8 +182,7 @@ public abstract class AbstractSerializer
protected static byte[] createContext(byte[] source, Node ctx) throws XMLEncryptionException {
// Create the context to parse the document against
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- try {
+ try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(byteArrayOutputStream, "UTF-8");
outputStreamWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><dummy");
Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/encryption/XMLCipher.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/encryption/XMLCipher.java?rev=1765231&r1=1765230&r2=1765231&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/encryption/XMLCipher.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/encryption/XMLCipher.java Mon Oct 17 09:40:25 2016
@@ -1187,13 +1187,14 @@ public class XMLCipher {
if (serializedData != null) {
int numBytes;
byte[] buf = new byte[8192];
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- while ((numBytes = serializedData.read(buf)) != -1) {
- byte[] data = c.update(buf, 0, numBytes);
- baos.write(data);
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+ while ((numBytes = serializedData.read(buf)) != -1) {
+ byte[] data = c.update(buf, 0, numBytes);
+ baos.write(data);
+ }
+ baos.write(c.doFinal());
+ encryptedBytes = baos.toByteArray();
}
- baos.write(c.doFinal());
- encryptedBytes = baos.toByteArray();
} else {
encryptedBytes = c.doFinal(serializedOctets);
if (log.isDebugEnabled()) {
Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/signature/XMLSignatureInput.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/signature/XMLSignatureInput.java?rev=1765231&r1=1765230&r2=1765231&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/signature/XMLSignatureInput.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/signature/XMLSignatureInput.java Mon Oct 17 09:40:25 2016
@@ -578,14 +578,16 @@ public class XMLSignatureInput {
Document doc = db.parse(this.getOctetStream());
this.subNode = doc;
} catch (SAXException ex) {
+ byte[] result = null;
// if a not-wellformed nodeset exists, put a container around it...
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
- baos.write("<container>".getBytes("UTF-8"));
- baos.write(this.getBytes());
- baos.write("</container>".getBytes("UTF-8"));
+ baos.write("<container>".getBytes("UTF-8"));
+ baos.write(this.getBytes());
+ baos.write("</container>".getBytes("UTF-8"));
- byte result[] = baos.toByteArray();
+ result = baos.toByteArray();
+ }
try (InputStream is = new ByteArrayInputStream(result)) {
Document document = db.parse(is);
this.subNode = document.getDocumentElement().getFirstChild().getFirstChild();
Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformXSLT.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformXSLT.java?rev=1765231&r1=1765230&r2=1765231&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformXSLT.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformXSLT.java Mon Oct 17 09:40:25 2016
@@ -109,15 +109,16 @@ public class TransformXSLT extends Trans
* so we convert the stylesheet to byte[] and use this as input stream
*/
{
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- Transformer transformer = tFactory.newTransformer();
- DOMSource source = new DOMSource(xsltElement);
- StreamResult result = new StreamResult(os);
-
- transformer.transform(source, result);
-
- stylesheet =
- new StreamSource(new ByteArrayInputStream(os.toByteArray()));
+ try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
+ Transformer transformer = tFactory.newTransformer();
+ DOMSource source = new DOMSource(xsltElement);
+ StreamResult result = new StreamResult(os);
+
+ transformer.transform(source, result);
+
+ stylesheet =
+ new StreamSource(new ByteArrayInputStream(os.toByteArray()));
+ }
}
Transformer transformer = tFactory.newTransformer(stylesheet);
@@ -136,12 +137,13 @@ public class TransformXSLT extends Trans
try (InputStream is = new ByteArrayInputStream(input.getBytes())) {
Source xmlSource = new StreamSource(is);
if (baos == null) {
- ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
- StreamResult outputTarget = new StreamResult(baos1);
- transformer.transform(xmlSource, outputTarget);
- XMLSignatureInput output = new XMLSignatureInput(baos1.toByteArray());
- output.setSecureValidation(secureValidation);
- return output;
+ try (ByteArrayOutputStream baos1 = new ByteArrayOutputStream()) {
+ StreamResult outputTarget = new StreamResult(baos1);
+ transformer.transform(xmlSource, outputTarget);
+ XMLSignatureInput output = new XMLSignatureInput(baos1.toByteArray());
+ output.setSecureValidation(secureValidation);
+ return output;
+ }
}
StreamResult outputTarget = new StreamResult(baos);
Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/JavaUtils.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/JavaUtils.java?rev=1765231&r1=1765230&r2=1765231&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/JavaUtils.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/JavaUtils.java Mon Oct 17 09:40:25 2016
@@ -56,11 +56,8 @@ public final class JavaUtils {
byte refBytes[] = null;
- FileInputStream fisRef = null;
- UnsyncByteArrayOutputStream baos = null;
- try {
- fisRef = new FileInputStream(fileName);
- baos = new UnsyncByteArrayOutputStream();
+ try (FileInputStream fisRef = new FileInputStream(fileName);
+ UnsyncByteArrayOutputStream baos = new UnsyncByteArrayOutputStream()) {
byte buf[] = new byte[1024];
int len;
@@ -69,13 +66,6 @@ public final class JavaUtils {
}
refBytes = baos.toByteArray();
- } finally {
- if (baos != null) {
- baos.close();
- }
- if (fisRef != null) {
- fisRef.close();
- }
}
return refBytes;
@@ -88,29 +78,18 @@ public final class JavaUtils {
* @param bytes
*/
public static void writeBytesToFilename(String filename, byte[] bytes) {
- FileOutputStream fos = null;
- try {
- if (filename != null && bytes != null) {
- File f = new File(filename);
-
- fos = new FileOutputStream(f);
-
+ if (filename != null && bytes != null) {
+ File f = new File(filename);
+ try (FileOutputStream fos = new FileOutputStream(f)) {
fos.write(bytes);
- fos.close();
- } else {
+ } catch (IOException ex) {
if (log.isDebugEnabled()) {
- log.debug("writeBytesToFilename got null byte[] pointed");
+ log.debug(ex.getMessage(), ex);
}
}
- } catch (IOException ex) {
- if (fos != null) {
- try {
- fos.close();
- } catch (IOException ioe) {
- if (log.isDebugEnabled()) {
- log.debug(ioe.getMessage(), ioe);
- }
- }
+ } else {
+ if (log.isDebugEnabled()) {
+ log.debug("writeBytesToFilename got null byte[] pointed");
}
}
}
@@ -126,16 +105,13 @@ public final class JavaUtils {
* @throws IOException
*/
public static byte[] getBytesFromStream(InputStream inputStream) throws IOException {
- UnsyncByteArrayOutputStream baos = new UnsyncByteArrayOutputStream();
- try {
+ try (UnsyncByteArrayOutputStream baos = new UnsyncByteArrayOutputStream()) {
byte buf[] = new byte[4 * 1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
baos.write(buf, 0, len);
}
return baos.toByteArray();
- } finally {
- baos.close();
}
}
Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/resolver/implementations/ResolverDirectHTTP.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/resolver/implementations/ResolverDirectHTTP.java?rev=1765231&r1=1765230&r2=1765231&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/resolver/implementations/ResolverDirectHTTP.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/resolver/implementations/ResolverDirectHTTP.java Mon Oct 17 09:40:25 2016
@@ -129,27 +129,29 @@ public class ResolverDirectHTTP extends
String mimeType = urlConnection.getHeaderField("Content-Type");
inputStream = urlConnection.getInputStream();
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- byte buf[] = new byte[4096];
- int read = 0;
- int summarized = 0;
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+ byte[] buf = new byte[4096];
+ int read = 0;
+ int summarized = 0;
+
+ while ((read = inputStream.read(buf)) >= 0) {
+ baos.write(buf, 0, read);
+ summarized += read;
+ }
+
+ if (log.isDebugEnabled()) {
+ log.debug("Fetched " + summarized + " bytes from URI " + uriNew.toString());
+ }
+
+ XMLSignatureInput result = new XMLSignatureInput(baos.toByteArray());
+ result.setSecureValidation(context.secureValidation);
- while ((read = inputStream.read(buf)) >= 0) {
- baos.write(buf, 0, read);
- summarized += read;
- }
+ result.setSourceURI(uriNew.toString());
+ result.setMIMEType(mimeType);
- if (log.isDebugEnabled()) {
- log.debug("Fetched " + summarized + " bytes from URI " + uriNew.toString());
+ return result;
}
- XMLSignatureInput result = new XMLSignatureInput(baos.toByteArray());
- result.setSecureValidation(context.secureValidation);
-
- result.setSourceURI(uriNew.toString());
- result.setMIMEType(mimeType);
-
- return result;
} catch (URISyntaxException ex) {
throw new ResourceResolverException(ex, context.uriToResolve, context.baseUri, "generic.EmptyMessage");
} catch (MalformedURLException ex) {