
com.undefinedlabs.scope.utils.ScopeIOUtils Maven / Gradle / Ivy
package com.undefinedlabs.scope.utils;
import com.undefinedlabs.scope.logger.ScopeLogger;
import com.undefinedlabs.scope.logger.ScopeLoggerResolver;
import com.undefinedlabs.scope.settings.ScopeSettings;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.apache.commons.lang3.StringUtils;
public class ScopeIOUtils {
private static final ScopeLogger LOGGER = ScopeLoggerResolver.INSTANCE.get();
public static String getSubstring(final InputStream byteStream) {
return getSubstring(byteStream, ScopeSettings.HTTP_MAX_PAYLOAD_CHARS, StandardCharsets.UTF_8);
}
public static String getSubstring(
final InputStream byteStream, final int maxChars, final Charset charset) {
try {
byteStream.mark(maxChars);
final byte[] buf = new byte[maxChars];
final int length = byteStream.read(buf);
final String string = (length > 0) ? new String(buf, 0, length, charset) : "";
return (StringUtils.isNotEmpty(string)) ? StringUtils.abbreviate(string, maxChars) : "";
} catch (final IOException e) {
LOGGER.error("Could not be possible to get data from InputStream. " + e.getMessage());
return "";
} finally {
try {
byteStream.reset();
} catch (final IOException e) {
LOGGER.debug("Could not be possible to reset the InputStream. " + e.getMessage());
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy