Esempio n. 1
0
 /**
  * Get the exact representation of the message
  *
  * @return the internal byte[] representing the contents of this message.
  */
 public static byte[] getMessage(MutableBufferedMessage message) {
   byte[] header = message.getHeader();
   byte[] content = message.getContent();
   if (content != null && content.length > 0) {
     byte[] bytes = new byte[header.length + content.length];
     System.arraycopy(header, 0, bytes, 0, header.length);
     System.arraycopy(content, 0, bytes, header.length, content.length);
     return bytes;
   }
   return header;
 }
Esempio n. 2
0
  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);
    }
  }
Esempio n. 3
0
 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;
     }
   }
 }