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 buffer(StreamingMessage message, MutableBufferedMessage buffered, int max) throws IOException, SizeLimitExceededException { buffered.setHeader(message.getHeader()); InputStream in = message.getContent(); if (in != null) { ByteArrayOutputStream copy; copy = new SizeLimitedByteArrayOutputStream(max); byte[] b = new byte[1024]; int got; try { while ((got = in.read(b)) > -1) { copy.write(b, 0, got); } buffered.setContent(copy.toByteArray()); } catch (SizeLimitExceededException slee) { buffered.setContent(copy.toByteArray()); throw slee; } } }
/** * @param content * @param codings * @return * @throws MessageFormatException */ public static InputStream encode(StreamingMessage message) throws MessageFormatException { return encode(message, message.getContent()); }
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)); }