[Jsptest-svn-commits] SF.net SVN: jsptest:[254] trunk/jsptest-generic/jsptest-common/src
Status: Alpha
Brought to you by:
lkoskela
From: <lko...@us...> - 2009-11-12 23:48:51
|
Revision: 254 http://jsptest.svn.sourceforge.net/jsptest/?rev=254&view=rev Author: lkoskela Date: 2009-11-12 23:48:30 +0000 (Thu, 12 Nov 2009) Log Message: ----------- Added IO.append Modified Paths: -------------- trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/IO.java trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/utils/IOTest.java Modified: trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/IO.java =================================================================== --- trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/IO.java 2008-11-19 13:06:40 UTC (rev 253) +++ trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/IO.java 2009-11-12 23:48:30 UTC (rev 254) @@ -23,39 +23,46 @@ import java.io.InputStream; /** - * The <tt>IO</tt> class provides utility methods for handling I/O related tasks such as reading - * from and writing to files, streams, and writers. + * The <tt>IO</tt> class provides utility methods for handling I/O related tasks + * such as reading from and writing to files, streams, and writers. * * @author Lasse Koskela */ public class IO { - public static byte[] readToByteArray(File file) throws IOException { - return readToByteArray(new FileInputStream(file)); - } + public static byte[] readToByteArray(File file) throws IOException { + return readToByteArray(new FileInputStream(file)); + } - public static byte[] readToByteArray(InputStream in) throws IOException { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - byte[] buffer = new byte[8096]; - int r = -1; - while ((r = in.read(buffer)) != -1) { - out.write(buffer, 0, r); - } - in.close(); - return out.toByteArray(); - } + public static byte[] readToByteArray(InputStream in) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buffer = new byte[8096]; + int r = -1; + while ((r = in.read(buffer)) != -1) { + out.write(buffer, 0, r); + } + in.close(); + return out.toByteArray(); + } - public static void write(String what, File to) throws IOException { - FileWriter fw = new FileWriter(to, false); - fw.write(what); - fw.close(); - } + public static void write(String what, File to) throws IOException { + write(what, new FileWriter(to, false)); + } - public static String readToString(File file) throws IOException { - return new String(readToByteArray(file)); - } + public static void append(String what, File to) throws IOException { + write(what, new FileWriter(to, true)); + } - public static String readToString(InputStream stream) throws IOException { - return new String(readToByteArray(stream)); - } + private static void write(String what, FileWriter to) throws IOException { + to.write(what); + to.close(); + } + + public static String readToString(File file) throws IOException { + return new String(readToByteArray(file)); + } + + public static String readToString(InputStream stream) throws IOException { + return new String(readToByteArray(stream)); + } } Modified: trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/utils/IOTest.java =================================================================== --- trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/utils/IOTest.java 2008-11-19 13:06:40 UTC (rev 253) +++ trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/utils/IOTest.java 2009-11-12 23:48:30 UTC (rev 254) @@ -23,4 +23,10 @@ IO.write(content, file); assertEquals(content, new String(IO.readToByteArray(file))); } + + public void testAppendingToFile() throws Exception { + IO.append("one", file); + IO.append("two", file); + assertEquals("onetwo", new String(IO.readToByteArray(file))); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |