use of java.io.FilterOutputStream in project druid by druid-io.
the class CompressionUtilsTest method testStreamErrorGunzip.
@Test(expected = IOException.class)
public void testStreamErrorGunzip() throws Exception {
final File tmpDir = temporaryFolder.newFolder("testGoodGzipByteSource");
final File gzFile = new File(tmpDir, testFile.getName() + ".gz");
Assert.assertFalse(gzFile.exists());
CompressionUtils.gzip(Files.asByteSource(testFile), Files.asByteSink(gzFile), Predicates.<Throwable>alwaysTrue());
Assert.assertTrue(gzFile.exists());
try (final InputStream inputStream = CompressionUtils.gzipInputStream(new FileInputStream(gzFile))) {
assertGoodDataStream(inputStream);
}
if (testFile.exists() && !testFile.delete()) {
throw new RuntimeException(String.format("Unable to delete file [%s]", testFile.getAbsolutePath()));
}
Assert.assertFalse(testFile.exists());
final AtomicLong flushes = new AtomicLong(0L);
CompressionUtils.gunzip(new FileInputStream(gzFile), new FilterOutputStream(new FileOutputStream(testFile) {
@Override
public void flush() throws IOException {
if (flushes.getAndIncrement() > 0) {
super.flush();
} else {
throw new IOException("Test exception");
}
}
}));
}
use of java.io.FilterOutputStream in project druid by druid-io.
the class StreamUtilsTest method testRetryExceptionOnFlush.
@Test
public void testRetryExceptionOnFlush() {
final byte[] bytes = new byte[1 << 10];
Random random = new Random(47831947819L);
random.nextBytes(bytes);
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final AtomicLong outputFlushes = new AtomicLong(0);
Assert.assertEquals(bytes.length, StreamUtils.retryCopy(new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return new ByteArrayInputStream(bytes);
}
}, new ByteSink() {
@Override
public OutputStream openStream() throws IOException {
byteArrayOutputStream.reset();
return new FilterOutputStream(byteArrayOutputStream) {
@Override
public void flush() throws IOException {
if (outputFlushes.getAndIncrement() > 0) {
out.flush();
} else {
throw new IOException("Test exception");
}
}
};
}
}, FileUtils.IS_EXCEPTION, 10));
// 2 closes and 2 manual flushes
Assert.assertEquals(4, outputFlushes.get());
Assert.assertArrayEquals(bytes, byteArrayOutputStream.toByteArray());
}
use of java.io.FilterOutputStream in project poi by apache.
the class AesZipFileZipEntrySource method copyToFile.
private static void copyToFile(InputStream is, File tmpFile, CipherAlgorithm cipherAlgorithm, byte[] keyBytes, byte[] ivBytes) throws IOException, GeneralSecurityException {
SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, cipherAlgorithm.jceId);
Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, "PKCS5Padding");
ZipInputStream zis = new ZipInputStream(is);
FileOutputStream fos = new FileOutputStream(tmpFile);
ZipOutputStream zos = new ZipOutputStream(fos);
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
// the cipher output stream pads the data, therefore we can't reuse the ZipEntry with set sizes
// as those will be validated upon close()
ZipEntry zeNew = new ZipEntry(ze.getName());
zeNew.setComment(ze.getComment());
zeNew.setExtra(ze.getExtra());
zeNew.setTime(ze.getTime());
// zeNew.setMethod(ze.getMethod());
zos.putNextEntry(zeNew);
FilterOutputStream fos2 = new FilterOutputStream(zos) {
// don't close underlying ZipOutputStream
@Override
public void close() {
}
};
CipherOutputStream cos = new CipherOutputStream(fos2, ciEnc);
IOUtils.copy(zis, cos);
cos.close();
fos2.close();
zos.closeEntry();
zis.closeEntry();
}
zos.close();
fos.close();
zis.close();
}
use of java.io.FilterOutputStream in project poi by apache.
the class StreamHelper method saveXmlInStream.
/**
* Save the document object in the specified output stream.
*
* @param xmlContent
* The XML document.
* @param outStream
* The OutputStream in which the XML document will be written.
* @return <b>true</b> if the xml is successfully written in the stream,
* else <b>false</b>.
*/
public static boolean saveXmlInStream(Document xmlContent, OutputStream outStream) {
try {
Transformer trans = getIdentityTransformer();
Source xmlSource = new DOMSource(xmlContent);
// prevent close of stream by transformer:
Result outputTarget = new StreamResult(new FilterOutputStream(outStream) {
@Override
public void write(byte[] b, int off, int len) throws IOException {
out.write(b, off, len);
}
@Override
public void close() throws IOException {
// only flush, don't close!
out.flush();
}
});
trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty(OutputKeys.STANDALONE, "yes");
trans.transform(xmlSource, outputTarget);
} catch (TransformerException e) {
return false;
}
return true;
}
use of java.io.FilterOutputStream in project robovm by robovm.
the class OldFilterOutputStreamTest method test_write$B.
public void test_write$B() throws IOException {
Support_OutputStream sos = new Support_OutputStream(testLength);
os = new FilterOutputStream(sos);
os.write(fileString.getBytes());
bis = new ByteArrayInputStream(sos.toByteArray());
assertTrue("Test 1: Bytes have not been written.", bis.available() == testLength);
byte[] wbytes = new byte[testLength];
bis.read(wbytes, 0, testLength);
assertTrue("Test 2: Incorrect bytes written or read.", fileString.equals(new String(wbytes)));
try {
// Support_OutputStream throws an IOException if the internal
// buffer is full, which it should be now.
os.write(42);
fail("Test 2: IOException expected.");
} catch (IOException e) {
// Expected.
}
}
Aggregations