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

StringasString(final InputStream is, Charset charset)
Reads the provided input stream as a string and then closes the stream.
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset))) {
    final String firstLine = reader.readLine();
    if (firstLine == null) {
        return "";
    final StringBuilder builder = new StringBuilder(firstLine);
    for (String line = reader.readLine(); line != null; line = reader.readLine()) {
        builder.append(EOL);
...
StringcollectStream(InputStream stream, Charset charset)
Gets the contents of the InputStream as a String of given Charset.
StringBuilder textBuilder = new StringBuilder();
try (Reader reader = new BufferedReader(new InputStreamReader(stream, charset))) {
    int c = reader.read();
    while (c != -1) {
        textBuilder.append((char) c);
        c = reader.read();
return textBuilder.toString();
Stringconsume(InputStream stream, Charset encoding)
consume
final char[] buffer = new char[0x10000];
final StringBuilder out = new StringBuilder();
final Reader in = new InputStreamReader(stream, encoding);
int read;
do {
    read = in.read(buffer, 0, buffer.length);
    if (read > 0)
        out.append(buffer, 0, read);
...
byte[]convertEncoding(Charset output_charset, String input_string)
convert Encoding
CharsetEncoder encoder = output_charset.newEncoder();
CharBuffer charBuffer = CharBuffer.wrap(input_string);
ByteBuffer byteBuffer = encoder.encode(charBuffer);
byte[] buffer = new byte[byteBuffer.remaining()];
byteBuffer.get(buffer);
return buffer;
StringconvertFromUnicode(String input, String targetCharset)
convert From Unicode
Charset charset = Charset.forName(targetCharset);
CharsetDecoder decoder = charset.newDecoder();
CharBuffer cbuf = null;
try {
    cbuf = decoder.decode(ByteBuffer.wrap(input.getBytes()));
} catch (CharacterCodingException e) {
return cbuf.toString();
...
StringconvertStreamToString(final InputStream is, final Charset charset)
Converts an input stream into a string value.
final BufferedReader br = new BufferedReader(new InputStreamReader(is, charset));
final StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
    sb.append(line + "\n");
br.close();
return sb.toString();
...
byte[]convertToCharacterSet(byte[] input, Charset fromCharset, Charset toCharSet)
Converts the input text between the two specified character sets.
if (input == null) {
    return null;
CharBuffer decodedData = fromCharset.decode(ByteBuffer.wrap(input));
ByteBuffer encodedData = toCharSet.encode(decodedData);
return Arrays.copyOf(encodedData.array(), encodedData.limit());
StringcopyToString(InputStream in, Charset charset)
Copy the contents of the given InputStream into a String.
StringBuilder out = new StringBuilder();
InputStreamReader reader = new InputStreamReader(in, charset);
char[] buffer = new char[BUFFER_SIZE];
int bytesRead = -1;
while ((bytesRead = reader.read(buffer)) != -1) {
    out.append(buffer, 0, bytesRead);
return out.toString();
...
InputStreamcreateInput(String s, String charsetName)
create Input
Charset cs = Charset.forName(charsetName);
InputStream in = new ByteArrayInputStream(cs.encode(s).array());
return in;
ZipInputStreamcreateZipInputStream(InputStream inStream, Charset charset)
Returns a ZipInputStream opened with a given charset.
if (charset == null)
    return new ZipInputStream(inStream);
try {
    Constructor<ZipInputStream> constructor = ZipInputStream.class
            .getConstructor(new Class[] { InputStream.class, Charset.class });
    return (ZipInputStream) constructor.newInstance(new Object[] { inStream, charset });
} catch (NoSuchMethodException e) {
    throw new IllegalStateException(MISSING_METHOD_PLEASE_UPGRADE, e);
...