From: <no...@us...> - 2003-07-19 13:49:53
|
Log Message: ----------- Removed dependency on deprecated class StringBufferInputStream Modified Files: -------------- /cvsroot/htmlunit/htmlunit/src/test/java/com/gargoylesoftware/htmlunit: TextUtilTest.java /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit: TextUtil.java Revision Data ------------- Index: TextUtilTest.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/TextUtilTest.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- TextUtilTest.java 10 Jun 2003 11:57:00 -0000 1.3 +++ TextUtilTest.java 19 Jul 2003 13:49:47 -0000 1.4 @@ -37,6 +37,10 @@ */ package com.gargoylesoftware.htmlunit; +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; + /** * Tests for TextUtil. * @@ -128,6 +132,36 @@ assertFalse( "stringToCheck=["+stringToCheck+"] prefix=["+prefix+"]", TextUtil.startsWithIgnoreCase(stringToCheck, prefix)); + } + } + + public void testToInputStream_null() throws Exception { + try { + TextUtil.toInputStream(null); + fail("Expected NullPointerException"); + } + catch( final NullPointerException e ) { + // Expected path + } + } + + + public void testToInputStream() throws Exception { + final String[][] data = { + {"", null}, + {"a", "a"}, + {"abcdefABCDEF", "abcdefABCDEF"}, + }; + final String encoding = "ISO-8859-1"; + + for( int i=0; i<data.length; i++ ) { + final String input = data[i][0]; + final String expectedResult = data[i][1]; + + final InputStream inputStream = TextUtil.toInputStream(input, encoding); + final String actualResult + = new BufferedReader( new InputStreamReader(inputStream, encoding) ).readLine(); + assertEquals( expectedResult, actualResult); } } } Index: TextUtil.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/TextUtil.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- TextUtil.java 10 Jun 2003 11:56:56 -0000 1.9 +++ TextUtil.java 19 Jul 2003 13:49:47 -0000 1.10 @@ -37,7 +37,12 @@ */ package com.gargoylesoftware.htmlunit; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.InputStream; +import java.io.OutputStreamWriter; +import java.io.UnsupportedEncodingException; /** * Utility methods relating to text. @@ -80,6 +85,43 @@ * @return The resulting input stream. */ public static InputStream toInputStream( final String content ) { - return new java.io.StringBufferInputStream(content); + try { + return toInputStream(content, "ISO-8859-1"); + } + catch( final UnsupportedEncodingException e ) { + throw new IllegalStateException( + "ISO-8859-1 is an unsupported encoding! You may have a corrupted installation of java."); + } + } + + /** + * Convert a string into an input stream. + * @param content The string + * @return The resulting input stream. + */ + public static InputStream toInputStream( + final String content, + final String encoding ) + throws + UnsupportedEncodingException { + + try { + final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(content.length()*2); + final OutputStreamWriter writer = new OutputStreamWriter(byteArrayOutputStream, encoding); + writer.write(content); + writer.flush(); + + final byte[] byteArray = byteArrayOutputStream.toByteArray(); + return new ByteArrayInputStream(byteArray); + } + catch( final UnsupportedEncodingException e ) { + throw e; + } + catch( final IOException e ) { + // Theoretically impossible since all the "IO" is in memory but it's a + // checked exception so we have to catch it. + e.printStackTrace(); + throw new IllegalStateException("Exception when converting a string to an input stream: "+e); + } } } |