use of org.apache.cxf.helpers.LoadingByteArrayOutputStream in project cxf by apache.
the class CachedOutputStream method writeCacheTo.
public void writeCacheTo(StringBuilder out, String charsetName, long limit) throws IOException {
flush();
if (totalLength < limit || limit == -1) {
writeCacheTo(out, charsetName);
return;
}
long count = 0;
if (inmem) {
if (currentStream instanceof LoadingByteArrayOutputStream) {
LoadingByteArrayOutputStream lout = (LoadingByteArrayOutputStream) currentStream;
out.append(IOUtils.newStringFromBytes(lout.getRawBytes(), charsetName, 0, (int) limit));
} else if (currentStream instanceof ByteArrayOutputStream) {
byte[] bytes = ((ByteArrayOutputStream) currentStream).toByteArray();
out.append(IOUtils.newStringFromBytes(bytes, charsetName, 0, (int) limit));
} else {
throw new IOException("Unknown format of currentStream");
}
} else {
// read the file
try (InputStream fin = createInputStream(tempFile);
Reader reader = new InputStreamReader(fin, charsetName)) {
char[] bytes = new char[1024];
long x = reader.read(bytes);
while (x != -1) {
if ((count + x) > limit) {
x = limit - count;
}
out.append(bytes, 0, (int) x);
count += x;
if (count >= limit) {
x = -1;
} else {
x = reader.read(bytes);
}
}
}
}
}
use of org.apache.cxf.helpers.LoadingByteArrayOutputStream in project cxf by apache.
the class CachedOutputStream method writeCacheTo.
public void writeCacheTo(StringBuilder out, String charsetName) throws IOException {
flush();
if (inmem) {
if (currentStream instanceof LoadingByteArrayOutputStream) {
LoadingByteArrayOutputStream lout = (LoadingByteArrayOutputStream) currentStream;
out.append(IOUtils.newStringFromBytes(lout.getRawBytes(), charsetName, 0, lout.size()));
} else if (currentStream instanceof ByteArrayOutputStream) {
byte[] bytes = ((ByteArrayOutputStream) currentStream).toByteArray();
out.append(IOUtils.newStringFromBytes(bytes, charsetName));
} else {
throw new IOException("Unknown format of currentStream");
}
} else {
// read the file
try (InputStream fin = createInputStream(tempFile);
Reader reader = new InputStreamReader(fin, charsetName)) {
char[] bytes = new char[1024];
int x = reader.read(bytes);
while (x != -1) {
out.append(bytes, 0, x);
x = reader.read(bytes);
}
}
}
}
use of org.apache.cxf.helpers.LoadingByteArrayOutputStream in project cxf by apache.
the class URIResolver method tryRemote.
private void tryRemote(String uriStr) throws IOException {
try {
LoadingByteArrayOutputStream bout = cache.get(uriStr);
url = new URL(uriStr);
uri = new URI(url.toString());
if (bout == null) {
URLConnection connection = url.openConnection();
is = connection.getInputStream();
bout = new LoadingByteArrayOutputStream(1024);
IOUtils.copy(is, bout);
is.close();
cache.put(uriStr, bout);
}
is = bout.createInputStream();
} catch (MalformedURLException e) {
// do nothing
} catch (URISyntaxException e) {
// do nothing
}
}
use of org.apache.cxf.helpers.LoadingByteArrayOutputStream in project cxf by apache.
the class SwAOutInterceptor method processAttachments.
protected void processAttachments(SoapMessage message, SoapBodyInfo sbi) {
Collection<Attachment> atts = setupAttachmentOutput(message);
List<Object> outObjects = CastUtils.cast(message.getContent(List.class));
for (MessagePartInfo mpi : sbi.getAttachments()) {
String partName = mpi.getConcreteName().getLocalPart();
String ct = (String) mpi.getProperty(Message.CONTENT_TYPE);
String id = new StringBuilder().append(partName).append("=").append(UUID.randomUUID()).append("@apache.org").toString();
// this assumes things are in order...
int idx = mpi.getIndex();
Object o = outObjects.get(idx);
if (o == null) {
continue;
}
outObjects.set(idx, null);
DataHandler dh = null;
// This code could probably be refactored out somewhere...
if (o instanceof Source) {
dh = new DataHandler(createDataSource((Source) o, ct));
} else if (o instanceof Image) {
final Image img = (Image) o;
final String contentType = ct;
dh = new DataHandler(o, ct) {
@Override
public InputStream getInputStream() throws IOException {
LoadingByteArrayOutputStream bout = new LoadingByteArrayOutputStream();
writeTo(bout);
return bout.createInputStream();
}
@Override
public void writeTo(OutputStream out) throws IOException {
ImageWriter writer = null;
Iterator<ImageWriter> writers = ImageIO.getImageWritersByMIMEType(contentType);
if (writers.hasNext()) {
writer = writers.next();
}
if (writer != null) {
BufferedImage bimg = convertToBufferedImage(img);
ImageOutputStream iout = ImageIO.createImageOutputStream(out);
writer.setOutput(iout);
writer.write(bimg);
writer.dispose();
iout.flush();
out.flush();
}
}
};
} else if (o instanceof DataHandler) {
dh = (DataHandler) o;
ct = dh.getContentType();
try {
if ("text/xml".equals(ct) && dh.getContent() instanceof Source) {
dh = new DataHandler(createDataSource((Source) dh.getContent(), ct));
}
} catch (IOException e) {
// ignore, use same dh
}
} else if (o instanceof byte[]) {
if (ct == null) {
ct = "application/octet-stream";
}
dh = new DataHandler(new ByteDataSource((byte[]) o, ct));
} else if (o instanceof String) {
if (ct == null) {
ct = "text/plain; charset=\'UTF-8\'";
}
dh = new DataHandler(new ByteDataSource(((String) o).getBytes(StandardCharsets.UTF_8), ct));
} else {
throw new Fault(new org.apache.cxf.common.i18n.Message("ATTACHMENT_NOT_SUPPORTED", LOG, o.getClass()));
}
AttachmentImpl att = new AttachmentImpl(id);
att.setDataHandler(dh);
att.setHeader("Content-Type", ct);
att.setHeader("Content-ID", "<" + id + ">");
atts.add(att);
}
}
use of org.apache.cxf.helpers.LoadingByteArrayOutputStream in project cxf by apache.
the class StaxSerializer method createStreamContext.
InputStream createStreamContext(byte[] source, Node ctx) throws XMLEncryptionException {
Vector<InputStream> v = new Vector<>(2);
LoadingByteArrayOutputStream byteArrayOutputStream = new LoadingByteArrayOutputStream();
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(byteArrayOutputStream, "UTF-8");
outputStreamWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><dummy");
// Run through each node up to the document node and find any xmlns: nodes
Map<String, String> storedNamespaces = new HashMap<>();
Node wk = ctx;
while (wk != null) {
NamedNodeMap atts = wk.getAttributes();
if (atts != null) {
for (int i = 0; i < atts.getLength(); ++i) {
Node att = atts.item(i);
String nodeName = att.getNodeName();
if (("xmlns".equals(nodeName) || nodeName.startsWith("xmlns:")) && !storedNamespaces.containsKey(att.getNodeName())) {
outputStreamWriter.write(" ");
outputStreamWriter.write(nodeName);
outputStreamWriter.write("=\"");
outputStreamWriter.write(att.getNodeValue());
outputStreamWriter.write("\"");
storedNamespaces.put(nodeName, att.getNodeValue());
}
}
}
wk = wk.getParentNode();
}
outputStreamWriter.write(">");
outputStreamWriter.close();
v.add(byteArrayOutputStream.createInputStream());
v.addElement(new ByteArrayInputStream(source));
byteArrayOutputStream = new LoadingByteArrayOutputStream();
outputStreamWriter = new OutputStreamWriter(byteArrayOutputStream, "UTF-8");
outputStreamWriter.write("</dummy>");
outputStreamWriter.close();
v.add(byteArrayOutputStream.createInputStream());
} catch (UnsupportedEncodingException e) {
throw new XMLEncryptionException(e);
} catch (IOException e) {
throw new XMLEncryptionException(e);
}
return new SequenceInputStream(v.elements());
}
Aggregations