class BlobDeserializer implements EventDeserializer {

  @SuppressWarnings("resource")
  @Override
  public Event readEvent() throws IOException {
    ensureOpen();
    ByteArrayOutputStream blob = null;
    byte[] buf = new byte[Math.min(maxBlobLength, DEFAULT_BUFFER_SIZE)];
    int blobLength = 0;
    int n = 0;
    while ((n = in.read(buf, 0, Math.min(buf.length, maxBlobLength - blobLength))) != -1) {
      if (blob == null) {
        blob = new ByteArrayOutputStream(n);
      }
      blob.write(buf, 0, n);
      blobLength += n;
      if (blobLength >= maxBlobLength) {
        LOGGER.warn("File length exceeds maxBlobLength ({}), truncating BLOB event!",
                    maxBlobLength);
        break;
      }
    }
    
    if (blob == null) {
      return null;
    } else {
      return EventBuilder.withBody(blob.toByteArray());
    }
  }

  protected  BlobDeserializer(Context context, ResettableInputStream in);

}