May 3, 2020
Estimated Post Reading Time ~

Converting InputStream to String

private String convertToString(InputStream inputStreamObj)
            throws IOException {
        if (inputStreamObj != null) {
            Writer writer = new StringWriter();
            char[] buffer = new char[1024];
            try {
                Reader reader = new BufferedReader(new InputStreamReader(
                        inputStreamObj, "UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                inputStreamObj.close();
            }
            return writer.toString();
        } else {
            return "";
        }
    }


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.