try { if (src.getInputStream() != null) { try (ByteArrayOutputStream bos = new ByteArrayOutputStream(2048)) { IOUtils.copy(src.getInputStream(), bos, 1024); ds = new ByteDataSource(bos.toByteArray(), ct); ds = new ByteDataSource(IOUtils.toString(src.getReader()).getBytes(StandardCharsets.UTF_8), ct); throw new Fault(e); ByteArrayOutputStream bwriter = new ByteArrayOutputStream(); XMLStreamWriter writer = null; try { ds = new ByteDataSource(bwriter.toByteArray(), ct); } catch (XMLStreamException e1) { throw new Fault(e1); } finally { StaxUtils.close(writer);
if (a.getId().startsWith(start)) { DataHandler dh = a.getDataHandler(); String ct = dh.getContentType(); Object o = null; Class<?> typeClass = mpi.getTypeClass(); try { o = dh.getContent(); } catch (IOException e) { throw new Fault(e); o = IOUtils.readBytesFromStream(dh.getInputStream()); } catch (IOException e) { throw new Fault(e); o = ImageIO.read(dh.getInputStream()); } catch (IOException e) { throw new Fault(e);
protected void serveStaticContent(HttpServletRequest request, HttpServletResponse response, String pathInfo) throws ServletException { InputStream is = getResourceAsStream(pathInfo); if (is == null) { throw new ServletException("Static resource " + pathInfo + " is not available"); } try { int ind = pathInfo.lastIndexOf("."); if (ind != -1 && ind < pathInfo.length()) { String type = getStaticResourceContentType(pathInfo.substring(ind + 1)); if (type != null) { response.setContentType(type); } } String cacheControl = getServletConfig().getInitParameter(STATIC_CACHE_CONTROL); if (cacheControl != null) { response.setHeader("Cache-Control", cacheControl.trim()); } ServletOutputStream os = response.getOutputStream(); IOUtils.copyAndCloseInput(is, os); os.flush(); } catch (IOException ex) { throw new ServletException("Static resource " + pathInfo + " can not be written to the output stream"); } }
public static byte[] readBytesFromStream(InputStream in) throws IOException { int i = in.available(); if (i < DEFAULT_BUFFER_SIZE) { i = DEFAULT_BUFFER_SIZE; } try (ByteArrayOutputStream bos = new ByteArrayOutputStream(i)) { copy(in, bos); return bos.toByteArray(); } finally { in.close(); } } }
@Override public byte[] getAttachmentAsByteArray(String contentId) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { InputStream is = AttachmentUtil.getAttachmentDataSource(contentId, attachments) .getInputStream(); IOUtils.copy(is, bos); is.close(); bos.close(); } catch (IOException e) { throw new Fault(new org.apache.cxf.common.i18n.Message("ATTACHMENT_READ_ERROR", LOG), e); } return bos.toByteArray(); }
public byte[] invokeBytes(String address, String transport, String message) throws Exception { EndpointInfo ei = new EndpointInfo(null, "http://schemas.xmlsoap.org/soap/http"); ei.setAddress(address); ConduitInitiatorManager conduitMgr = getBus().getExtension(ConduitInitiatorManager.class); ConduitInitiator conduitInit = conduitMgr.getConduitInitiator(transport); Conduit conduit = conduitInit.getConduit(ei); TestMessageObserver obs = new TestMessageObserver(); conduit.setMessageObserver(obs); Message m = new MessageImpl(); conduit.prepare(m); OutputStream os = m.getContent(OutputStream.class); InputStream is = getResourceAsStream(message); if (is == null) { throw new RuntimeException("Could not find resource " + message); } IOUtils.copy(is, os); // TODO: shouldn't have to do this. IO caching needs cleaning // up or possibly removal... os.flush(); is.close(); os.close(); byte[] bs = obs.getResponseStream().toByteArray(); return bs; } public byte[] invokeBytes(String address, String transport, byte[] message) throws Exception {
@Override protected byte[] getBytes(Object object) { DataSource dataSource = (DataSource) object; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { IOUtils.copyAndCloseInput(dataSource.getInputStream(), baos); } catch (IOException e) { throw new RuntimeException(e); } return baos.toByteArray(); } }
/** * Read the entire original input stream and cache it. Useful * if switching threads or doing something where the original * stream may not be valid by the time the next read() occurs */ public void cacheInput() { if (!cached) { CachedOutputStream cache = new CachedOutputStream(); try { InputStream origIn = in; IOUtils.copy(in, cache); if (cache.size() > 0) { in = cache.getInputStream(); } else { in = new ByteArrayInputStream(new byte[0]); } cache.close(); origIn.close(); } catch (IOException e) { //ignore } cached = true; } }
public void onMessage(Message message) { try { contentType = (String)message.get(Message.CONTENT_TYPE); InputStream is = message.getContent(InputStream.class); try { IOUtils.copy(is, response); is.close(); response.close(); } catch (IOException e) { throw new RuntimeException(e); } } finally { synchronized (this) { written = true; notifyAll(); } } } }
public static String readBody(InputStream is, String encoding) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); IOUtils.copy(is, bos, 1024); return new String(bos.toByteArray(), encoding); } catch (Exception ex) { throw ExceptionUtils.toInternalServerErrorException(ex, null); } }
/** * Copied from LoggingInInterceptor * * @param soapMessage */ private void getSoapRequest(Message soapMessage, ExchangeData exchange) { InputStream is = soapMessage.getContent(InputStream.class); if (is != null) { CachedOutputStream bos = new CachedOutputStream(); try { IOUtils.copy(is, bos); bos.flush(); is.close(); soapMessage.setContent(InputStream.class, bos.getInputStream()); StringBuilder builder = new StringBuilder(); bos.writeCacheTo(builder, bos.size()); bos.close(); exchange.setRequest(builder.toString()); exchange.setRequestSize((int)bos.size()); } catch (IOException e) { throw new Fault(e); } } }
public void echoData(Holder<String> text, Holder<DataHandler> data) { try { InputStream bis = null; bis = data.value.getDataSource().getInputStream(); byte[] b = new byte[6]; bis.read(b, 0, 6); String string = IOUtils.newStringFromBytes(b); ByteArrayDataSource source = new ByteArrayDataSource(("test" + string).getBytes(), "application/octet-stream"); data.value = new DataHandler(source); } catch (IOException e) { e.printStackTrace(); } }
public void handleMessage(Message message) throws Fault { BindingOperationInfo bop = message.getExchange().getBindingOperationInfo(); if (bop != null && !bindingName.equals(bop.getBinding().getName())) { return; } else if (DataSource.class.isAssignableFrom(type)) { deser.initializeAttachments(); } catch (IOException ex) { throw new Fault(ex); try { InputStream in = ds.getInputStream(); IOUtils.copy(in, out); in.close(); out.flush(); out.close(); } catch (IOException e) { throw new Fault(e); out = new CachedOutputStream(); ds = new DOMSource(StaxUtils.read(ds)); } catch (XMLStreamException e) { throw new Fault(e);
public byte[] getBytes() throws IOException { flush(); if (inmem) { if (currentStream instanceof ByteArrayOutputStream) { return ((ByteArrayOutputStream)currentStream).toByteArray(); } throw new IOException("Unknown format of currentStream"); } // read the file try (InputStream fin = createInputStream(tempFile)) { return IOUtils.readBytesFromStream(fin); } }
public static String getStringFromInputStream(InputStream in) throws Exception { CachedOutputStream bos = new CachedOutputStream(); IOUtils.copy(in, bos); in.close(); bos.close(); return bos.getOut().toString(); }
public void onMessage(Message message) { try { // HTTP seems to need this right now... ExchangeImpl ex = new ExchangeImpl(); ex.setInMessage(message); Conduit backChannel = message.getDestination().getBackChannel(message); MessageImpl res = new MessageImpl(); ex.setOutMessage(res); res.put(Message.CONTENT_TYPE, "text/xml"); backChannel.prepare(res); OutputStream out = res.getContent(OutputStream.class); InputStream is = resource.openStream(); IOUtils.copy(is, out, 2048); out.flush(); out.close(); is.close(); backChannel.close(res); } catch (Exception e) { e.printStackTrace(); } }
public static String toString(final InputStream input, int bufferSize, String charset) throws IOException { int avail = input.available(); if (avail > bufferSize) { bufferSize = avail; } Reader reader = charset == null ? new InputStreamReader(input, UTF8_CHARSET) : new InputStreamReader(input, charset); return toString(reader, bufferSize); }
inMessage.setExchange(new ExchangeImpl()); inMessage.getExchange().put(Bus.class, bus); inMessage.put(Message.DECOUPLED_CHANNEL_MESSAGE, Boolean.TRUE); InputStream in = inMessage.getContent(InputStream.class); if (in != null) { CachedOutputStream cos = new CachedOutputStream(); IOUtils.copy(in, cos); inMessage.setContent(InputStream.class, cos.getInputStream());
public void writeCacheTo(OutputStream out) throws IOException { flush(); if (inmem) { if (currentStream instanceof ByteArrayOutputStream) { ((ByteArrayOutputStream)currentStream).writeTo(out); } else { throw new IOException("Unknown format of currentStream"); } } else { // read the file InputStream fin = createInputStream(tempFile); IOUtils.copyAndCloseInput(fin, out); } }