### Question:
FileUtil { public static String readContents( InputStream input, String encoding ) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); FileUtil.copyContents( input, out ); ByteBuffer bbuf = ByteBuffer.wrap( out.toByteArray() ); Charset cset = Charset.forName( encoding ); CharsetDecoder csetdecoder = cset.newDecoder(); csetdecoder.onMalformedInput( CodingErrorAction.REPORT ); csetdecoder.onUnmappableCharacter( CodingErrorAction.REPORT ); try { CharBuffer cbuf = csetdecoder.decode( bbuf ); return cbuf.toString(); } catch( CharacterCodingException e ) { Charset latin1 = Charset.forName("ISO-8859-1"); CharsetDecoder l1decoder = latin1.newDecoder(); l1decoder.onMalformedInput( CodingErrorAction.REPORT ); l1decoder.onUnmappableCharacter( CodingErrorAction.REPORT ); try { bbuf = ByteBuffer.wrap( out.toByteArray() ); CharBuffer cbuf = l1decoder.decode( bbuf ); return cbuf.toString(); } catch( CharacterCodingException ex ) { throw (CharacterCodingException) ex.fillInStackTrace(); } } } private  FileUtil(); static File newTmpFile( final String content, final Charset encoding ); static File newTmpFile( final String content ); static String runSimpleCommand( final String command, final String directory ); static void copyContents( final Reader in, final Writer out ); static void copyContents( final InputStream in, final OutputStream out ); static String readContents( InputStream input, String encoding ); static String readContents( final Reader in ); static String getThrowingMethod( Throwable t );  }### Answer:
@Test public void testReadContentsLatin1() throws Exception { String src = "abc\u00e4\u00e5\u00a6"; String res = FileUtil.readContents( new ByteArrayInputStream( src.getBytes(StandardCharsets.ISO_8859_1) ), StandardCharsets.ISO_8859_1.name() ); Assertions.assertEquals( src, res ); }
@Test public void testReadContentsLatin1_2() throws Exception { String src = "abc\u00e4\u00e5\u00a6def"; String res = FileUtil.readContents( new ByteArrayInputStream( src.getBytes(StandardCharsets.ISO_8859_1) ), StandardCharsets.UTF_8.name() ); Assertions.assertEquals( src, res ); }
@Test public void testReadContentsReader() throws IOException { String data = "ABCDEF"; String result = FileUtil.readContents( new StringReader( data ) ); Assertions.assertEquals( data, result ); }