First, a few criticisms regarding the approach you have already taken. You should not unnecessarily use NIO CharBuffer when you want just char[512] . You also do not need to clear buffer each iteration.
int numBytes; final char[] buf = new char[512]; while ((numBytes = i.read(buf)) != -1) { text += String.copyValueOf(buf, 0, numBytes); }
You should also know that only building a String with these arguments will have the same effect as the constructor also copies the data.
The contents of the subarray are copied; subsequent modification of the character array does not affect the newly created string.
You can use the dynamic ByteArrayOutputStream , which increases the internal buffer to accommodate all the data. Then you can use the whole byte[] from toByteArray to decode to String .
The advantage is that decoding decoding to the end allows you to avoid fragments of decoding individually; while this may work for simple encodings such as ASCII or ISO-8859-1, it will not work on multibyte schemes such as UTF-8 and UTF-16. This means that in the future it is easier to change the character encoding, since the code does not require changes.
private static final String DEFAULT_ENCODING = "ISO-8859-1"; public static final String convert(final InputStream in) throws IOException { return convert(in, DEFAULT_ENCODING); } public static final String convert(final InputStream in, final String encoding) throws IOException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final byte[] buf = new byte[2048]; int rd; while ((rd = in.read(buf, 0, 2048) >= 0) { out.write(buf, 0, rd); } return new String(out.toByteArray(), 0, encoding); }
oldrinb
source share