Example usage for org.apache.commons.io.output DeferredFileOutputStream getFile

List of usage examples for org.apache.commons.io.output DeferredFileOutputStream getFile

Introduction

In this page you can find the example usage for org.apache.commons.io.output DeferredFileOutputStream getFile.

Prototype

public File getFile() 

Source Link

Document

Returns either the output file specified in the constructor or the temporary file created or null.

Usage

From source file:mitm.common.util.MiscIOUtils.java

/**
 * Creates an InputStream of the content of {@link DeferredFileOutputStream}. The output is closed before reading the
 * content. This is required when the {@link DeferredFileOutputStream} uses a file to make sure the file is
 * completely written to disk.//from  ww  w. ja  v  a 2  s.co m
 *
 * Note: If {@link DeferredFileOutputStream} uses a file, the file will be automatically deleted when #close is
 * called for the InputStream. The caller is responsible for calling close.
 */
@SuppressWarnings("resource")
public static InputStream toInputStream(DeferredFileOutputStream output) throws IOException {
    Check.notNull(output, "output");

    IOUtils.closeQuietly(output);

    return output.isInMemory() ? new ByteArrayInputStream(output.getData())
            : new BufferedInputStream(new DeleteFileOnCloseFileInputStream(output.getFile()));
}

From source file:mitm.common.mail.MailUtils.java

/**
 * Saves the MimePart to the output stream
 * @param part//from  w  ww .  ja va  2 s.co m
 * @param output
 * @throws IOException
 * @throws MessagingException
 */
public static void writeMessage(MimePart part, OutputStream output) throws IOException, MessagingException {
    /*
     * we need to store the result in a temporary buffer so we can fall back 
     * on a different procedure when writeTo fails. If not, MimePart#writeTo might
     * fail halfway and some data has already been written to output. 
     */
    DeferredFileOutputStream buffer = new DeferredFileOutputStream(SizeUtils.MB, FileConstants.TEMP_FILE_PREFIX,
            null, null);

    /*
     * First try the writeTo method. Sometimes this will fail when the message uses 
     * an unsupported or corrupt encoding. For example the content encoding says that
     * the message is base64 encoded but it is not.  
     */
    try {
        part.writeTo(buffer);

        /*
         * Need to close the DeferredFileOutputStream before we can write
         */
        buffer.close();

        /* 
         * writeTo was successful so we can copy the result to the final output
         */
        buffer.writeTo(output);
    } catch (IOException e) {
        writeMessageRaw(part, output);
    } catch (MessagingException e) {
        writeMessageRaw(part, output);
    } finally {
        /*
         * Delete any possible temp file used by DeferredFileOutputStream.
         */
        FileUtils.deleteQuietly(buffer.getFile());
    }
}

From source file:mitm.common.extractor.impl.TextExtractorUtils.java

/**
 * conveniance method that writes data and fires a 
 * {@link TextExtractorEventHandler#textEvent(mitm.common.extractor.ExtractedPart)} event. The Data is written
 * to disk if total bytes exceed threshold. If total bytes written exceeds maxSize, an IOException will
 * be thrown.//from ww w  .  j  av a2  s.  c  o  m
 * 
 */
public static void fireAttachmentEvent(TextExtractorEventHandler handler, TextExtractorContext context,
        TextExtractorAttachmentHandler attachmentHandler, int threshold, long maxSize) throws IOException {
    Check.notNull(handler, "handler");
    Check.notNull(attachmentHandler, "attachmentHandler");

    DeferredFileOutputStream output = new DeferredFileOutputStream(threshold, FileConstants.TEMP_FILE_PREFIX,
            null, null);

    RewindableInputStream content = null;

    try {
        /*
         * Wrap the output in a buffered stream and protect against 'zip bombs'.
         */
        SizeLimitedOutputStream buffered = new SizeLimitedOutputStream(new BufferedOutputStream(output),
                maxSize);

        try {
            attachmentHandler.write(buffered);
        } finally {
            /*
             * Must close to make sure all data is written. 
             * 
             * Note: if DeferredFileOutputStream uses a file, the file should NOT 
             * be deleted here because it will be deleted when the returned 
             * DeferredBufferedInputStream is closed.
             */
            buffered.close();
        }

        content = new RewindableInputStream(MiscIOUtils.toInputStream(output), threshold);

        handler.attachmentEvent(new ExtractedPartImpl(context, content, buffered.getByteCount()));
    } catch (IOException e) {
        /*
         * Must close the RewindableInputStream to prevent a possible temp file leak
         */
        IOUtils.closeQuietly(content);

        /*
         * Delete the tempfile (if) used by DeferredFileOutputStream.
         */
        IOUtils.closeQuietly(output);
        FileUtils.deleteQuietly(output.getFile());

        throw e;
    } catch (RuntimeException e) {
        /*
         * Must close the RewindableInputStream to prevent a possible temp file leak
         */
        IOUtils.closeQuietly(content);

        /*
         * Delete the tempfile (if) used by DeferredFileOutputStream.
         */
        IOUtils.closeQuietly(output);
        FileUtils.deleteQuietly(output.getFile());

        throw e;
    }
}

From source file:mitm.common.extractor.impl.TextExtractorUtils.java

/**
 * conveniance method that writes data and fires a 
 * {@link TextExtractorEventHandler#textEvent(mitm.common.extractor.ExtractedPart)} event. The Data is written
 * to disk if total bytes exceed threshold. If total bytes written exceeds maxSize, an IOException will
 * be thrown.//from  w w w  .  j  av a  2s.  c  o m
 * 
 */
public static void fireTextEvent(TextExtractorEventHandler handler, TextExtractorContext context,
        TextExtractorWriterHandler writerHandler, int threshold, long maxSize) throws IOException {
    Check.notNull(handler, "handler");
    Check.notNull(writerHandler, "writerHandler");

    DeferredFileOutputStream output = new DeferredFileOutputStream(threshold, FileConstants.TEMP_FILE_PREFIX,
            null, null);

    RewindableInputStream content = null;

    try {
        /*
         * Wrap the output in a buffered stream and protect against 'zip bombs'.
         */
        SizeLimitedOutputStream buffered = new SizeLimitedOutputStream(new BufferedOutputStream(output),
                maxSize);

        Writer writer = new OutputStreamWriter(buffered, TextExtractor.ENCODING);

        try {
            writerHandler.write(writer);
        } finally {
            /*
             * Must close to make sure all data is written. 
             * 
             * Note: if DeferredFileOutputStream uses a file, the file should NOT 
             * be deleted here because it will be deleted when the returned 
             * DeferredBufferedInputStream is closed.
             */
            writer.close();
        }

        content = new RewindableInputStream(MiscIOUtils.toInputStream(output), threshold);

        handler.textEvent(new ExtractedPartImpl(context, content, buffered.getByteCount()));
    } catch (IOException e) {
        /*
         * Must close the RewindableInputStream to prevent a possible temp file leak
         */
        IOUtils.closeQuietly(content);

        /*
         * Delete the tempfile (if) used by DeferredFileOutputStream.
         */
        IOUtils.closeQuietly(output);
        FileUtils.deleteQuietly(output.getFile());

        throw e;
    } catch (RuntimeException e) {
        /*
         * Must close the RewindableInputStream to prevent a possible temp file leak
         */
        IOUtils.closeQuietly(content);

        /*
         * Delete the tempfile (if) used by DeferredFileOutputStream.
         */
        IOUtils.closeQuietly(output);
        FileUtils.deleteQuietly(output.getFile());

        throw e;
    }
}

From source file:de.mendelson.comm.as2.message.AS2MessageParser.java

/**Decrypts the data of a message with all given certificates etc
 * @param info MessageInfo, the encryption algorith will be stored in the encryption type of this info
 * @param rawMessageData encrypted data, will be decrypted
 * @param contentType contentType of the data
 * @param privateKey receivers private key
 * @param certificate receivers certificate
 *///from  ww w  .  j  a va  2  s  .  c om
public byte[] decryptData(AS2Message message, byte[] data, String contentType, PrivateKey privateKeyReceiver,
        X509Certificate certificateReceiver, String receiverCryptAlias) throws Exception {
    AS2MessageInfo info = (AS2MessageInfo) message.getAS2Info();
    MimeBodyPart encryptedBody = new MimeBodyPart();
    encryptedBody.setHeader("content-type", contentType);
    encryptedBody.setDataHandler(new DataHandler(new ByteArrayDataSource(data, contentType)));
    RecipientId recipientId = new JceKeyTransRecipientId(certificateReceiver);
    SMIMEEnveloped enveloped = new SMIMEEnveloped(encryptedBody);
    BCCryptoHelper helper = new BCCryptoHelper();
    String algorithm = helper.convertOIDToAlgorithmName(enveloped.getEncryptionAlgOID());
    if (algorithm.equals(BCCryptoHelper.ALGORITHM_3DES)) {
        info.setEncryptionType(AS2Message.ENCRYPTION_3DES);
    } else if (algorithm.equals(BCCryptoHelper.ALGORITHM_DES)) {
        info.setEncryptionType(AS2Message.ENCRYPTION_DES);
    } else if (algorithm.equals(BCCryptoHelper.ALGORITHM_RC2)) {
        info.setEncryptionType(AS2Message.ENCRYPTION_RC2_UNKNOWN);
    } else if (algorithm.equals(BCCryptoHelper.ALGORITHM_AES_128)) {
        info.setEncryptionType(AS2Message.ENCRYPTION_AES_128);
    } else if (algorithm.equals(BCCryptoHelper.ALGORITHM_AES_192)) {
        info.setEncryptionType(AS2Message.ENCRYPTION_AES_192);
    } else if (algorithm.equals(BCCryptoHelper.ALGORITHM_AES_256)) {
        info.setEncryptionType(AS2Message.ENCRYPTION_AES_256);
    } else if (algorithm.equals(BCCryptoHelper.ALGORITHM_RC4)) {
        info.setEncryptionType(AS2Message.ENCRYPTION_RC4_UNKNOWN);
    } else {
        info.setEncryptionType(AS2Message.ENCRYPTION_UNKNOWN_ALGORITHM);
    }
    RecipientInformationStore recipients = enveloped.getRecipientInfos();
    enveloped = null;
    encryptedBody = null;
    RecipientInformation recipient = recipients.get(recipientId);
    if (recipient == null) {
        //give some details about the required and used cert for the decryption
        Collection recipientList = recipients.getRecipients();
        Iterator iterator = recipientList.iterator();
        while (iterator.hasNext()) {
            RecipientInformation recipientInfo = (RecipientInformation) iterator.next();
            if (this.logger != null) {
                this.logger.log(Level.SEVERE, this.rb.getResourceString("decryption.inforequired",
                        new Object[] { info.getMessageId(), recipientInfo.getRID() }), info);
            }
        }
        if (this.logger != null) {
            this.logger.log(Level.SEVERE, this.rb.getResourceString("decryption.infoassigned",
                    new Object[] { info.getMessageId(), receiverCryptAlias, recipientId }), info);
        }
        throw new AS2Exception(AS2Exception.AUTHENTIFICATION_ERROR,
                "Error decrypting the message: Recipient certificate does not match.", message);
    }
    //Streamed decryption. Its also possible to use in memory decryption using getContent but that uses
    //far more memory.
    InputStream contentStream = recipient
            .getContentStream(new JceKeyTransEnvelopedRecipient(privateKeyReceiver).setProvider("BC"))
            .getContentStream();
    //InputStream contentStream = recipient.getContentStream(privateKeyReceiver, "BC").getContentStream();
    //threshold set to 20 MB: if the data is less then 20MB perform the operaion in memory else stream to disk
    DeferredFileOutputStream decryptedOutput = new DeferredFileOutputStream(20 * 1024 * 1024, "as2decryptdata_",
            ".mem", null);
    this.copyStreams(contentStream, decryptedOutput);
    decryptedOutput.flush();
    decryptedOutput.close();
    contentStream.close();
    byte[] decryptedData = null;
    //size of the data was < than the threshold
    if (decryptedOutput.isInMemory()) {
        decryptedData = decryptedOutput.getData();
    } else {
        //data has been written to a temp file: reread and return
        ByteArrayOutputStream memOut = new ByteArrayOutputStream();
        decryptedOutput.writeTo(memOut);
        memOut.flush();
        memOut.close();
        //finally delete the temp file
        boolean deleted = decryptedOutput.getFile().delete();
        decryptedData = memOut.toByteArray();
    }
    if (this.logger != null) {
        this.logger.log(Level.INFO,
                this.rb.getResourceString("decryption.done.alias",
                        new Object[] { info.getMessageId(), receiverCryptAlias,
                                this.rbMessage.getResourceString("encryption." + info.getEncryptionType()) }),
                info);
    }
    return (decryptedData);
}

From source file:hudson.scm.CvsTagsParamDefinition.java

@Exported
public ListBoxModel getSymbolicNames() {
    ListBoxModel model = new ListBoxModel();
    CvsChangeSet changeSet = null;//from w  w  w  .jav a 2  s . c om

    RlogCommand statusCommand = new RlogCommand();
    statusCommand.setHeaderOnly(true);
    statusCommand.setModule(moduleName);
    statusCommand.setRecursive(true);

    try {
        final File tempRlogSpill = File.createTempFile("cvs",
                "status );                                                                                                                                                                                                 `                                                                                                                                                                                                                                                                                                                                                                   ");
        final DeferredFileOutputStream outputStream = new DeferredFileOutputStream(100 * 1024, tempRlogSpill);
        final PrintStream logStream = new PrintStream(outputStream, true,
                getCvsDescriptor().getChangelogEncoding());

        final OutputStream errorOutputStream = new OutputStream() {
            final StringBuffer buffer = new StringBuffer();

            @Override
            public void write(int b) throws IOException {
                if ((int) ("\n".getBytes()[0]) == b) {
                    flush();
                } else {
                    buffer.append(new String(new byte[] { (byte) b }));
                }
            }

            @Override
            public void flush() throws IOException {
                logger.info(buffer.toString());
                buffer.delete(0, buffer.length());
                super.flush();
            }

            public void close() throws IOException {
                flush();
                super.close();
            }
        };
        final PrintStream errorPrintStream = new PrintStream(errorOutputStream);
        Client cvsClient = getCvsClient(cvsRoot, passwordRequired, password);
        cvsClient.getEventManager().addCVSListener(new BasicListener(logStream, errorPrintStream));
        cvsClient.executeCommand(statusCommand, getGlobalOptions(cvsRoot));

        logStream.close();
        errorPrintStream.flush();
        errorPrintStream.close();

        CvsLog parser = new CvsLog() {
            @Override
            public Reader read() throws IOException {
                if (outputStream.isInMemory())
                    return new InputStreamReader(new ByteArrayInputStream(outputStream.getData()),
                            getCvsDescriptor().getChangelogEncoding());
                else
                    return new InputStreamReader(new FileInputStream(outputStream.getFile()),
                            getCvsDescriptor().getChangelogEncoding());
            }

            @Override
            public void dispose() {
                tempRlogSpill.delete();
            }
        };

        changeSet = parser.mapCvsLog(cvsRoot, new CvsRepositoryLocation.HeadRepositoryLocation());

    } catch (IOException ex) {
        model.add(new ListBoxModel.Option("Could not load symbolic names - " + ex.getLocalizedMessage()));
        return model;
    } catch (CommandAbortedException ex) {
        model.add(new ListBoxModel.Option("Could not load symbolic names - " + ex.getLocalizedMessage()));
        return model;
    } catch (CommandException ex) {
        model.add(new ListBoxModel.Option("Could not load symbolic names - " + ex.getLocalizedMessage()));
        return model;
    } catch (AuthenticationException ex) {
        model.add(new ListBoxModel.Option("Could not load symbolic names - " + ex.getLocalizedMessage()));
        return model;
    }

    model.add(new ListBoxModel.Option("Head", "HEAD"));

    for (String branchName : changeSet.getBranchNames()) {
        model.add(new ListBoxModel.Option(branchName + " (Branch)", branchName));
    }

    for (String tagName : changeSet.getTagNames()) {
        model.add(new ListBoxModel.Option(tagName + " (Tag)", tagName));
    }

    return model;
}

From source file:hudson.scm.AbstractCvs.java

/**
 * Gets the output for the CVS <tt>rlog</tt> command for the given module
 * between the specified dates.//from w w  w  .  j  a  v  a 2 s . c  om
 *
 * @param repository
 *            the repository to connect to for running rlog against
 * @param module
 *            the module to check for changes against
 * @param listener
 *            where to log any error messages to
 * @param startTime
 *            don't list any changes before this time
 * @param endTime
 *            don't list any changes after this time
 * @return the output of rlog with no modifications
 * @throws IOException
 *             on underlying communication failure
 */
private CvsLog getRemoteLogForModule(final CvsRepository repository, final CvsRepositoryItem item,
        final CvsModule module, final Date startTime, final Date endTime, final EnvVars envVars,
        final TaskListener listener) throws IOException {
    final Client cvsClient = getCvsClient(repository, envVars, listener);

    RlogCommand rlogCommand = new RlogCommand();

    // we have to synchronize since we're dealing with DateFormat.format()
    synchronized (DATE_FORMATTER) {
        final String lastBuildDate = DATE_FORMATTER.format(startTime);
        final String endDate = DATE_FORMATTER.format(endTime);

        rlogCommand.setDateFilter(lastBuildDate + "<" + endDate);
    }

    // tell CVS which module we're logging
    rlogCommand.setModule(envVars.expand(module.getRemoteName()));

    // ignore headers for files that aren't in the current change-set
    rlogCommand.setSuppressHeader(true);

    // create an output stream to send the output from CVS command to - we
    // can then parse it from here
    final File tmpRlogSpill = File.createTempFile("cvs", "rlog");
    final DeferredFileOutputStream outputStream = new DeferredFileOutputStream(100 * 1024, tmpRlogSpill);
    final PrintStream logStream = new PrintStream(outputStream, true, getDescriptor().getChangelogEncoding());

    // set a listener with our output stream that we parse the log from
    final CVSListener basicListener = new BasicListener(logStream, listener.getLogger());
    cvsClient.getEventManager().addCVSListener(basicListener);

    // log the command to the current run/polling log
    listener.getLogger().println("cvs " + rlogCommand.getCVSCommand());

    // send the command to be run, we can't continue of the task fails
    try {
        if (!cvsClient.executeCommand(rlogCommand, getGlobalOptions(repository, envVars))) {
            throw new RuntimeException("Error while trying to run CVS rlog");
        }
    } catch (CommandAbortedException e) {
        throw new RuntimeException("CVS rlog command aborted", e);
    } catch (CommandException e) {
        throw new RuntimeException("CVS rlog command failed", e);
    } catch (AuthenticationException e) {
        throw new RuntimeException("CVS authentication failure while running rlog command", e);
    } finally {
        try {
            cvsClient.getConnection().close();
        } catch (IOException ex) {
            listener.getLogger().println("Could not close client connection: " + ex.getMessage());
        }
    }

    // flush the output so we have it all available for parsing
    logStream.close();

    // return the contents of the stream as the output of the command
    return new CvsLog() {
        @Override
        public Reader read() throws IOException {
            // note that master and slave can have different platform encoding
            if (outputStream.isInMemory())
                return new InputStreamReader(new ByteArrayInputStream(outputStream.getData()),
                        getDescriptor().getChangelogEncoding());
            else
                return new InputStreamReader(new FileInputStream(outputStream.getFile()),
                        getDescriptor().getChangelogEncoding());
        }

        @Override
        public void dispose() {
            tmpRlogSpill.delete();
        }
    };
}

From source file:de.mendelson.comm.as2.message.AS2MessageCreation.java

/**Encrypts a byte array and returns it*/
private void encryptDataToMessage(AS2Message message, String receiverCryptAlias, int encryptionType,
        Partner receiver) throws Exception {
    AS2MessageInfo info = (AS2MessageInfo) message.getAS2Info();
    BCCryptoHelper cryptoHelper = new BCCryptoHelper();
    X509Certificate certificate = this.encryptionCertManager.getX509Certificate(receiverCryptAlias);
    CMSEnvelopedDataStreamGenerator dataGenerator = new CMSEnvelopedDataStreamGenerator();
    dataGenerator.addKeyTransRecipient(certificate);
    DeferredFileOutputStream encryptedOutput = new DeferredFileOutputStream(1024 * 1024, "as2encryptdata_",
            ".mem", null);
    OutputStream out = null;/*from w w w  . jav a  2s  .  c o  m*/
    if (encryptionType == AS2Message.ENCRYPTION_3DES) {
        out = dataGenerator.open(encryptedOutput, CMSEnvelopedDataGenerator.DES_EDE3_CBC, "BC");
    } else if (encryptionType == AS2Message.ENCRYPTION_DES) {
        out = dataGenerator.open(encryptedOutput,
                cryptoHelper.convertAlgorithmNameToOID(BCCryptoHelper.ALGORITHM_DES), 56, "BC");
    } else if (encryptionType == AS2Message.ENCRYPTION_RC2_40) {
        out = dataGenerator.open(encryptedOutput, CMSEnvelopedDataGenerator.RC2_CBC, 40, "BC");
    } else if (encryptionType == AS2Message.ENCRYPTION_RC2_64) {
        out = dataGenerator.open(encryptedOutput, CMSEnvelopedDataGenerator.RC2_CBC, 64, "BC");
    } else if (encryptionType == AS2Message.ENCRYPTION_RC2_128) {
        out = dataGenerator.open(encryptedOutput, CMSEnvelopedDataGenerator.RC2_CBC, 128, "BC");
    } else if (encryptionType == AS2Message.ENCRYPTION_RC2_196) {
        out = dataGenerator.open(encryptedOutput, CMSEnvelopedDataGenerator.RC2_CBC, 196, "BC");
    } else if (encryptionType == AS2Message.ENCRYPTION_AES_128) {
        out = dataGenerator.open(encryptedOutput, CMSEnvelopedDataGenerator.AES128_CBC, "BC");
    } else if (encryptionType == AS2Message.ENCRYPTION_AES_192) {
        out = dataGenerator.open(encryptedOutput, CMSEnvelopedDataGenerator.AES192_CBC, "BC");
    } else if (encryptionType == AS2Message.ENCRYPTION_AES_256) {
        out = dataGenerator.open(encryptedOutput, CMSEnvelopedDataGenerator.AES256_CBC, "BC");
    } else if (encryptionType == AS2Message.ENCRYPTION_RC4_40) {
        out = dataGenerator.open(encryptedOutput,
                cryptoHelper.convertAlgorithmNameToOID(BCCryptoHelper.ALGORITHM_RC4), 40, "BC");
    } else if (encryptionType == AS2Message.ENCRYPTION_RC4_56) {
        out = dataGenerator.open(encryptedOutput,
                cryptoHelper.convertAlgorithmNameToOID(BCCryptoHelper.ALGORITHM_RC4), 56, "BC");
    } else if (encryptionType == AS2Message.ENCRYPTION_RC4_128) {
        out = dataGenerator.open(encryptedOutput,
                cryptoHelper.convertAlgorithmNameToOID(BCCryptoHelper.ALGORITHM_RC4), 128, "BC");
    }
    if (out == null) {
        throw new Exception("Internal failure: unsupported encryption type " + encryptionType);
    }
    out.write(message.getDecryptedRawData());
    out.close();
    encryptedOutput.close();
    //size of the data was < than the threshold
    if (encryptedOutput.isInMemory()) {
        message.setRawData(encryptedOutput.getData());
    } else {
        //data has been written to a temp file: reread and return
        ByteArrayOutputStream memOut = new ByteArrayOutputStream();
        encryptedOutput.writeTo(memOut);
        memOut.flush();
        memOut.close();
        //finally delete the temp file
        boolean deleted = encryptedOutput.getFile().delete();
        message.setRawData(memOut.toByteArray());
    }
    if (this.logger != null) {
        String cryptAlias = this.encryptionCertManager
                .getAliasByFingerprint(receiver.getCryptFingerprintSHA1());
        this.logger.log(Level.INFO, this.rb.getResourceString("message.encrypted",
                new Object[] { info.getMessageId(), cryptAlias,
                        this.rbMessage.getResourceString("encryption." + receiver.getEncryptionType()) }),
                info);
    }
}

From source file:org.apache.maven.plugin.resources.remote.ProcessRemoteResourcesMojo.java

/**
 * If the transformation result fits in memory and the destination file already exists
 * then both are compared./*w  ww.ja  v a  2  s  .co m*/
 * <p>If destination file is byte-by-byte equal, then it is not overwritten.
 * This improves subsequent compilation times since upstream plugins property see that
 * the resource was not modified.
 * <p>Note: the method should be called after {@link org.apache.commons.io.output.DeferredFileOutputStream#close}
 *
 * @param outStream Deferred stream
 * @throws IOException
 */
private void fileWriteIfDiffers(DeferredFileOutputStream outStream) throws IOException {
    File file = outStream.getFile();
    if (outStream.isThresholdExceeded()) {
        getLog().info("File " + file + " was overwritten due to content limit threshold "
                + outStream.getThreshold() + " reached");
        return;
    }
    boolean needOverwrite = true;

    if (file.exists()) {
        InputStream is = new FileInputStream(file);
        InputStream newContents = new ByteArrayInputStream(outStream.getData());
        try {
            needOverwrite = !IOUtil.contentEquals(is, newContents);
            if (getLog().isDebugEnabled()) {
                getLog().debug("File " + file + " contents " + (needOverwrite ? "differs" : "does not differ"));
            }
        } finally {
            IOUtil.close(is);
        }
    }

    if (!needOverwrite) {
        getLog().info("File " + file + " is up to date");
        return;
    }
    getLog().info("Writing " + file);
    OutputStream os = new FileOutputStream(file);
    try {
        outStream.writeTo(os);
    } finally {
        IOUtil.close(os);
    }
}

From source file:org.apache.phoenix.iterate.SpoolingResultIterator.java

/**
* Create a result iterator by iterating through the results of a scan, spooling them to disk once
* a threshold has been reached. The scanner passed in is closed prior to returning.
* @param scanner the results of a table scan
* @param mm memory manager tracking memory usage across threads.
* @param thresholdBytes the requested threshold.  Will be dialed down if memory usage (as determined by
*  the memory manager) is exceeded.//from w  ww . j  a  v  a  2 s .  com
* @throws SQLException
*/
SpoolingResultIterator(SpoolingMetricsHolder sMetrics, MemoryMetricsHolder mMetrics, ResultIterator scanner,
        MemoryManager mm, final int thresholdBytes, final long maxSpoolToDisk, final String spoolDirectory)
        throws SQLException {
    this.spoolMetrics = sMetrics;
    this.memoryMetrics = mMetrics;
    boolean success = false;
    long startTime = System.currentTimeMillis();
    final MemoryChunk chunk = mm.allocate(0, thresholdBytes);
    long waitTime = System.currentTimeMillis() - startTime;
    GLOBAL_MEMORY_WAIT_TIME.update(waitTime);
    memoryMetrics.getMemoryWaitTimeMetric().change(waitTime);
    DeferredFileOutputStream spoolTo = null;
    try {
        // Can't be bigger than int, since it's the max of the above allocation
        int size = (int) chunk.getSize();
        spoolTo = new DeferredFileOutputStream(size, "ResultSpooler", ".bin", new File(spoolDirectory)) {
            @Override
            protected void thresholdReached() throws IOException {
                try {
                    super.thresholdReached();
                } finally {
                    chunk.close();
                }
            }
        };
        DataOutputStream out = new DataOutputStream(spoolTo);
        final long maxBytesAllowed = maxSpoolToDisk == -1 ? Long.MAX_VALUE : thresholdBytes + maxSpoolToDisk;
        long bytesWritten = 0L;
        for (Tuple result = scanner.next(); result != null; result = scanner.next()) {
            int length = TupleUtil.write(result, out);
            bytesWritten += length;
            if (bytesWritten > maxBytesAllowed) {
                throw new SpoolTooBigToDiskException("result too big, max allowed(bytes): " + maxBytesAllowed);
            }
        }
        if (spoolTo.isInMemory()) {
            byte[] data = spoolTo.getData();
            chunk.resize(data.length);
            spoolFrom = new InMemoryResultIterator(data, chunk);
            GLOBAL_MEMORY_CHUNK_BYTES.update(data.length);
            memoryMetrics.getMemoryChunkSizeMetric().change(data.length);
        } else {
            long sizeOfSpoolFile = spoolTo.getFile().length();
            GLOBAL_SPOOL_FILE_SIZE.update(sizeOfSpoolFile);
            GLOBAL_SPOOL_FILE_COUNTER.increment();
            spoolMetrics.getNumSpoolFileMetric().increment();
            spoolMetrics.getSpoolFileSizeMetric().change(sizeOfSpoolFile);
            spoolFrom = new OnDiskResultIterator(spoolTo.getFile());
            if (spoolTo.getFile() != null) {
                spoolTo.getFile().deleteOnExit();
            }
        }
        success = true;
    } catch (IOException e) {
        throw ServerUtil.parseServerException(e);
    } finally {
        try {
            scanner.close();
        } finally {
            try {
                if (spoolTo != null) {
                    if (!success && spoolTo.getFile() != null) {
                        spoolTo.getFile().delete();
                    }
                    spoolTo.close();
                }
            } catch (IOException ignored) {
                // ignore close error
            } finally {
                if (!success) {
                    chunk.close();
                }
            }
        }
    }
}