use of org.apache.commons.io.output.ByteArrayOutputStream in project zeppelin by apache.
the class PigInterpreter method interpret.
@Override
public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) {
// remember the origial stdout, because we will redirect stdout to capture
// the pig dump output.
PrintStream originalStdOut = System.out;
ByteArrayOutputStream bytesOutput = new ByteArrayOutputStream();
File tmpFile = null;
try {
pigServer.setJobName(createJobName(cmd, contextInterpreter));
tmpFile = PigUtils.createTempPigScript(cmd);
System.setOut(new PrintStream(bytesOutput));
// each thread should its own ScriptState & PigStats
ScriptState.start(pigServer.getPigContext().getExecutionEngine().instantiateScriptState());
// reset PigStats, otherwise you may get the PigStats of last job in the same thread
// because PigStats is ThreadLocal variable
PigStats.start(pigServer.getPigContext().getExecutionEngine().instantiatePigStats());
PigScriptListener scriptListener = new PigScriptListener();
ScriptState.get().registerListener(scriptListener);
listenerMap.put(contextInterpreter.getParagraphId(), scriptListener);
pigServer.registerScript(tmpFile.getAbsolutePath());
} catch (IOException e) {
if (e instanceof FrontendException) {
FrontendException fe = (FrontendException) e;
if (!fe.getMessage().contains("Backend error :")) {
// If the error message contains "Backend error :", that means the exception is from
// backend.
LOGGER.error("Fail to run pig script.", e);
return new InterpreterResult(Code.ERROR, ExceptionUtils.getStackTrace(e));
}
}
PigStats stats = PigStats.get();
if (stats != null) {
String errorMsg = PigUtils.extactJobStats(stats);
if (errorMsg != null) {
LOGGER.error("Fail to run pig script, " + errorMsg);
return new InterpreterResult(Code.ERROR, errorMsg);
}
}
LOGGER.error("Fail to run pig script.", e);
return new InterpreterResult(Code.ERROR, ExceptionUtils.getStackTrace(e));
} finally {
System.setOut(originalStdOut);
listenerMap.remove(contextInterpreter.getParagraphId());
if (tmpFile != null) {
tmpFile.delete();
}
}
StringBuilder outputBuilder = new StringBuilder();
PigStats stats = PigStats.get();
if (stats != null && includeJobStats) {
String jobStats = PigUtils.extactJobStats(stats);
if (jobStats != null) {
outputBuilder.append(jobStats);
}
}
outputBuilder.append(bytesOutput.toString());
return new InterpreterResult(Code.SUCCESS, outputBuilder.toString());
}
use of org.apache.commons.io.output.ByteArrayOutputStream in project eureka by Netflix.
the class DiscoveryJerseyProviderTest method testEncodingDecoding.
private void testEncodingDecoding(MediaType mediaType) throws IOException {
// Write
assertThat(jerseyProvider.isWriteable(InstanceInfo.class, InstanceInfo.class, null, mediaType), is(true));
ByteArrayOutputStream out = new ByteArrayOutputStream();
jerseyProvider.writeTo(INSTANCE, InstanceInfo.class, InstanceInfo.class, null, mediaType, null, out);
// Read
assertThat(jerseyProvider.isReadable(InstanceInfo.class, InstanceInfo.class, null, mediaType), is(true));
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
InstanceInfo decodedInstance = (InstanceInfo) jerseyProvider.readFrom(InstanceInfo.class, InstanceInfo.class, null, mediaType, null, in);
assertThat(decodedInstance, is(equalTo(INSTANCE)));
}
use of org.apache.commons.io.output.ByteArrayOutputStream in project quickstarts by jboss-switchyard.
the class JAXBUtil method marshal.
/**
* Returns string representation of {@link GreetingRequest}.
*
* @param rq Request.
* @return XML representation.
* @throws JAXBException
*/
public static String marshal(GreetingRequest rq) throws JAXBException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Marshaller marshaller = CONTEXT.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(rq, bos);
return new String(bos.toByteArray());
}
use of org.apache.commons.io.output.ByteArrayOutputStream in project k-9 by k9mail.
the class DeferredFileBodyTest method withShortData__writeTo__shouldWriteData.
@Test
public void withShortData__writeTo__shouldWriteData() throws Exception {
writeShortTestData();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
deferredFileBody.writeTo(baos);
assertArrayEquals(TEST_DATA_SHORT, baos.toByteArray());
}
use of org.apache.commons.io.output.ByteArrayOutputStream in project hudson-2.x by hudson.
the class ConsoleNote method encodeToBytes.
private ByteArrayOutputStream encodeToBytes() throws IOException {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(new GZIPOutputStream(buf));
oos.writeObject(this);
oos.close();
ByteArrayOutputStream buf2 = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(new Base64OutputStream(buf2, true, -1, null));
buf2.write(PREAMBLE);
dos.writeInt(buf.size());
buf.writeTo(dos);
dos.close();
buf2.write(POSTAMBLE);
return buf2;
}
Aggregations