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

@Deprecated
public synchronized String toString(int hibyte) 

Source Link

Document

Creates a newly allocated string.

Usage

From source file:com.azaptree.services.json.JsonUtils.java

public static String serializePrettyPrint(final Object obj) {
    Assert.notNull(obj, "obj is required");
    final ByteArrayOutputStream bos = new ByteArrayOutputStream(512);
    try {//from w ww.  j a  va  2 s .  c om
        JsonUtils.prettyPrintobjectMapper.writeValue(bos, obj);
        return bos.toString("UTF-8");
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:spark.protocol.SparqlCall.java

/** Logs a request, executes it, and dumps the response to the logger. For development use only. */
@SuppressWarnings("unused")
private static final void dump(HttpClient client, HttpUriRequest req) {
    if (logger.isTraceEnabled()) {
        StringBuilder sb = new StringBuilder("\n=== Request ===");
        sb.append("\n").append(req.getRequestLine());
        for (Header h : req.getAllHeaders()) {
            sb.append("\n").append(h.getName()).append(": ").append(h.getValue());
        }//from   w  w  w. j  a v  a 2  s .  c o m
        logger.trace(sb.toString());

        HttpResponse resp = null;
        try {
            resp = client.execute(req);
        } catch (Exception e) {
            logger.trace("Error executing request", e);
            return;
        }

        sb = new StringBuilder("\n=== Response ===");
        sb.append("\n").append(resp.getStatusLine());
        for (Header h : resp.getAllHeaders()) {
            sb.append("\n").append(h.getName()).append(": ").append(h.getValue());
        }
        logger.trace(sb.toString());

        HttpEntity entity = resp.getEntity();
        if (entity != null) {
            sb = new StringBuilder("\n=== Content ===");
            try {
                int len = (int) entity.getContentLength();
                if (len < 0)
                    len = 100;
                ByteArrayOutputStream baos = new ByteArrayOutputStream(len);
                entity.writeTo(baos);
                sb.append("\n").append(baos.toString("UTF-8"));
                logger.trace(sb.toString());
            } catch (IOException e) {
                logger.trace("Error reading content", e);
            }
        }
    }
}

From source file:com.discovery.darchrow.io.SerializableUtil.java

/**
 * To string.//from   www .j a  v  a2s .co m
 *
 * @param serializable
 *            the serializable
 * @return the string
 * @see #toByteArrayOutputStream(Serializable)
 * @deprecated 
 */
//TODO
@Deprecated
public static String toString(Serializable serializable) {
    ByteArrayOutputStream byteArrayOutputStream = null;
    try {
        byteArrayOutputStream = toByteArrayOutputStream(serializable);

        String serializableString = byteArrayOutputStream.toString(CharsetType.ISO_8859_1);
        serializableString = java.net.URLEncoder.encode(serializableString, CharsetType.UTF8);

        return serializableString;
    } catch (IOException e) {
        LOGGER.error("", e);
        throw new SerializationException(e);
    } finally {
        IOUtils.closeQuietly(byteArrayOutputStream);
    }
}

From source file:com.sap.prd.mobile.ios.mios.XCodeVersionUtil.java

public static String getXCodeVersionString() throws XCodeException {
    PrintStream out = null;/*from  w ww  . ja  va  2s. c  om*/
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        out = new PrintStream(bos, true, Charset.defaultCharset().name());
        int exitCode = Forker.forkProcess(out, new File("."), new String[] { "xcodebuild", "-version" });
        if (exitCode == 0) {
            return bos.toString(Charset.defaultCharset().name());
        } else {
            throw new XCodeException("Could not get xcodebuild version (exit code = " + exitCode + ")");
        }
    } catch (Exception e) {
        throw new XCodeException("Could not get xcodebuild version");
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:com.kixeye.chassis.transport.serde.SerDeTest.java

private static void dumpBytes(byte[] bytes) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    HexDump.dump(bytes, 0, baos, 0);//from w  w  w. ja v a2  s. c  o  m
    logger.info("Serialized object to: \n{}", baos.toString(Charsets.UTF_8.name()).trim());
}

From source file:Main.java

public static String objectSerializableToString(Serializable object) {
    ByteArrayOutputStream baos = null;
    ObjectOutputStream os = null;
    try {/*from   w ww.  ja  v  a  2  s . c  o m*/
        if (object != null) {
            baos = new ByteArrayOutputStream();
            os = new ObjectOutputStream(baos);
            os.writeObject(object);
            os.flush();
            baos.flush();
            return baos.toString("UTF-8");
        }
    } catch (Throwable e) {
    } finally {
        try {
            if (os != null) {
                os.close();
            }
        } catch (Throwable e2) {
        }

        try {
            if (baos != null) {
                baos.close();
            }
        } catch (Throwable e2) {
        }
    }
    return null;
}

From source file:Main.java

/**
 * The method read XML from a file, skips irrelevant chars and serves back the content as string. 
 * @param inputStream//  ww w . j a va 2s  . c o  m
 * @return String : content of a file 
 * @throws IOException
 */
public static String getFileToString(Context context, String xml_file) throws IOException {
    InputStream inputStream = context.getAssets().open(xml_file);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    try {
        int i = inputStream.read();
        while (i != -1) {
            if (i != TAB && i != NL && i != CR)
                byteArrayOutputStream.write(i);
            i = inputStream.read();
        }
        return byteArrayOutputStream.toString("UTF-8");

    } catch (IOException e) {
        e.printStackTrace();
        return "";

    } finally {
        inputStream.close();
    }
}

From source file:Main.java

/**
 * The method read a XML from URL, skips irrelevant chars and serves back the content as string. 
 * @param inputStream/*w ww . j  av a 2  s  .  c  o  m*/
 * @return String : content of a file 
 * @throws IOException
 */
public static String getURLToString(URL url) throws IOException {
    InputStreamReader inputStream = new InputStreamReader(url.openStream(), "ISO-8859-1");
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try {
        int i = inputStream.read();
        while (i != -1) {
            if (i != TAB && i != NL && i != CR)
                byteArrayOutputStream.write(i);
            i = inputStream.read();
        }

        String x = byteArrayOutputStream.toString("UTF-8");
        return x;

    } catch (IOException e) {
        e.printStackTrace();
        return "";

    } finally {
        inputStream.close();
    }
}

From source file:com.sap.prd.mobile.ios.mios.CodeSignManager.java

private static ExecResult exec(String[] cmd) throws IOException {
    String cmdStr = StringUtils.join(cmd, " ");
    System.out.println("Invoking " + cmdStr);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos, true, "UTF-8");
    try {//from   w w w. j av a2s.  c o  m
        int exitValue = Forker.forkProcess(ps, null, "bash", "-c", cmdStr);
        return new ExecResult(cmdStr, baos.toString("UTF-8"), exitValue);
    } finally {
        ps.close();
    }
}

From source file:com.lenovo.tensorhusky.common.utils.ReflectionUtils.java

/**
 * Log the current thread stacks at INFO level.
 *
 * @param log         the logger that logs the stack trace
 * @param title       a descriptive title for the call stacks
 * @param minInterval the minimum time from the last
 *///w  ww .  jav a 2s .  c  om
public static void logThreadInfo(Log log, String title, long minInterval) {
    boolean dumpStack = false;
    if (log.isInfoEnabled()) {
        synchronized (ReflectionUtils.class) {
            long now = clock.getTime();
            if (now - previousLogTime >= minInterval * 1000) {
                previousLogTime = now;
                dumpStack = true;
            }
        }
        if (dumpStack) {
            try {
                ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                printThreadInfo(new PrintStream(buffer, false, "UTF-8"), title);
                log.info(buffer.toString(Charset.defaultCharset().name()));
            } catch (UnsupportedEncodingException ignored) {
            }
        }
    }
}