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:Main.java

public static String transformDOMToString(DOMSource source) {
    try {//from w  ww  .  ja  va2  s  . co m
        // Use a Transformer for output
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        ByteArrayOutputStream sos = new ByteArrayOutputStream();
        StreamResult result = new StreamResult(sos);
        transformer.transform(source, result);
        return sos.toString();
    } catch (TransformerException e) {
        throw new IllegalArgumentException(e);
    }

}

From source file:Main.java

/**
 * @param context/* w ww  .  j a  va  2  s  .  c o m*/
 * @param filePath file path relative to assets, like request_init1/search_index.json
 * @return
 */
public static String readAssert(Context context, String filePath) {
    try {
        if (filePath.startsWith(File.separator)) {
            filePath = filePath.substring(File.separator.length());
        }
        AssetManager assetManager = context.getAssets();
        InputStream inputStream = assetManager.open(filePath);
        DataInputStream stream = new DataInputStream(inputStream);
        int length = stream.available();
        byte[] buffer = new byte[length];
        stream.readFully(buffer);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byteArrayOutputStream.write(buffer);
        stream.close();
        return byteArrayOutputStream.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.spotify.echoprintserver.nativelib.Util.java

/**
 * Decode an Echoprint string into its list of codes (temporal offsets are not returned).
 *//* w ww .  j  a v  a  2 s  . c om*/
public static List<Integer> decodeEchoprintString(String echoprintString) throws IOException {

    byte[] decoded = b64SafeDecode(echoprintString);

    DeflateCompressorInputStream is = new DeflateCompressorInputStream(new ByteArrayInputStream(decoded));

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    IOUtils.copy(is, os);
    String decodedEchoprintString = os.toString();

    // skip the first half of the string (offsets)
    // take every 5 chars, parse int in base 16

    List<Integer> codes = new ArrayList<Integer>();

    int N = decodedEchoprintString.length();
    for (int n = N / 2; n < N; n += 5) {
        String s = decodedEchoprintString.substring(n, n + 5);
        Integer c = Integer.parseInt(s, 16);
        codes.add(c);
    }

    return codes;

}

From source file:Main.java

public static String inputStreamToString(InputStream inputStream) {
    if (inputStream == null) {
        System.out.println("InputStream is null");
        return null;
    }// w w w  . j ava  2s  .  c  o m

    try {
        int i = -1;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((i = inputStream.read()) != -1) {
            baos.write(i);
        }
        String content = baos.toString();
        return content;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String readString(String filePath) {
    File file = new File(filePath);
    if (!file.exists())
        return null;

    FileInputStream fileInput = null;
    FileChannel channel = null;//from w w  w  . ja  v a2  s .c o m
    try {
        fileInput = new FileInputStream(filePath);
        channel = fileInput.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
        channel.read(buffer);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byteArrayOutputStream.write(buffer.array());
        return byteArrayOutputStream.toString();
    } catch (Exception e) {
    } finally {

        if (fileInput != null) {
            try {
                fileInput.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (channel != null) {
            try {
                channel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:Main.java

public static String readStream(InputStream is) {
    try {//from ww w  . j av a2  s  .c  o m
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        int i = is.read();
        while (i != -1) {
            bo.write(i);
            i = is.read();
        }
        return bo.toString();
    } catch (IOException e) {
        return "";
    }
}

From source file:Main.java

private static String readStream(InputStream is) {
    try {//from w  w  w  .j  a  v  a 2  s. c  o m
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        int i = is.read();
        while (i != -1) {
            bo.write(i);
            i = is.read();
        }
        return bo.toString();
    } catch (IOException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public synchronized static String writeXmlToString(Document doc)
        throws TransformerFactoryConfigurationError, TransformerException {
    if (doc != null) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        OutputStreamWriter writer = new OutputStreamWriter(output);
        writeXml(doc, writer, null);//from  w w w.  j a va2s.  c o  m
        return output.toString();
    }
    return null;
}

From source file:Main.java

public static String documentToString(Document doc)
        throws TransformerConfigurationException, TransformerException, IOException {
    //return doc.getDocumentElement().toString();

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");

    DOMSource src = new DOMSource(doc);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*from  w w w . jav a  2 s. co  m*/
        StreamResult sr = new StreamResult(baos);
        transformer.transform(src, sr);

        String result = baos.toString();
        return result;
    } finally {
        baos.close();
    }
}

From source file:Main.java

public static String getErrorReport(String message, Throwable e) {
    if (message != null && message.length() > 0) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        e.printStackTrace(new PrintStream(byteArrayOutputStream));
        return String.format(ERROR_REPORT, message, byteArrayOutputStream.toString());
    } else {//from www  . j  ava2s .co m
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        e.printStackTrace(new PrintStream(byteArrayOutputStream));
        return byteArrayOutputStream.toString();
    }

}