Example usage for org.apache.commons.io.output CloseShieldOutputStream CloseShieldOutputStream

List of usage examples for org.apache.commons.io.output CloseShieldOutputStream CloseShieldOutputStream

Introduction

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

Prototype

public CloseShieldOutputStream(OutputStream out) 

Source Link

Document

Creates a proxy that shields the given output stream from being closed.

Usage

From source file:fi.jumi.launcher.JumiBootstrap.java

public JumiBootstrap enableDebugMode() {
    return enableDebugMode(new CloseShieldOutputStream(System.err));
}

From source file:end2endtests.runner.ProcessRunner.java

private static void redirectStream(InputStream input, OutputStream systemOut, OutputStream toWatcher) {
    redirectStream(new TeeInputStream(input, toWatcher), new CloseShieldOutputStream(systemOut));
}

From source file:company.gonapps.loghut.utils.FileUtils.java

public static void compress(String filePathString, OutputStream outputStream) throws IOException {
    try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(filePathString));
            GzipCompressorOutputStream gzipCompressorOutputStream = new GzipCompressorOutputStream(
                    new CloseShieldOutputStream(outputStream))) {
        IOUtils.copy(bufferedInputStream, gzipCompressorOutputStream);
    }/*from   ww  w.ja v a2  s.  c  om*/
}

From source file:company.gonapps.loghut.utils.FileUtils.java

public static void archive(List<String> filePathStrings, OutputStream outputStream) throws IOException {
    try (TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(
            new CloseShieldOutputStream(outputStream))) {

        for (String filePathString : filePathStrings)
            addFileToArchive(tarArchiveOutputStream, filePathString, "");

        tarArchiveOutputStream.finish();
    }//from w w w  . jav  a2  s .c om
}

From source file:de.tudarmstadt.ukp.dkpro.core.api.discourse.DiscourseDumpWriter.java

@Override
public void initialize(UimaContext context) throws ResourceInitializationException {
    super.initialize(context);

    try {/*from   w w w.j a v  a  2 s.co  m*/
        if (out == null) {
            if ("-".equals(outputFile.getName())) {
                out = new PrintWriter(new CloseShieldOutputStream(System.out));
            } else {
                if (outputFile.getParentFile() != null) {
                    outputFile.getParentFile().mkdirs();
                }
                out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
            }
        }
    } catch (IOException e) {
        throw new ResourceInitializationException(e);
    }

}

From source file:gov.nih.nci.caarray.web.helper.DownloadHelper.java

/**
 * Zips the selected files and writes the result to the servlet output stream. Also sets content type and
 * disposition appropriately.//from w  w w.j  ava2  s.c  o m
 * 
 * @param files the files to zip and send
 * @param baseFilename the filename w/o the suffix to use for the archive file. This filename will be set as the
 *            Content-disposition header
 * @throws IOException if there is an error writing to the stream
 */
public static void downloadFiles(Collection<CaArrayFile> files, String baseFilename) throws IOException {
    final HttpServletResponse response = ServletActionContext.getResponse();

    try {
        final PackagingInfo info = getPreferedPackageInfo(files, baseFilename);
        response.setContentType(info.getMethod().getMimeType());
        response.addHeader("Content-disposition", "filename=\"" + info.getName() + "\"");

        final List<CaArrayFile> sortedFiles = new ArrayList<CaArrayFile>(files);
        Collections.sort(sortedFiles, CAARRAYFILE_NAME_COMPARATOR_INSTANCE);
        final OutputStream sos = response.getOutputStream();
        final OutputStream closeShield = new CloseShieldOutputStream(sos);
        final ArchiveOutputStream arOut = info.getMethod().createArchiveOutputStream(closeShield);
        try {
            for (final CaArrayFile f : sortedFiles) {
                fileAccessUtils.addFileToArchive(f, arOut);
            }
        } finally {
            // note that the caller's stream is shielded from the close(),
            // but this is the only way to finish and flush the (gzip) stream.
            try {
                arOut.close();
            } catch (final Exception e) {
                LOG.error(e);
            }
        }
    } catch (final Exception e) {
        LOG.error("Error streaming download of files " + files, e);
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
}

From source file:de.tudarmstadt.ukp.dkpro.argumentation.io.writer.ArgumentDumpWriter.java

@Override
public void initialize(UimaContext context) throws ResourceInitializationException {
    super.initialize(context);

    try {/*ww w.j ava  2s.co  m*/
        if (out == null) {
            if ("-".equals(outputFile.getName())) {
                // default to System.out
                out = new PrintWriter(new CloseShieldOutputStream(System.out));
            } else {
                if (outputFile.getParentFile() != null) {
                    outputFile.getParentFile().mkdirs();
                }
                out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
            }
        }
    } catch (IOException e) {
        throw new ResourceInitializationException(e);
    }
}

From source file:fi.jumi.test.AppRunner.java

private JumiLauncher createLauncher() {
    class CustomJumiLauncherBuilder extends JumiLauncherBuilder {

        @Override/*from  w w  w. j  ava  2s  . c  o  m*/
        protected ProcessStarter createProcessStarter() {
            return processStarter;
        }

        @Override
        protected NetworkServer createNetworkServer() {
            if (mockNetworkServer != null) {
                return mockNetworkServer;
            }
            return super.createNetworkServer();
        }

        @Override
        protected OutputStream createDaemonOutputListener() {
            return new TeeOutputStream(new CloseShieldOutputStream(System.out),
                    new WriterOutputStream(daemonOutput, daemonDefaultCharset, 1024, true));
        }
    }

    JumiLauncherBuilder builder = new CustomJumiLauncherBuilder();
    return builder.build();
}

From source file:edu.wisc.doit.tcrypt.BouncyCastleFileEncrypter.java

@Override
public void encrypt(String fileName, int size, InputStream inputStream, OutputStream outputStream)
        throws InvalidCipherTextException, IOException {
    final TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(outputStream, 512,
            ENCODING);//from  w w w.  j a  v a  2s.  com

    final BufferedBlockCipher cipher = createCipher(tarArchiveOutputStream);

    startEncryptedFile(fileName, size, tarArchiveOutputStream, cipher);

    //Setup cipher output stream, has to protect from close as the cipher stream must close but the tar cannot get closed yet
    final CipherOutputStream cipherOutputStream = new CipherOutputStream(
            new CloseShieldOutputStream(tarArchiveOutputStream), cipher);

    //Setup digester
    final DigestOutputStream digestOutputStream = new DigestOutputStream(this.createDigester());

    //Perform streaming encryption and hashing of the file
    IOUtils.copy(inputStream, new TeeOutputStream(cipherOutputStream, digestOutputStream));
    cipherOutputStream.close();

    tarArchiveOutputStream.closeArchiveEntry();

    //Capture the hash code of the encrypted file
    digestOutputStream.close();
    final byte[] hashBytes = digestOutputStream.getDigest();

    this.writeHashfile(tarArchiveOutputStream, hashBytes);

    //Close the TAR stream, nothing else should be written to it
    tarArchiveOutputStream.close();
}

From source file:com.joyent.manta.client.crypto.EncryptingEntityHelper.java

/**
 * Creates a new {@link OutputStream} implementation that is backed directly
 * by a {@link CipherOutputStream} or a {@link HmacOutputStream} that wraps
 * a {@link CipherOutputStream} depending on the encryption cipher/mode being
 * used. This allows us to support EtM authentication for ciphers/modes that
 * do not natively support authenticating encryption.
 *
 * NOTE: The design of com.joyent.manta.client.multipart.EncryptionStateRecorder
 * is heavily coupled to this implementation! Changing how these streams are
 * wrapped requires changes to EncryptionStateRecorder!
 *
 * @param httpOut       output stream for writing to the HTTP network socket
 * @param cipherDetails information about the method of encryption in use
 * @param cipher        cipher to utilize for encrypting stream
 * @param hmac          current HMAC object with the current checksum state
 * @return a new stream configured based on the parameters
 *///from   ww  w .  j  av  a  2s. c om
public static OutputStream makeCipherOutputForStream(final OutputStream httpOut,
        final SupportedCipherDetails cipherDetails, final Cipher cipher, final HMac hmac) {
    /* We have to use a "close shield" here because when close() is called
     * on a CipherOutputStream() for two reasons:
     *
     * 1. CipherOutputStream.close() writes additional bytes that a HMAC
     *    would need to read.
     * 2. Since we are going to append a HMAC to the end of the OutputStream
     *    httpOut, then we have to pretend to close it so that the HMAC bytes
     *    are not being written in the middle of the CipherOutputStream and
     *    thereby corrupting the ciphertext. */

    final CloseShieldOutputStream noCloseOut = new CloseShieldOutputStream(httpOut);
    final CipherOutputStream cipherOut = new CipherOutputStream(noCloseOut, cipher);
    final OutputStream out;

    Validate.notNull(cipherDetails, "Cipher details must not be null");
    Validate.notNull(cipher, "Cipher must not be null");

    // Things are a lot more simple if we are using AEAD
    if (cipherDetails.isAEADCipher()) {
        out = cipherOut;
    } else {
        out = new HmacOutputStream(hmac, cipherOut);
    }

    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Creating new OutputStream for multipart [{}]", out.getClass());
    }

    return out;
}