##### Coverage Report - org.apache.maven.shared.io.location.FileLocation

Classes in this File | Line Coverage | Branch Coverage | Complexity
FileLocation |
96% | 26/27
100% | 3/3
| 1.4545454545454546;1.455

1 |  |
     package org.apache.maven.shared.io.location;
2 |  |
3 |  |
     import java.io.File;
4 |  |
     import java.io.FileInputStream;
5 |  |
     import java.io.IOException;
6 |  |
     import java.nio.ByteBuffer;
7 |  |
     import java.nio.channels.FileChannel;
8 |  |
9 |  |
10 |  |
     public class FileLocation
11 |  |
         implements Location
12 |  |
     {
13 |  |
14 |  |
         private File file;
15 |  |
         private FileChannel channel;
16 |  |
         private final String specification;
17 |  |
18 | 5 |
         public FileLocation( File file, String specification )
19 |  |
         {
20 | 5 |
             this.file = file;
21 | 5 |
             this.specification = specification;
22 | 5 |
         }
23 |  |
24 | 16 |
         protected FileLocation( String specification )
25 |  |
         {
26 | 16 |
             this.specification = specification;
27 | 16 |
         }
28 |  |
29 |  |
         public void close()
30 |  |
         {
31 | 1 |
             if ( channel != null && channel.isOpen() )
32 |  |
             {
33 |  |
                 try
34 |  |
                 {
35 | 1 |
                     channel.close();
36 |  |
                 }
37 | 0 |
                 catch ( IOException e )
38 |  |
                 {
39 |  |
                     //swallow it.
40 |  |
                 }
41 |  |
             }
42 | 1 |
         }
43 |  |
44 |  |
         public File getFile()
45 |  |
             throws IOException
46 |  |
         {
47 | 12 |
             initFile();
48 |  |
49 | 12 |
             return unsafeGetFile();
50 |  |
         }
51 |  |
52 |  |
         protected File unsafeGetFile()
53 |  |
         {
54 | 16 |
             return file;
55 |  |
         }
56 |  |
57 |  |
         protected void initFile()
58 |  |
             throws IOException
59 |  |
         {
60 |  |
             // TODO: Log this in the debug log-level...
61 | 16 |
             if ( file == null )
62 |  |
             {
63 | 2 |
                 file = new File( specification );
64 |  |
             }
65 | 16 |
         }
66 |  |
67 |  |
         protected void setFile( File file )
68 |  |
         {
69 | 13 |
             if ( channel != null )
70 |  |
             {
71 | 1 |
                 throw new IllegalStateException( "Location is already open; cannot setFile(..)." );
72 |  |
             }
73 |  |
74 | 12 |
             this.file = file;
75 | 12 |
         }
76 |  |
77 |  |
         public String getSpecification()
78 |  |
         {
79 | 2 |
             return specification;
80 |  |
         }
81 |  |
82 |  |
         public void open()
83 |  |
             throws IOException
84 |  |
         {
85 | 8 |
             initFile();
86 |  |
87 | 8 |
             channel = new FileInputStream( file ).getChannel();
88 | 8 |
         }
89 |  |
90 |  |
         public int read( ByteBuffer buffer )
91 |  |
             throws IOException
92 |  |
         {
93 | 1 |
             return channel.read( buffer );
94 |  |
         }
95 |  |
96 |  |
         public int read( byte[] buffer )
97 |  |
             throws IOException
98 |  |
         {
99 | 5 |
             return channel.read( ByteBuffer.wrap( buffer ) );
100 |  |
         }
101 |  |
102 |  |
     }