if(cipher == null){
initCipher();
}
//Base64OutputStream bos = new Base64OutputStream(outputStream);
//TODO :: Wrap outputstream with base64 encoder
CipherOutputStream cos = new CipherOutputStream(outputStream,cipher);
//BufferedStreamWriter bsw = new BufferedStreamWriter(cos);
byte [] iv = cipher.getIV();
outputStream.write(iv);
outputStream.flush();
if(data instanceof JAXBData){
((JAXBData)data).writeTo(cos);// write in chucks
}else if(data instanceof StreamWriterData){
StAXC14nCanonicalizerImpl exc14n = new StAXEXC14nCanonicalizerImpl();
//((StAXEXC14nCanonicalizerImpl)exc14n).setInclusivePrefixList(new ArrayList());
NamespaceContextEx nsEx = ((StreamWriterData)data).getNamespaceContext();
Iterator<Binding> iter = nsEx.iterator();
while(iter.hasNext()){
Binding binding = iter.next();
exc14n.writeNamespace(binding.getPrefix(),binding.getNamespaceURI());
}
if(logger.isLoggable(Level.FINEST)){
exc14n.setStream(new ByteArrayOutputStream());
}else{
exc14n.setStream(cos);
}
try {
((StreamWriterData)data).write(exc14n);
if(logger.isLoggable(Level.FINEST)){
byte [] cd=((ByteArrayOutputStream)exc14n.getOutputStream()).toByteArray();
logger.log(Level.FINEST, LogStringsMessages.WSS_1951_ENCRYPTED_DATA_VALUE(new String(cd)));
cos.write(cd);
}
} catch (javax.xml.stream.XMLStreamException ex) {
logger.log(Level.SEVERE, LogStringsMessages.WSS_1908_ERROR_WRITING_ENCRYPTEDDATA(),ex);
}
}
cos.flush();
cos.close();
} catch (NoSuchAlgorithmException ex) {
logger.log(Level.SEVERE, LogStringsMessages.WSS_1909_UNSUPPORTED_DATAENCRYPTION_ALGORITHM(getAlgorithm()), ex);
throw new XWSSecurityRuntimeException("Unable to compute CipherValue as "+getAlgorithm()+" is not supported", ex);
} catch (javax.crypto.NoSuchPaddingException ex) {
logger.log(Level.SEVERE, LogStringsMessages.WSS_1905_ERROR_INITIALIZING_CIPHER(), ex);