Example usage for java.io ByteArrayOutputStream toString

List of usage examples for java.io ByteArrayOutputStream toString

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream toString.

Prototype

public synchronized String toString() 

Source Link

Document

Converts the buffer's contents into a string decoding bytes using the platform's default character set.

Usage

From source file:com.jaspersoft.studio.community.utils.CommunityAPIUtils.java

private static String tryCreateMozilla() {
    if (EnvironmentUtils.IS_LINUX) {
        boolean oldDebug = Device.DEBUG;
        Device.DEBUG = true;/*from   w  w w  .  j  a  va  2s . c o  m*/
        PrintStream oldOut = System.out;
        Shell shell = null;
        PrintStream newOut = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            newOut = new PrintStream(baos);
            // replace the out since the Mozilla output debug results into
            // stdout.
            System.setOut(newOut);
            shell = new Shell();
            try {
                new Browser(shell, SWT.NONE);
            } catch (Throwable e) {
                UIUtils.showError(e);
            }
            return baos.toString();
        } catch (Throwable e1) {
            // ignore
        } finally {
            if (shell != null) {
                shell.dispose();
            }
            System.setOut(oldOut);
            IOUtils.closeQuietly(newOut);
            Device.DEBUG = oldDebug;
        }
    }
    return ""; //$NON-NLS-1$
}

From source file:net.sf.jasperreports.eclipse.util.FileUtils.java

public static String readInputStreamAsString(InputStream in) throws IOException {

    BufferedInputStream bis = new BufferedInputStream(in);
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    int result = bis.read();
    while (result != -1) {
        byte b = (byte) result;
        buf.write(b);/*  w  w w  .j  a va 2  s  .  c om*/
        result = bis.read();
    }
    return buf.toString();
}

From source file:me.tfeng.toolbox.avro.AvroHelper.java

public static String toJson(Schema schema, Object object) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    JsonGenerator generator = new JsonFactory().createJsonGenerator(outputStream, JsonEncoding.UTF8);
    generator.useDefaultPrettyPrinter();
    SpecificDatumWriter<Object> writer = new SpecificDatumWriter<>(schema);
    JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, generator);
    writer.write(object, encoder);//ww  w  . j  a  v a  2  s . c  o m
    encoder.flush();
    return outputStream.toString();
}

From source file:me.tfeng.play.avro.AvroHelper.java

public static String toJson(Schema schema, Object object) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    JsonGenerator generator = new JsonFactory().createJsonGenerator(outputStream, JsonEncoding.UTF8);
    generator.useDefaultPrettyPrinter();
    SpecificDatumWriter<Object> writer = new SpecificDatumWriter<>(schema);
    JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, generator);
    writer.write(object, encoder);/*w w w  .  ja va  2s.c om*/
    encoder.flush();
    String json = outputStream.toString();
    return AvroHelper.convertToSimpleRecord(schema, json);
}

From source file:com.ikon.util.ExecutionUtils.java

/**
 * Execute script from file/* w  w  w . j  a va 2s.  c  o m*/
 * 
 * @return 0 - Return
 *         1 - StdOut
 *         2 - StdErr
 */
public static Object[] runScript(File script) throws EvalError {
    Object[] ret = new Object[3];
    FileReader fr = null;

    try {
        if (script.exists() && script.canRead()) {
            ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
            PrintStream out = new PrintStream(baosOut);
            ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
            PrintStream err = new PrintStream(baosErr);
            Interpreter i = new Interpreter(null, out, err, false);
            fr = new FileReader(script);

            ret[0] = i.eval(fr);

            out.flush();
            ret[1] = baosOut.toString();
            err.flush();
            ret[2] = baosErr.toString();
        } else {
            log.warn("Unable to read script: {}", script.getPath());
        }
    } catch (IOException e) {
        log.warn(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(fr);
    }

    log.debug("runScript: {}", Arrays.toString(ret));
    return ret;
}

From source file:net.sf.sahi.util.Utils.java

public static String getStackTraceString(Exception e, boolean forHTML) {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    PrintStream s = new PrintStream(b);
    e.printStackTrace(s);// w  w w  .j  a va 2  s  . com
    String str = b.toString();
    if (forHTML)
        str = str.replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\\\r", "").replaceAll("\\\n",
                "<br/>");
    return str;
}

From source file:XMLWriter.java

/**
 * Convert a String to an encoded version of that String replacing
 * ML delimiter characters by special entities.
 * @param any//from   w w w  .  ja v a2s . com
 * @return encoded argument String
 */
public static String encoded(String any) {
    // Replace <, >, ' and " by special entities
    ByteArrayOutputStream bos = new ByteArrayOutputStream(any.length());
    XMLWriter.encodeOn(any, new PrintStream(bos));
    return bos.toString();
}

From source file:com.ning.metrics.collector.TestPerformance.java

@SuppressWarnings("unused")
private static String createSmilePayload() throws IOException {
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    SmileFactory f = new SmileFactory();
    JsonGenerator g = f.createJsonGenerator(stream);

    g.writeStartObject();//  w ww  .j a  v a2 s.  com
    g.writeNumberField(SmileEnvelopeEvent.SMILE_EVENT_DATETIME_TOKEN_NAME, new DateTime().getMillis());
    g.writeStringField("FuuField", "fuu");
    g.writeBooleanField("TrueField", true);
    g.writeNumberField("Pi", 3.1459);
    g.writeNumberField("Long", 10001000000L);
    g.writeEndObject();
    g.close(); // important: will force flushing of output, close underlying output stream

    return stream.toString();
}

From source file:com.ghgande.j2mod.modbus.utils.TestUtils.java

/**
 * Runs a command line task and returns the screen output or throws and
 * error if something bad happened// w  ww  .jav  a 2  s  . c  o  m
 *
 * @param command Command to run
 *
 * @return Screen output
 *
 * @throws Exception
 */
public static String execToString(String command) throws Exception {

    // Prepare the command line

    CommandLine commandline = CommandLine.parse(command);

    // Prepare the output stream

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);

    // Prepare the executor

    DefaultExecutor exec = new DefaultExecutor();
    exec.setExitValues(null);
    exec.setStreamHandler(streamHandler);
    exec.setWatchdog(new ExecuteWatchdog(5000));

    // Execute the command
    try {
        exec.execute(commandline);
        return (outputStream.toString());
    } catch (Exception e) {
        throw new Exception(String.format("%s - %s", outputStream.toString(), e.getMessage()));
    }
}

From source file:com.jredrain.base.utils.CommandUtils.java

public static String executeShell(File shellFile, String... args) {
    String info = null;//from   w w w.j a  v a2s  .  c  om
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {

        String params = " ";
        if (CommonUtils.notEmpty(args)) {
            for (String p : args) {
                params += p + " ";
            }
        }

        CommandLine commandLine = CommandLine.parse("/bin/bash +x " + shellFile.getAbsolutePath() + params);
        DefaultExecutor exec = new DefaultExecutor();
        exec.setExitValues(null);
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, outputStream);
        exec.setStreamHandler(streamHandler);

        exec.execute(commandLine);
        info = outputStream.toString().trim();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return info;
    }
}