Java Utililty Methods InputStream Read by Charset

List of utility methods to do InputStream Read by Charset

Description

The list of methods to do InputStream Read by Charset are organized into topic(s).

Method

StringinputstreamToString(InputStream input, CharsetDecoder decoder)
Reads an input stream into a string.
CharsetDecoder charsetDecoder = decoder;
if (decoder == null) {
    charsetDecoder = Charset.defaultCharset().newDecoder();
BufferedReader reader = new BufferedReader(new InputStreamReader(input, charsetDecoder));
StringBuilder stringBuffer = new StringBuilder();
String line = reader.readLine();
while (line != null) {
...
StringinputStreamToString(InputStream inputStream, Charset charset)
Convert an InputStream into a String.
if (inputStream != null && inputStream.available() != -1) {
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    while ((length = inputStream.read(buffer)) != -1) {
        result.write(buffer, 0, length);
    if (charset != null) {
...
StringinputStreamToString(InputStream inputStream, String charset)
input Stream To String
return inputStreamToString(inputStream, Charset.forName(charset));
StringinputStreamToString(InputStream is, Charset charset)
input Stream To String
if (is == null) {
    throw new IllegalArgumentException("input stream MUST NOT be null");
if (charset == null) {
    throw new IllegalArgumentException("charset MUST NOT be null");
ByteArrayOutputStream result = new ByteArrayOutputStream();
int readLen;
...
booleanisCharsetMisInterpreted(String input, String encoding)
is Charset Mis Interpreted
CharsetDecoder decoder = Charset.forName("ascii").newDecoder();
CharsetEncoder encoder = Charset.forName(encoding).newEncoder();
ByteBuffer tmp;
try {
    tmp = encoder.encode(CharBuffer.wrap(input));
} catch (CharacterCodingException e) {
    return false;
try {
    decoder.decode(tmp);
    return true;
} catch (CharacterCodingException e) {
    return false;
Streamlines(InputStream in, Charset cs)
Read all lines lazily from an InputStream as a Stream .
InputStreamReader reader = new InputStreamReader(in, cs);
BufferedReader br = new BufferedReader(reader);
try {
    return br.lines().onClose(asUncheckedRunnable(br));
} catch (Error | RuntimeException e) {
    try {
        br.close();
    } catch (IOException ex) {
...
StringloadInputStream(InputStream in, Charset cs)
Load an input stream
BufferedInputStream buffIn = null;
InputStreamReader reader = null;
try {
    buffIn = new BufferedInputStream(in);
    reader = new InputStreamReader(buffIn, cs);
    StringWriter dest = new StringWriter();
    int ch;
    while ((ch = reader.read()) >= 0) {
...
booleanprocessSubstitute(CharBuffer cb, String replacement, boolean endOfInput, String outputCharset, OutputStream os)
Scan the characters in the buffer and do replacement.
int remaining = cb.remaining();
if (remaining == 0) {
    cb.clear();
    return false;
String result = SEARCH_PATTERN.matcher(cb).replaceFirst(replacement);
cb.clear();
if (result.length() != remaining) {
...
Stringstream2Bytes(InputStream is, Charset charset)
stream Bytes
String result = null;
try {
    int total = is.available();
    byte[] bs = new byte[total];
    is.read(bs);
    result = new String(bs, charset.name());
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
...
StringstreamString(InputStream in, boolean closeIn, String charset)
stream String
return new String(streamBytes(in, closeIn), Charset.forName(charset));