private static void delayedCopy( StreamingMessage message, final MutableBufferedMessage copy, final int max, final DelayedCopyObserver observer) { if (observer == null) throw new NullPointerException("Observer may not be null"); copy.setHeader(message.getHeader()); InputStream content = message.getContent(); if (content == null) { observer.copyCompleted(false, 0); } else { final ByteArrayOutputStream copyContent = new SizeLimitedByteArrayOutputStream(max) { @Override protected void overflow() { // do not throw an exception } }; content = new CopyInputStream(content, copyContent); content = new CountingInputStream(content) { protected void eof() { copy.setContent(copyContent.toByteArray()); observer.copyCompleted(getCount() > max, getCount()); } }; message.setContent(content); } }
private static void stream(BufferedMessage message, StreamingMessage stream) { stream.setHeader(message.getHeader()); byte[] content = message.getContent(); if (content != null && content.length > 0) stream.setContent(new ByteArrayInputStream(content)); }