it.bz.opendatahub.alpinebits.xml.StreamConverter Maven / Gradle / Ivy
The newest version!
// SPDX-FileCopyrightText: NOI Techpark
//
// SPDX-License-Identifier: MPL-2.0
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package it.bz.opendatahub.alpinebits.xml;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
/**
* This class contains helper functions to convert different stream types.
*/
public final class StreamConverter {
private StreamConverter() {
// Empty
}
/**
* Convert the given {@link InputStream} to a String.
*
* @param is The InputStream to convert.
* @return The content of InputStream writtern to String.
* @throws IOException If the conversion fails.
*/
public static String readToString(InputStream is) throws IOException {
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
for (int result = bis.read(); result != -1; result = bis.read()) {
buf.write((byte) result);
}
return buf.toString(StandardCharsets.UTF_8.name());
}
}