
com.chavaillaz.client.common.utility.Utils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-client Show documentation
Show all versions of common-client Show documentation
Library for the creation of asynchronous clients supporting multiple HTTP clients
package com.chavaillaz.client.common.utility;
import static java.net.URLEncoder.encode;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.joining;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import com.chavaillaz.client.common.security.Authentication;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.tuple.Pair;
/**
* General utilities for all clients.
*/
@UtilityClass
public class Utils {
/**
* Gets the content of an {@link InputStream} and convert it to a {@link String}.
* Uses {@link StandardCharsets#UTF_8} as charset.
*
* @param inputStream The input stream to read
* @return The {@link String} representing the content of the input stream
* @throws RuntimeException If some I/O error occurs when reading the input stream
*/
public static String readInputStream(InputStream inputStream) {
return readInputStream(inputStream, StandardCharsets.UTF_8);
}
/**
* Gets the content of an {@link InputStream} and convert it to a {@link String}.
*
* @param inputStream The input stream to read
* @param charset The charset to use to read the input stream
* @return The {@link String} representing the content of the input stream
* @throws RuntimeException If some I/O error occurs when reading the input stream
*/
@SneakyThrows
public static String readInputStream(InputStream inputStream, Charset charset) {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int length; (length = inputStream.read(buffer)) != -1; ) {
result.write(buffer, 0, length);
}
return result.toString(charset);
}
/**
* Creates a query representing the given data.
* For example, {@code Map.of("border", "orange", "background", "black")}
* will be {@code border=orange&background=black}.
*
* @param data The data to format
* @return the corresponding {@link String}
*/
public static String encodeQuery(Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy