Java InputStream Read by Charset streamToString(final InputStream is, final Charset charset)

Here you can find the source of streamToString(final InputStream is, final Charset charset)

Description

stream To String

License

Open Source License

Declaration

public static String streamToString(final InputStream is,
            final Charset charset) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2018 Arrow Electronics, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License 2.0
 * which accompanies this distribution, and is available at
 * http://apache.org/licenses/LICENSE-2.0
 *
 * Contributors://from w w  w.j  ava2 s  .  c  om
 *     Arrow Electronics, Inc.
 *******************************************************************************/

import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;

import java.nio.charset.Charset;

public class Main {
    public static String streamToString(final InputStream is,
            final Charset charset) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        while ((length = is.read(buffer)) != -1) {
            bos.write(buffer, 0, length);
        }
        return bos.toString(charset.name());
    }
}

Related

  1. lines(InputStream in, Charset cs)
  2. loadInputStream(InputStream in, Charset cs)
  3. processSubstitute(CharBuffer cb, String replacement, boolean endOfInput, String outputCharset, OutputStream os)
  4. stream2Bytes(InputStream is, Charset charset)
  5. streamString(InputStream in, boolean closeIn, String charset)
  6. streamToString(InputStream inputStream, Charset encoding)
  7. stringFromStream(InputStream in, Charset cs)
  8. toInputStream(final String string, final Charset charset)
  9. toInputStream(String input, Charset charset)