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