Java InputStream Read by Charset stream2Bytes(InputStream is, Charset charset)

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

Description

stream Bytes

License

Open Source License

Declaration

public static String stream2Bytes(InputStream is, Charset charset) 

Method Source Code

//package com.java2s;
/*/*from  w w  w.j  av  a  2s. c om*/
 * Copyright 2013-2023 "Peng Li"<aqnote@qq.com>
 * Licensed under the AQNote License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.aqnote.com/licenses/LICENSE-1.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

import java.io.UnsupportedEncodingException;

import java.nio.charset.Charset;

public class Main {
    public static byte[] stream2Bytes(InputStream is) throws IOException {
        int total = is.available();
        byte[] bs = new byte[total];
        is.read(bs);
        return bs;
    }

    public static String stream2Bytes(InputStream is, Charset charset) {
        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();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}

Related

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