if (rawContent != null) {
try {
stream = rawContent.getInputStream();
} catch (Exception e) {
log.log(Level.SEVERE,"SAAJ0579.soap.attachment.getbase64content.exception", e);
throw new SOAPExceptionImpl(e.getLocalizedMessage());
}
} else if (dataHandler != null) {
try {
stream = dataHandler.getInputStream();
} catch (IOException e) {
log.severe("SAAJ0574.soap.attachment.datahandler.ioexception");
throw new SOAPExceptionImpl("DataHandler error" + e);
}
} else {
log.severe("SAAJ0572.soap.no.content.for.attachment");
throw new SOAPExceptionImpl("No data handler/content associated with this attachment");
}
//TODO: Write a BASE64EncoderInputStream instead,
// this code below is inefficient
// where we are trying to read the whole attachment first
int len;
int size = 1024;
byte [] buf;
if (stream != null) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream(size);
//TODO: try and optimize this on the same lines as
// ByteOutputStream : to eliminate the temp buffer here
OutputStream ret = MimeUtility.encode(bos, "base64");
buf = new byte[size];
while ((len = stream.read(buf, 0, size)) != -1) {
ret.write(buf, 0, len);
}
ret.flush();
buf = bos.toByteArray();
return new ByteArrayInputStream(buf);
} catch (Exception e) {
// throw new SOAPException
log.log(Level.SEVERE,"SAAJ0579.soap.attachment.getbase64content.exception", e);
throw new SOAPExceptionImpl(e.getLocalizedMessage());
}
} else {
//throw new SOAPException
log.log(Level.SEVERE,"SAAJ0572.soap.no.content.for.attachment");
throw new SOAPExceptionImpl("No data handler/content associated with this attachment");
}
}