commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/io/src/java/org/apache/commons/io FileUtils.java
Date Thu, 15 Jul 2004 08:21:14 GMT
scolebourne    2004/07/15 01:21:14

  Modified:    io/src/test/org/apache/commons/io FileUtilsTestCase.java
               io/src/java/org/apache/commons/io FileUtils.java
  Log:
  FileUtils.read/write byte array methods
  
  Revision  Changes    Path
  1.21      +43 -3     jakarta-commons/io/src/test/org/apache/commons/io/FileUtilsTestCase.java
  
  Index: FileUtilsTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/io/src/test/org/apache/commons/io/FileUtilsTestCase.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- FileUtilsTestCase.java	3 Jul 2004 11:24:49 -0000	1.20
  +++ FileUtilsTestCase.java	15 Jul 2004 08:21:14 -0000	1.21
  @@ -22,12 +22,12 @@
   import java.io.OutputStream;
   import java.net.URL;
   
  -import org.apache.commons.io.testtools.FileBasedTestCase;
  -
   import junit.framework.Test;
   import junit.framework.TestSuite;
   import junit.textui.TestRunner;
   
  +import org.apache.commons.io.testtools.FileBasedTestCase;
  +
   /**
    * This is used to test FileUtils for correctness.
    *
  @@ -465,6 +465,46 @@
           FileUtils.touch(file) ;
           assertEquals("FileUtils.touch() didn't empty the file.", 1, file.length());
           assertFalse("FileUtils.touch() changed lastModified.", 0 == file.lastModified())
;        
  +    }
  +
  +    public void testReadFileToString() throws Exception {
  +        File file = new File(getTestDirectory(), "read.obj");
  +        FileOutputStream out = new FileOutputStream(file);
  +        byte[] text = "Hello /u1234".getBytes("UTF8");
  +        out.write(text);
  +        out.close();
  +        
  +        String data = FileUtils.readFileToString(file, "UTF8");
  +        assertEquals("Hello /u1234", data);
  +    }
  +
  +    public void testReadFileToByteArray() throws Exception {
  +        File file = new File(getTestDirectory(), "read.txt");
  +        FileOutputStream out = new FileOutputStream(file);
  +        out.write(11);
  +        out.write(21);
  +        out.write(31);
  +        out.close();
  +        
  +        byte[] data = FileUtils.readFileToByteArray(file);
  +        assertEquals(3, data.length);
  +        assertEquals(11, data[0]);
  +        assertEquals(21, data[1]);
  +        assertEquals(31, data[2]);
  +    }
  +
  +    public void testWriteStringToFile() throws Exception {
  +        File file = new File(getTestDirectory(), "write.txt");
  +        FileUtils.writeStringToFile(file, "Hello /u1234", "UTF8");
  +        byte[] text = "Hello /u1234".getBytes("UTF8");
  +        assertEqualContent(text, file);
  +    }
  +
  +    public void testWriteByteArrayToFile() throws Exception {
  +        File file = new File(getTestDirectory(), "write.obj");
  +        byte[] data = new byte[] {11, 21, 31};
  +        FileUtils.writeByteArrayToFile(file, data);
  +        assertEqualContent(data, file);
       }
   
   }
  
  
  
  1.34      +48 -12    jakarta-commons/io/src/java/org/apache/commons/io/FileUtils.java
  
  Index: FileUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/FileUtils.java,v
  retrieving revision 1.33
  retrieving revision 1.34
  diff -u -r1.33 -r1.34
  --- FileUtils.java	3 Jul 2004 11:20:45 -0000	1.33
  +++ FileUtils.java	15 Jul 2004 08:21:14 -0000	1.34
  @@ -121,7 +121,7 @@
        * @throws IOException If an I/O problem occurs
        */
       public static void touch(File file) throws IOException {
  -        OutputStream out = new java.io.FileOutputStream(file, true);
  +        OutputStream out = new FileOutputStream(file, true);
           IOUtils.closeQuietly(out);
           file.setLastModified(System.currentTimeMillis());
       }
  @@ -271,8 +271,8 @@
           InputStream input1 = null;
           InputStream input2 = null;
           try {
  -            input1 = new java.io.FileInputStream(file1);
  -            input2 = new java.io.FileInputStream(file2);
  +            input1 = new FileInputStream(file1);
  +            input2 = new FileInputStream(file2);
               return IOUtils.contentEquals(input1, input2);
   
           } finally {
  @@ -574,16 +574,15 @@
        * in inconsistent results.
        * </p>
        *
  -     * @param file the file to read.
  -     * @param encoding the encoding to use
  +     * @param file  the file to read
  +     * @param encoding  the encoding to use
        * @return The file contents or null if read failed.
        * @throws IOException in case of an I/O error
  -     * @throws UnsupportedEncodingException if the encoding is not supported
  -     *   by the VM
  +     * @throws UnsupportedEncodingException if the encoding is not supported by the VM
        */
       public static String readFileToString(
               File file, String encoding) throws IOException {
  -        InputStream in = new java.io.FileInputStream(file);
  +        InputStream in = new FileInputStream(file);
           try {
               return IOUtils.toString(in, encoding);
           } finally {
  @@ -593,10 +592,28 @@
   
       /**
        * <p>
  -     * Writes data to a file. The file will be created if it does not exist.
  +     * Reads the contents of a file into a byte array.
        * </p>
  +     *
  +     * @param file  the file to read
  +     * @return The file contents or null if read failed.
  +     * @throws IOException in case of an I/O error
  +     */
  +    public static byte[] readFileToByteArray(File file) throws IOException {
  +        InputStream in = new FileInputStream(file);
  +        try {
  +            return IOUtils.toByteArray(in);
  +        } finally {
  +            IOUtils.closeQuietly(in);
  +        }
  +    }
  +
  +    /**
        * <p>
  -     * There is no readFileToString method without encoding parameter because
  +     * Writes a String to a file creating the file if it does not exist.
  +     * </p>
  +     * <p>
  +     * There is no writeStringToFile method without encoding parameter because
        * the default encoding can differ between platforms and therefore results
        * in inconsistent results.
        * </p>
  @@ -610,9 +627,28 @@
        */
       public static void writeStringToFile(File file, 
               String data, String encoding) throws IOException {
  -        OutputStream out = new java.io.FileOutputStream(file);
  +        OutputStream out = new FileOutputStream(file);
           try {
               out.write(data.getBytes(encoding));
  +        } finally {
  +            IOUtils.closeQuietly(out);
  +        }
  +    }
  +
  +    /**
  +     * <p>
  +     * Writes a byte array to a file creating the file if it does not exist.
  +     * </p>
  +     *
  +     * @param file  the file to write to
  +     * @param data  the content to write to the file
  +     * @throws IOException in case of an I/O error
  +     */
  +    public static void writeByteArrayToFile(
  +            File file, byte[] data) throws IOException {
  +        OutputStream out = new FileOutputStream(file);
  +        try {
  +            out.write(data);
           } finally {
               IOUtils.closeQuietly(out);
           }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message