Adapted from com.google.common.io.CharStreams.toString().
(Reader reader)
| 234 | |
| 235 | /** Adapted from {@code com.google.common.io.CharStreams.toString()}. */ |
| 236 | public static String toString(Reader reader) throws IOException { |
| 237 | if (reader == null) { |
| 238 | return null; |
| 239 | } |
| 240 | try { |
| 241 | StringBuilder to = new StringBuilder(); |
| 242 | CharBuffer charBuf = CharBuffer.allocate(BUF_SIZE); |
| 243 | // must cast to super class Buffer otherwise break when running with java 11 |
| 244 | Buffer buf = charBuf; |
| 245 | while (reader.read(charBuf) != -1) { |
| 246 | buf.flip(); |
| 247 | to.append(charBuf); |
| 248 | buf.clear(); |
| 249 | } |
| 250 | return to.toString(); |
| 251 | } finally { |
| 252 | ensureClosed(reader); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /** Adapted from {@code com.google.common.io.ByteStreams.toByteArray()}. */ |
| 257 | public static byte[] toByteArray(InputStream in) throws IOException { |