You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by jd...@apache.org on 2007/07/27 00:42:02 UTC

svn commit: r560022 - in /maven/shared/trunk/maven-shared-io/src: main/java/org/apache/maven/shared/io/location/ test/java/org/apache/maven/shared/io/location/

Author: jdcasey
Date: Thu Jul 26 15:42:00 2007
New Revision: 560022

URL: http://svn.apache.org/viewvc?view=rev&rev=560022
Log: (empty)

Modified:
    maven/shared/trunk/maven-shared-io/src/main/java/org/apache/maven/shared/io/location/FileLocation.java
    maven/shared/trunk/maven-shared-io/src/main/java/org/apache/maven/shared/io/location/Location.java
    maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/FileLocationTest.java

Modified: maven/shared/trunk/maven-shared-io/src/main/java/org/apache/maven/shared/io/location/FileLocation.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-io/src/main/java/org/apache/maven/shared/io/location/FileLocation.java?view=diff&rev=560022&r1=560021&r2=560022
==============================================================================
--- maven/shared/trunk/maven-shared-io/src/main/java/org/apache/maven/shared/io/location/FileLocation.java (original)
+++ maven/shared/trunk/maven-shared-io/src/main/java/org/apache/maven/shared/io/location/FileLocation.java Thu Jul 26 15:42:00 2007
@@ -3,6 +3,7 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
 
@@ -14,13 +15,14 @@
     private File file;
     private FileChannel channel;
     private final String specification;
-    
+    private FileInputStream stream;
+
     public FileLocation( File file, String specification )
     {
         this.file = file;
         this.specification = specification;
     }
-    
+
     protected FileLocation( String specification )
     {
         this.specification = specification;
@@ -28,7 +30,7 @@
 
     public void close()
     {
-        if ( channel != null && channel.isOpen() )
+        if ( ( channel != null ) && channel.isOpen() )
         {
             try
             {
@@ -38,17 +40,29 @@
             {
                 //swallow it.
             }
-        }        
+        }
+
+        if ( stream != null )
+        {
+            try
+            {
+                stream.close();
+            }
+            catch( IOException e )
+            {
+                // swallow it.
+            }
+        }
     }
 
     public File getFile()
         throws IOException
     {
         initFile();
-        
+
         return unsafeGetFile();
     }
-    
+
     protected File unsafeGetFile()
     {
         return file;
@@ -63,14 +77,14 @@
             file = new File( specification );
         }
     }
-    
+
     protected void setFile( File file )
     {
         if ( channel != null )
         {
-            throw new IllegalStateException( "Location is already open; cannot setFile(..)." ); 
+            throw new IllegalStateException( "Location is already open; cannot setFile(..)." );
         }
-        
+
         this.file = file;
     }
 
@@ -82,21 +96,34 @@
     public void open()
         throws IOException
     {
-        initFile();
-        
-        channel = new FileInputStream( file ).getChannel();
+        if ( stream == null )
+        {
+            initFile();
+
+            stream = new FileInputStream( file );
+            channel = stream.getChannel();
+        }
     }
 
     public int read( ByteBuffer buffer )
         throws IOException
     {
+        open();
         return channel.read( buffer );
     }
 
     public int read( byte[] buffer )
         throws IOException
     {
+        open();
         return channel.read( ByteBuffer.wrap( buffer ) );
+    }
+
+    public InputStream getInputStream()
+        throws IOException
+    {
+        open();
+        return stream;
     }
 
 }

Modified: maven/shared/trunk/maven-shared-io/src/main/java/org/apache/maven/shared/io/location/Location.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-io/src/main/java/org/apache/maven/shared/io/location/Location.java?view=diff&rev=560022&r1=560021&r2=560022
==============================================================================
--- maven/shared/trunk/maven-shared-io/src/main/java/org/apache/maven/shared/io/location/Location.java (original)
+++ maven/shared/trunk/maven-shared-io/src/main/java/org/apache/maven/shared/io/location/Location.java Thu Jul 26 15:42:00 2007
@@ -2,21 +2,24 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.nio.ByteBuffer;
 
 public interface Location
 {
-    
+
     File getFile() throws IOException;
-    
+
     void open() throws IOException;
-    
+
     void close();
-    
+
     int read( ByteBuffer buffer ) throws IOException;
-    
+
     int read( byte[] buffer ) throws IOException;
-    
+
+    InputStream getInputStream() throws IOException;
+
     String getSpecification();
 
 }

Modified: maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/FileLocationTest.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/FileLocationTest.java?view=diff&rev=560022&r1=560021&r2=560022
==============================================================================
--- maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/FileLocationTest.java (original)
+++ maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/FileLocationTest.java Thu Jul 26 15:42:00 2007
@@ -1,129 +1,152 @@
 package org.apache.maven.shared.io.location;
 
+import org.apache.maven.shared.io.TestUtils;
+import org.codehaus.plexus.util.IOUtil;
+
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringWriter;
 import java.nio.ByteBuffer;
 
-import org.apache.maven.shared.io.TestUtils;
-
 import junit.framework.TestCase;
 
 public class FileLocationTest
     extends TestCase
 {
-    
+
     public void testShouldConstructWithFileThenRetrieveSameFile() throws IOException
     {
         File file = File.createTempFile( "test.", ".file-location" );
         file.deleteOnExit();
-        
+
         FileLocation location = new FileLocation( file, file.getAbsolutePath() );
-        
+
         assertSame( file, location.getFile() );
         assertEquals( file.getAbsolutePath(), location.getSpecification() );
     }
-    
+
     public void testShouldReadFileContentsUsingByteBuffer() throws IOException
     {
         File file = File.createTempFile( "test.", ".file-location" );
         file.deleteOnExit();
-        
+
         String testStr = "This is a test";
-        
+
         TestUtils.writeToFile( file, testStr );
-        
+
         FileLocation location = new FileLocation( file, file.getAbsolutePath() );
-        
+
         location.open();
-        
+
         ByteBuffer buffer = ByteBuffer.allocate( testStr.length() );
         location.read( buffer );
-        
+
         assertEquals( testStr, new String( buffer.array() ) );
     }
-    
+
+    public void testShouldReadFileContentsUsingStream() throws IOException
+    {
+        File file = File.createTempFile( "test.", ".file-location" );
+        file.deleteOnExit();
+
+        String testStr = "This is a test";
+
+        TestUtils.writeToFile( file, testStr );
+
+        FileLocation location = new FileLocation( file, file.getAbsolutePath() );
+
+        location.open();
+
+        InputStream stream = location.getInputStream();
+        StringWriter writer = new StringWriter();
+        IOUtil.copy( stream, writer );
+
+        assertEquals( testStr, writer.toString() );
+    }
+
     public void testShouldReadFileContentsUsingByteArray() throws IOException
     {
         File file = File.createTempFile( "test.", ".file-location" );
         file.deleteOnExit();
-        
+
         String testStr = "This is a test";
-        
+
         TestUtils.writeToFile( file, testStr );
-        
+
         FileLocation location = new FileLocation( file, file.getAbsolutePath() );
-        
+
         location.open();
-        
+
         byte[] buffer = new byte[ testStr.length() ];
         location.read( buffer );
-        
+
         assertEquals( testStr, new String( buffer ) );
     }
-    
+
     public void testShouldReadThenClose() throws IOException
     {
         File file = File.createTempFile( "test.", ".file-location" );
         file.deleteOnExit();
-        
+
         String testStr = "This is a test";
-        
+
         TestUtils.writeToFile( file, testStr );
-        
+
         FileLocation location = new FileLocation( file, file.getAbsolutePath() );
-        
+
         location.open();
-        
+
         byte[] buffer = new byte[ testStr.length() ];
         location.read( buffer );
-        
+
         assertEquals( testStr, new String( buffer ) );
-        
+
         location.close();
     }
-    
+
     public void testShouldOpenThenFailToSetFile() throws IOException
     {
         File file = File.createTempFile( "test.", ".file-location" );
         file.deleteOnExit();
-        
+
         TestFileLocation location = new TestFileLocation( file.getAbsolutePath() );
-        
+
         location.open();
-        
+
         try
         {
             location.setFile( file );
-            
+
             fail( "should not succeed." );
         }
         catch( IllegalStateException e )
         {
         }
     }
-    
+
     public void testShouldConstructWithoutFileThenSetFileThenOpen() throws IOException
     {
         File file = File.createTempFile( "test.", ".file-location" );
         file.deleteOnExit();
-        
+
         TestFileLocation location = new TestFileLocation( file.getAbsolutePath() );
-        
+
         location.setFile( file );
         location.open();
     }
-    
+
     public void testShouldConstructWithLocationThenRetrieveEquivalentFile() throws IOException
     {
         File file = File.createTempFile( "test.", ".file-location" );
         file.deleteOnExit();
-        
+
         Location location = new TestFileLocation( file.getAbsolutePath() );
-        
+
         assertEquals( file, location.getFile() );
         assertEquals( file.getAbsolutePath(), location.getSpecification() );
     }
-    
+
     private static final class TestFileLocation extends FileLocation
     {
 
@@ -131,7 +154,7 @@
         {
             super( specification );
         }
-        
+
     }
 
 }