public static byte[] serializeJobConf(JobConf jobConf) { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { jobConf.write(new DataOutputStream(out)); } catch (IOException e) { LOG.error("Error serializing job configuration: " + e, e); return null; } finally { try { out.close(); } catch (IOException e) { LOG.error("Error closing output stream: " + e, e); } } return out.toByteArray(); }
public static byte[] serializeJobConf(JobConf jobConf) { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { jobConf.write(new DataOutputStream(out)); } catch (IOException e) { LOG.error("Error serializing job configuration: " + e, e); return null; } finally { try { out.close(); } catch (IOException e) { LOG.error("Error closing output stream: " + e, e); } } return out.toByteArray(); }
return eventList; } finally { outputStream.close(); inputStream.close();
/** * Encrypt a test string with a symmetric key and check that it can be decrypted * @throws IOException * @throws PGPException */ @Test public void encryptSym() throws IOException, PGPException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStream os = GPGFileEncryptor.encryptFile(baos, PASSWORD, "DES"); os.write(EXPECTED_FILE_CONTENT_BYTES); os.close(); baos.close(); byte[] encryptedBytes = baos.toByteArray(); try (InputStream is = GPGFileDecryptor.decryptFile(new ByteArrayInputStream(encryptedBytes), "test")) { byte[] decryptedBytes = IOUtils.toByteArray(is); Assert.assertNotEquals(EXPECTED_FILE_CONTENT_BYTES, encryptedBytes); Assert.assertEquals(EXPECTED_FILE_CONTENT_BYTES, decryptedBytes); } }
/** * Encrypt a test string with an asymmetric key and check that it can be decrypted * @throws IOException * @throws PGPException */ @Test public void encryptAsym() throws IOException, PGPException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStream os = GPGFileEncryptor.encryptFile(baos, getClass().getResourceAsStream(PUBLIC_KEY), Long.parseUnsignedLong(KEY_ID, 16), "CAST5"); os.write(EXPECTED_FILE_CONTENT_BYTES); os.close(); baos.close(); byte[] encryptedBytes = baos.toByteArray(); try (InputStream is = GPGFileDecryptor.decryptFile(new ByteArrayInputStream(encryptedBytes), getClass().getResourceAsStream(PRIVATE_KEY), PASSPHRASE)) { byte[] decryptedBytes = IOUtils.toByteArray(is); Assert.assertNotEquals(EXPECTED_FILE_CONTENT_BYTES, encryptedBytes); Assert.assertEquals(EXPECTED_FILE_CONTENT_BYTES, decryptedBytes); } }
} catch(MessagingException me) { logger.error("Exception while constructing key body headers", me); keysOutputStream.close(); throw me; } catch(MessagingException me) { logger.error("Exception while constructing value body part", me); keysOutputStream.close(); throw me; } catch(MessagingException me) { logger.error("Exception while constructing key body part", me); keysOutputStream.close(); throw me; keysOutputStream.close();
@Test(expectedExceptions = NtlmEngineException.class) public void testGenerateType3MsgThworsExceptionWhenType2IndicatorNotPresent() throws IOException { ByteArrayOutputStream buf = new ByteArrayOutputStream(); buf.write("NTLMSSP".getBytes(StandardCharsets.US_ASCII)); buf.write(0); // type 2 indicator buf.write(3); buf.write(0); buf.write(0); buf.write(0); buf.write("challenge".getBytes()); NtlmEngine engine = new NtlmEngine(); engine.generateType3Msg("username", "password", "localhost", "workstation", Base64.getEncoder().encodeToString(buf.toByteArray())); buf.close(); fail("An NtlmEngineException must have occurred as type 2 indicator is incorrect"); }
@Test public void testToInputStream() throws IOException { final ByteArrayOutputStream baout = new ByteArrayOutputStream(); final java.io.ByteArrayOutputStream ref = new java.io.ByteArrayOutputStream(); //Write 8224 bytes writeData(baout, ref, 32); for(int i=0;i<128;i++) { writeData(baout, ref, 64); } //Get data before more writes final InputStream in = baout.toInputStream(); byte refData[] = ref.toByteArray(); //Write some more data writeData(baout, ref, new int[] { 2, 4, 8, 16 }); //Check original data byte baoutData[] = IOUtils.toByteArray(in); assertEquals(8224, baoutData.length); checkByteArrays(refData, baoutData); //Check all data written baoutData = IOUtils.toByteArray(baout.toInputStream()); refData = ref.toByteArray(); assertEquals(8254, baoutData.length); checkByteArrays(refData, baoutData); baout.close(); in.close(); }
@Test public void testToInputStreamWithReset() throws IOException { //Make sure reset() do not destroy InputStream returned from toInputStream() final ByteArrayOutputStream baout = new ByteArrayOutputStream(); final java.io.ByteArrayOutputStream ref = new java.io.ByteArrayOutputStream(); //Write 8224 bytes writeData(baout, ref, 32); for(int i=0;i<128;i++) { writeData(baout, ref, 64); } //Get data before reset final InputStream in = baout.toInputStream(); byte refData[] = ref.toByteArray(); //Reset and write some new data baout.reset(); ref.reset(); writeData(baout, ref, new int[] { 2, 4, 8, 16 }); //Check original data byte baoutData[] = IOUtils.toByteArray(in); assertEquals(8224, baoutData.length); checkByteArrays(refData, baoutData); //Check new data written after reset baoutData = IOUtils.toByteArray(baout.toInputStream()); refData = ref.toByteArray(); assertEquals(30, baoutData.length); checkByteArrays(refData, baoutData); baout.close(); in.close(); }
@Test(expectedExceptions = NtlmEngineException.class) public void testGenerateType3MsgThrowsExceptionWhenUnicodeSupportNotIndicated() throws IOException { ByteArrayOutputStream buf = new ByteArrayOutputStream(); buf.write("NTLMSSP".getBytes(StandardCharsets.US_ASCII)); buf.write(0); // type 2 indicator buf.write(2); buf.write(0); buf.write(0); buf.write(0); buf.write(longToBytes(1L)); // we want to write a Long // flags buf.write(0);// unicode support indicator buf.write(0); buf.write(0); buf.write(0); buf.write(longToBytes(1L));// challenge NtlmEngine engine = new NtlmEngine(); engine.generateType3Msg("username", "password", "localhost", "workstation", Base64.getEncoder().encodeToString(buf.toByteArray())); buf.close(); fail("An NtlmEngineException must have occurred as unicode support is not indicated"); }
private <K, V> String explainToString(Map<K, V> explainMap) throws Exception { ExplainWork work = new ExplainWork(); ParseContext pCtx = new ParseContext(); HashMap<String, TableScanOperator> topOps = new HashMap<>(); TableScanOperator scanOp = new DummyOperator(new DummyExplainDesc<K, V>(explainMap)); topOps.put("sample", scanOp); pCtx.setTopOps(topOps); work.setParseContext(pCtx); ByteArrayOutputStream baos = new ByteArrayOutputStream(); work.setConfig(new ExplainConfiguration()); ExplainTask newExplainTask = new ExplainTask(); newExplainTask.queryState = uut.queryState; newExplainTask.getJSONLogicalPlan(new PrintStream(baos), work); baos.close(); return baos.toString(); }
@Test public void testGenerateType3Msg() throws IOException { ByteArrayOutputStream buf = new ByteArrayOutputStream(); buf.write("NTLMSSP".getBytes(StandardCharsets.US_ASCII)); buf.write(0); // type 2 indicator buf.write(2); buf.write(0); buf.write(0); buf.write(0); buf.write(longToBytes(0L)); // we want to write a Long // flags buf.write(1);// unicode support indicator buf.write(0); buf.write(0); buf.write(0); buf.write(longToBytes(1L));// challenge NtlmEngine engine = new NtlmEngine(); String type3Msg = engine.generateType3Msg("username", "password", "localhost", "workstation", Base64.getEncoder().encodeToString(buf.toByteArray())); buf.close(); assertEquals( type3Msg, "TlRMTVNTUAADAAAAGAAYAEgAAAAYABgAYAAAABIAEgB4AAAAEAAQAIoAAAAWABYAmgAAAAAAAACwAAAAAQAAAgUBKAoAAAAP1g6lqqN1HZ0wSSxeQ5riQkyh7/UexwVlCPQm0SHU2vsDQm2wM6NbT2zPonPzLJL0TABPAEMAQQBMAEgATwBTAFQAdQBzAGUAcgBuAGEAbQBlAFcATwBSAEsAUwBUAEEAVABJAE8ATgA=", "Incorrect type3 message generated"); }
@Override public void close() throws IOException { super.close(); data = this.toByteArray(); } };
@Override public void close() throws IOException { super.close(); data = this.toByteArray(); } };
public static byte[] write(Serializable obj) throws Exception{ ByteArrayOutputStream arroutput = new ByteArrayOutputStream(); FSTObjectOutput objoutput = conf.get().getObjectOutput(arroutput); try{ objoutput.writeObject(obj); objoutput.flush(); return arroutput.toByteArray(); }finally{ arroutput.close(); } }
@Override public void close() throws IOException { super.close(); modify(); content = toByteArray(); } };
@Override public void tearDown() throws Exception { super.tearDown(); outputStream.close(); }
@After public void tearDown() throws Exception { MgnlContext.setInstance(null); outputStream.close(); }