public BlobURI write(
      final UUID messageId,
      final Mailbox mailbox,
      final String profileName,
      final InputStream in,
      final Long size)
      throws IOException, GeneralSecurityException {
    Assert.notNull(in, "No data to store");

    BlobURI blobUri;
    InputStream in1;
    Long updatedSize = size;
    boolean compressed = false;

    // compress stream and calculate compressed size
    if ((compressionHandler != null) && (size > MIN_COMPRESS_SIZE)) {
      InputStream compressedInputStream = compressionHandler.compress(in);
      FileBackedOutputStream fbout = new FileBackedOutputStream(MAX_MEMORY_FILE_SIZE, true);
      updatedSize = ByteStreams.copy(compressedInputStream, fbout);
      in1 = fbout.getSupplier().getInput();
      compressed = true;
    } else {
      in1 = in;
    }

    if (updatedSize <= Configurator.getDatabaseBlobMaxSize()) {
      logger.debug(
          "Storing Blob in the database because size ({}KB) was less than database threshold {}KB",
          updatedSize,
          Configurator.getDatabaseBlobMaxSize());
      blobUri = dbBlobStorage.write(messageId, mailbox, null, in1, updatedSize);
    } else {
      logger.debug(
          "Storing Blob in the cloud because size ({}KB) was greater than database threshold {}KB",
          updatedSize,
          Configurator.getDatabaseBlobMaxSize());
      blobUri =
          cloudBlobStorage.write(
              messageId, mailbox, Configurator.getBlobStoreWriteProfileName(), in1, updatedSize);
    }

    // add compression information to the blob URI
    if (compressed) {
      blobUri.setCompression(compressionHandler.getType());
    }

    return blobUri;
  }
Esempio n. 2
0
  public void write(String text, boolean fromSource) {
    if (LOG.isLoggable(Level.FINER)) {
      LOG.finer(
          "Inserting Text: '"
              + text.replaceAll("[\r\n]+", "\\\\n")
              + "' ("
              + (fromSource ? "from source" : "generated")
              + ")");
    }
    try {
      final int textLength = text.length();
      final StringBuilder inserted = new StringBuilder();
      if (fromSource) {
        final boolean preserveSpace = isSpacePreserved();
        for (int cc = 0; cc < textLength; cc++) {
          char currentChar = text.charAt(cc);
          if (!preserveSpace
              && configuration.isCompressingWhitespace()
              && Character.isWhitespace(lastChar)
              && Character.isWhitespace(currentChar)) {
            mapOffsetDelta(0, 1);
            continue;
          }
          if (currentChar == '\n' || currentChar == '\r') {
            currentChar = ' ';
          }
          textBuffer.write(Character.toString(lastChar = currentChar).getBytes());
          inserted.append(lastChar);
          mapOffsetDelta(1, 1);
        }
      } else {
        textBuffer.write(text.getBytes());
        inserted.append(text);
        mapOffsetDelta(inserted.length(), 0);
      }

      final String insertedStr = inserted.toString();
      for (XMLTransformerModule<T> m : configuration.getModules()) {
        m.textWritten(this, text, insertedStr);
      }
    } catch (IOException e) {
      throw Throwables.propagate(e);
    }
  }
Esempio n. 3
0
 Reader read() throws IOException {
   textBuffer.flush();
   return new InputStreamReader(textBuffer.getSupplier().getInput());
 }