Java Utililty Methods InputStream Read by Charset

List of utility methods to do InputStream Read by Charset

Description

The list of methods to do InputStream Read by Charset are organized into topic(s).

Method

StringforceEncoding(String inputString, String targetCharset)
Converts a given string to use the given encoding, e.g.
String returnString = "";
Charset charset = Charset.forName(targetCharset);
CharsetDecoder decoder = charset.newDecoder();
CharsetEncoder encoder = charset.newEncoder();
encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
encoder.replaceWith(" ".getBytes());
ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(inputString));
CharBuffer cbuf = decoder.decode(bbuf);
...
StringfromUnicode(String charset, String input)
from Unicode
StringBuffer ret = new StringBuffer();
Matcher matcher = UNICODE_PATTERN.matcher(input);
String error = null;
while (matcher.find()) {
    try {
        String uniValue = matcher.group().substring(2);
        String newValue = new String(new char[] { (char) Integer.parseInt(uniValue, 16) });
        if (charset != null) {
...
StringgetContentFromInputStream(InputStream in, String charset)

get string content form inputStream with given charset

StringBuffer dist = new StringBuffer();
byte[] data = new byte[1024];
int readNum = -1;
try {
    while ((readNum = in.read(data)) != -1) {
        dist.append(new String(data, 0, readNum, charset));
    in.close();
...
longgetHeaderLen(FileInputStream in, Charset encoding)
get Header Len
byte[] expectedHeader = getHeader(encoding);
byte[] magic = new byte[expectedHeader.length];
int read = readGuaranteed(magic, in);
if (read != expectedHeader.length) {
    return 0;
for (int i = 0; i < magic.length; i++) {
    if (magic[i] != expectedHeader[i]) {
...
CharsetgetInputCharset()
get Input Charset
if (inputCharset == null)
    inputCharset = getDefaultCharset();
return inputCharset;
StringgetStreamAsString(InputStream source, Charset charset)
get Stream As String
String result = getReaderAsString(new InputStreamReader(source, charset));
source.close();
return result;
StringgetText(InputStream stream, Charset charset)
Read an InputStream completely, using the specified encoding.
BufferedReader br = null;
try {
    br = new BufferedReader(new InputStreamReader(stream, charset));
    StringWriter sw = new StringWriter();
    int n;
    char[] cbuf = new char[1024];
    while ((n = br.read(cbuf, 0, cbuf.length)) != -1)
        sw.write(cbuf, 0, n);
...
Stringinput2String(InputStream input, Charset encoding)
input String
byte[] bytes = new byte[input.available()];
input.read(bytes);
return new String(bytes, encoding);
StringinputStreamAsString(InputStream stream, Charset cs)
input Stream As String
BufferedReader br = new BufferedReader(new InputStreamReader(stream, cs));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
    sb.append(line).append("\n");
br.close();
return sb.toString();
...
StringinputStreamToString(final InputStream inputStream, final String charsetName)
Read from an java.io.InputStream to a java.lang.String .
final Scanner scanner = new Scanner(inputStream, charsetName).useDelimiter("\\A");
String nextToken = "";
if (scanner.hasNext()) {
    nextToken = scanner.next();
return nextToken;