use of java.io.FilterOutputStream in project apex-malhar by apache.
the class AbstractFileOutputOperatorTest method testChainFilters.
@Test
public void testChainFilters() throws NoSuchAlgorithmException, IOException {
EvenOddHDFSExactlyOnceWriter writer = new EvenOddHDFSExactlyOnceWriter();
KeyGenerator keygen = KeyGenerator.getInstance("AES");
keygen.init(128);
final SecretKey secretKey = keygen.generateKey();
byte[] iv = "TestParam16bytes".getBytes();
final IvParameterSpec ivps = new IvParameterSpec(iv);
FilterStreamProvider.FilterChainStreamProvider<FilterOutputStream, OutputStream> chainStreamProvider = new FilterStreamProvider.FilterChainStreamProvider<FilterOutputStream, OutputStream>();
chainStreamProvider.addStreamProvider(new FilterStreamCodec.GZipFilterStreamProvider());
// The filter is to keep track of the offsets to handle multi member gzip issue with openjdk
// http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4691425
final CounterFilterStreamContext evenCounterContext = new CounterFilterStreamContext();
final CounterFilterStreamContext oddCounterContext = new CounterFilterStreamContext();
chainStreamProvider.addStreamProvider(new FilterStreamProvider.SimpleFilterReusableStreamProvider<CounterFilterOutputStream, OutputStream>() {
@Override
protected FilterStreamContext<CounterFilterOutputStream> createFilterStreamContext(OutputStream outputStream) throws IOException {
if (evenCounterContext.isDoInit()) {
evenCounterContext.init(outputStream);
return evenCounterContext;
} else {
oddCounterContext.init(outputStream);
return oddCounterContext;
}
}
});
chainStreamProvider.addStreamProvider(new FilterStreamProvider.SimpleFilterReusableStreamProvider<CipherOutputStream, OutputStream>() {
@Override
protected FilterStreamContext<CipherOutputStream> createFilterStreamContext(OutputStream outputStream) throws IOException {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivps);
return new FilterStreamCodec.CipherFilterStreamContext(outputStream, cipher);
} catch (Exception e) {
throw new IOException(e);
}
}
});
writer.setFilterStreamProvider(chainStreamProvider);
File evenFile = new File(testMeta.getDir(), EVEN_FILE);
File oddFile = new File(testMeta.getDir(), ODD_FILE);
List<Long> evenOffsets = new ArrayList<Long>();
List<Long> oddOffsets = new ArrayList<Long>();
writer.setFilePath(testMeta.getDir());
writer.setAlwaysWriteToTmp(false);
writer.setup(testMeta.testOperatorContext);
for (int i = 0; i < 10; ++i) {
writer.beginWindow(i);
for (int j = 0; j < 1000; ++j) {
writer.input.put(i);
}
writer.endWindow();
if ((i % 2) == 1) {
writer.beforeCheckpoint(i);
evenOffsets.add(evenCounterContext.getCounter());
oddOffsets.add(oddCounterContext.getCounter());
}
}
writer.teardown();
/*
evenOffsets.add(evenFile.length());
oddOffsets.add(oddFile.length());
*/
checkCompressedFile(evenFile, evenOffsets, 0, 5, 1000, secretKey, iv);
checkCompressedFile(oddFile, oddOffsets, 1, 5, 1000, secretKey, iv);
}
Aggregations