Search in sources :

Example 21 with NullOutputStream

use of org.apache.commons.io.output.NullOutputStream in project jackrabbit-oak by apache.

the class AzureDataStoreTest method getIdForInputStream.

private static String getIdForInputStream(final InputStream in) throws NoSuchAlgorithmException, IOException {
    MessageDigest digest = MessageDigest.getInstance("SHA-1");
    OutputStream output = new DigestOutputStream(new NullOutputStream(), digest);
    try {
        IOUtils.copyLarge(in, output);
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.closeQuietly(in);
    }
    return encodeHexString(digest.digest());
}
Also used : DigestOutputStream(java.security.DigestOutputStream) DigestOutputStream(java.security.DigestOutputStream) OutputStream(java.io.OutputStream) NullOutputStream(org.apache.commons.io.output.NullOutputStream) MessageDigest(java.security.MessageDigest) NullOutputStream(org.apache.commons.io.output.NullOutputStream)

Example 22 with NullOutputStream

use of org.apache.commons.io.output.NullOutputStream in project jmeter by apache.

the class FTPSampler method sample.

@Override
public SampleResult sample(Entry e) {
    SampleResult res = new SampleResult();
    // Assume failure
    res.setSuccessful(false);
    String remote = getRemoteFilename();
    String local = getLocalFilename();
    boolean binaryTransfer = isBinaryMode();
    res.setSampleLabel(getName());
    final String label = getLabel();
    res.setSamplerData(label);
    try {
        res.setURL(new URL(label));
    } catch (MalformedURLException e1) {
        log.warn("Cannot set URL: " + e1.getLocalizedMessage());
    }
    InputStream input = null;
    FileInputStream fileIS = null;
    res.sampleStart();
    FTPClient ftp = new FTPClient();
    try {
        savedClient = ftp;
        final int port = getPortAsInt();
        if (port > 0) {
            ftp.connect(getServer(), port);
        } else {
            ftp.connect(getServer());
        }
        res.latencyEnd();
        int reply = ftp.getReplyCode();
        if (FTPReply.isPositiveCompletion(reply)) {
            if (ftp.login(getUsername(), getPassword())) {
                if (binaryTransfer) {
                    ftp.setFileType(FTP.BINARY_FILE_TYPE);
                }
                // should probably come from the setup dialog
                ftp.enterLocalPassiveMode();
                boolean ftpOK = false;
                if (isUpload()) {
                    String contents = getLocalFileContents();
                    if (contents.length() > 0) {
                        // TODO - charset?
                        byte[] bytes = contents.getBytes();
                        input = new ByteArrayInputStream(bytes);
                        res.setBytes((long) bytes.length);
                    } else {
                        File infile = new File(local);
                        res.setBytes(infile.length());
                        // NOSONAR False positive, fileIS is closed in finally and not overwritten
                        fileIS = new FileInputStream(infile);
                        input = new BufferedInputStream(fileIS);
                    }
                    ftpOK = ftp.storeFile(remote, input);
                } else {
                    final boolean saveResponse = isSaveResponse();
                    // No need to close this
                    ByteArrayOutputStream baos = null;
                    OutputStream target = null;
                    OutputStream output = null;
                    try {
                        if (saveResponse) {
                            baos = new ByteArrayOutputStream();
                            target = baos;
                        }
                        if (local.length() > 0) {
                            // NOSONAR False positive, the output is closed in finally and not overwritten
                            output = new FileOutputStream(local);
                            if (target == null) {
                                target = output;
                            } else {
                                target = new TeeOutputStream(output, baos);
                            }
                        }
                        if (target == null) {
                            target = new NullOutputStream();
                        }
                        input = ftp.retrieveFileStream(remote);
                        if (input == null) {
                            // Could not access file or other error
                            res.setResponseCode(Integer.toString(ftp.getReplyCode()));
                            res.setResponseMessage(ftp.getReplyString());
                        } else {
                            long bytes = IOUtils.copy(input, target);
                            ftpOK = bytes > 0;
                            if (saveResponse && baos != null) {
                                res.setResponseData(baos.toByteArray());
                                if (!binaryTransfer) {
                                    res.setDataType(SampleResult.TEXT);
                                }
                            } else {
                                res.setBytes(bytes);
                            }
                        }
                    } finally {
                        IOUtils.closeQuietly(target);
                        IOUtils.closeQuietly(output);
                    }
                }
                if (ftpOK) {
                    res.setResponseCodeOK();
                    res.setResponseMessageOK();
                    res.setSuccessful(true);
                } else {
                    res.setResponseCode(Integer.toString(ftp.getReplyCode()));
                    res.setResponseMessage(ftp.getReplyString());
                }
            } else {
                res.setResponseCode(Integer.toString(ftp.getReplyCode()));
                res.setResponseMessage(ftp.getReplyString());
            }
        } else {
            // TODO
            res.setResponseCode("501");
            res.setResponseMessage("Could not connect");
            //res.setResponseCode(Integer.toString(ftp.getReplyCode()));
            res.setResponseMessage(ftp.getReplyString());
        }
    } catch (IOException ex) {
        // TODO
        res.setResponseCode("000");
        res.setResponseMessage(ex.toString());
    } finally {
        savedClient = null;
        if (ftp.isConnected()) {
            try {
                ftp.logout();
            } catch (IOException ignored) {
            }
            try {
                ftp.disconnect();
            } catch (IOException ignored) {
            }
        }
        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(fileIS);
    }
    res.sampleEnd();
    return res;
}
Also used : MalformedURLException(java.net.MalformedURLException) TeeOutputStream(org.apache.commons.io.output.TeeOutputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TeeOutputStream(org.apache.commons.io.output.TeeOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) NullOutputStream(org.apache.commons.io.output.NullOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) URL(java.net.URL) FileInputStream(java.io.FileInputStream) FTPClient(org.apache.commons.net.ftp.FTPClient) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) SampleResult(org.apache.jmeter.samplers.SampleResult) File(java.io.File) NullOutputStream(org.apache.commons.io.output.NullOutputStream)

Example 23 with NullOutputStream

use of org.apache.commons.io.output.NullOutputStream in project jmeter by apache.

the class SmtpSampler method calculateMessageSize.

private long calculateMessageSize(Message message) throws IOException, MessagingException {
    if (getPropertyAsBoolean(MESSAGE_SIZE_STATS)) {
        // calculate message size
        CountingOutputStream cs = new CountingOutputStream(new NullOutputStream());
        message.writeTo(cs);
        return cs.getByteCount();
    } else {
        return -1L;
    }
}
Also used : CountingOutputStream(org.apache.commons.io.output.CountingOutputStream) NullOutputStream(org.apache.commons.io.output.NullOutputStream)

Example 24 with NullOutputStream

use of org.apache.commons.io.output.NullOutputStream in project webservices-axiom by apache.

the class TestGetPrefixAfterWriteDefaultNamespace method runTest.

protected void runTest() throws Throwable {
    XMLStreamWriter writer = staxImpl.newNormalizedXMLOutputFactory().createXMLStreamWriter(new NullOutputStream());
    writer.writeStartElement("ns1", "root", "urn:ns1");
    writer.writeDefaultNamespace("urn:ns2");
    assertEquals("", writer.getPrefix("urn:ns2"));
}
Also used : XMLStreamWriter(javax.xml.stream.XMLStreamWriter) NullOutputStream(org.apache.commons.io.output.NullOutputStream)

Example 25 with NullOutputStream

use of org.apache.commons.io.output.NullOutputStream in project webservices-axiom by apache.

the class TestSerializeAndConsume method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMDocument document = OMXMLBuilderFactory.createOMBuilder(factory, new StringReader("<elem>text</elem>")).getDocument();
    document.serializeAndConsume(new NullOutputStream());
    assertConsumed(document);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) StringReader(java.io.StringReader) OMDocument(org.apache.axiom.om.OMDocument) NullOutputStream(org.apache.commons.io.output.NullOutputStream)

Aggregations

NullOutputStream (org.apache.commons.io.output.NullOutputStream)30 InputStream (java.io.InputStream)7 Test (org.junit.Test)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 OutputStream (java.io.OutputStream)5 DigestOutputStream (java.security.DigestOutputStream)5 MessageDigest (java.security.MessageDigest)5 FileInputStream (java.io.FileInputStream)3 PrintStream (java.io.PrintStream)3 DataHandler (javax.activation.DataHandler)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 PipedInputStream (java.io.PipedInputStream)2 ArrayList (java.util.ArrayList)2 Node (javax.jcr.Node)2 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)2 OMFactory (org.apache.axiom.om.OMFactory)2 ExceptionInputStream (org.apache.axiom.testutils.io.ExceptionInputStream)2