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());
}
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;
}
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;
}
}
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"));
}
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);
}
Aggregations