in src/main/java/org/apache/xml/security/stax/impl/transformer/TransformBase64Decode.java [71:173]
public void transform(XMLSecEvent xmlSecEvent) throws XMLStreamException {
int eventType = xmlSecEvent.getEventType();
if (XMLStreamConstants.CHARACTERS == eventType) {
if (getOutputStream() != null) {
//we have an output stream
//encoding shouldn't matter here, because the data is Base64 encoded and is therefore in the ASCII range.
try {
getOutputStream().write(xmlSecEvent.asCharacters().getData().getBytes());
} catch (IOException e) {
throw new XMLStreamException(e);
}
} else {
//we have a child transformer
if (childOutputMethod == null) {
final XMLSecurityConstants.TransformMethod preferredChildTransformMethod =
getTransformer().getPreferredTransformMethod(XMLSecurityConstants.TransformMethod.XMLSecEvent);
if (preferredChildTransformMethod == XMLSecurityConstants.TransformMethod.XMLSecEvent) {
childOutputMethod = new ChildOutputMethod() {
private UnsyncByteArrayOutputStream byteArrayOutputStream;
private Base64OutputStream base64OutputStream;
@Override
public void transform(Object object) throws XMLStreamException {
if (base64OutputStream == null) {
byteArrayOutputStream = new UnsyncByteArrayOutputStream();
base64OutputStream = new Base64OutputStream(byteArrayOutputStream, false);
}
try {
base64OutputStream.write((byte[]) object);
} catch (IOException e) {
throw new XMLStreamException(e);
}
}
@Override
public void doFinal() throws XMLStreamException {
try {
base64OutputStream.close();
} catch (IOException e) {
throw new XMLStreamException(e);
}
try (InputStream is = new UnsyncByteArrayInputStream(byteArrayOutputStream.toByteArray())) {
XMLEventReaderInputProcessor xmlEventReaderInputProcessor
= new XMLEventReaderInputProcessor(null,
getXmlInputFactory().createXMLStreamReader(is)
);
XMLSecEvent xmlSecEvent;
do {
xmlSecEvent = xmlEventReaderInputProcessor.processEvent(null);
getTransformer().transform(xmlSecEvent);
} while (xmlSecEvent.getEventType() != XMLStreamConstants.END_DOCUMENT);
} catch (XMLSecurityException | IOException e) {
throw new XMLStreamException(e);
}
getTransformer().doFinal();
}
};
} else if (preferredChildTransformMethod == XMLSecurityConstants.TransformMethod.InputStream) {
childOutputMethod = new ChildOutputMethod() {
private UnsyncByteArrayOutputStream byteArrayOutputStream;
private Base64OutputStream base64OutputStream;
@Override
public void transform(Object object) throws XMLStreamException {
if (base64OutputStream == null) {
byteArrayOutputStream = new UnsyncByteArrayOutputStream();
base64OutputStream = new Base64OutputStream(byteArrayOutputStream, false);
}
try {
base64OutputStream.write((byte[]) object);
} catch (IOException e) {
throw new XMLStreamException(e);
}
}
@Override
public void doFinal() throws XMLStreamException {
try {
base64OutputStream.close();
} catch (IOException e) {
throw new XMLStreamException(e);
}
try (InputStream is = new UnsyncByteArrayInputStream(byteArrayOutputStream.toByteArray())) {
getTransformer().transform(is);
getTransformer().doFinal();
} catch (IOException ex) {
throw new XMLStreamException(ex);
}
}
};
}
if (childOutputMethod != null) {
childOutputMethod.transform(xmlSecEvent.asCharacters().getData().getBytes());
}
}
}
}
}