Here you can find the source of stream2Bytes(InputStream is, Charset charset)
public static String stream2Bytes(InputStream is, Charset charset)
//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; } }