Coverage Report - org.apache.maven.shared.io.location.FileLocation
 
Classes in this File Line Coverage Branch Coverage Complexity
FileLocation
95 %
36/38
75 %
9/12
1,667
 
 1  
 package org.apache.maven.shared.io.location;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.File;
 23  
 import java.io.FileInputStream;
 24  
 import java.io.IOException;
 25  
 import java.io.InputStream;
 26  
 import java.nio.ByteBuffer;
 27  
 import java.nio.channels.FileChannel;
 28  
 
 29  
 
 30  
 public class FileLocation
 31  
     implements Location
 32  
 {
 33  
 
 34  
     private File file;
 35  
     private FileChannel channel;
 36  
     private final String specification;
 37  
     private FileInputStream stream;
 38  
 
 39  
     public FileLocation( File file, String specification )
 40  6
     {
 41  6
         this.file = file;
 42  6
         this.specification = specification;
 43  6
     }
 44  
 
 45  
     protected FileLocation( String specification )
 46  16
     {
 47  16
         this.specification = specification;
 48  16
     }
 49  
 
 50  
     public void close()
 51  
     {
 52  1
         if ( ( channel != null ) && channel.isOpen() )
 53  
         {
 54  
             try
 55  
             {
 56  1
                 channel.close();
 57  
             }
 58  0
             catch ( IOException e )
 59  
             {
 60  
                 //swallow it.
 61  1
             }
 62  
         }
 63  
 
 64  1
         if ( stream != null )
 65  
         {
 66  
             try
 67  
             {
 68  1
                 stream.close();
 69  
             }
 70  0
             catch( IOException e )
 71  
             {
 72  
                 // swallow it.
 73  1
             }
 74  
         }
 75  1
     }
 76  
 
 77  
     public File getFile()
 78  
         throws IOException
 79  
     {
 80  12
         initFile();
 81  
 
 82  12
         return unsafeGetFile();
 83  
     }
 84  
 
 85  
     protected File unsafeGetFile()
 86  
     {
 87  16
         return file;
 88  
     }
 89  
 
 90  
     protected void initFile()
 91  
         throws IOException
 92  
     {
 93  
         // TODO: Log this in the debug log-level...
 94  17
         if ( file == null )
 95  
         {
 96  2
             file = new File( specification );
 97  
         }
 98  17
     }
 99  
 
 100  
     protected void setFile( File file )
 101  
     {
 102  13
         if ( channel != null )
 103  
         {
 104  1
             throw new IllegalStateException( "Location is already open; cannot setFile(..)." );
 105  
         }
 106  
 
 107  12
         this.file = file;
 108  12
     }
 109  
 
 110  
     public String getSpecification()
 111  
     {
 112  2
         return specification;
 113  
     }
 114  
 
 115  
     public void open()
 116  
         throws IOException
 117  
     {
 118  16
         if ( stream == null )
 119  
         {
 120  9
             initFile();
 121  
 
 122  9
             stream = new FileInputStream( file );
 123  9
             channel = stream.getChannel();
 124  
         }
 125  16
     }
 126  
 
 127  
     public int read( ByteBuffer buffer )
 128  
         throws IOException
 129  
     {
 130  1
         open();
 131  1
         return channel.read( buffer );
 132  
     }
 133  
 
 134  
     public int read( byte[] buffer )
 135  
         throws IOException
 136  
     {
 137  5
         open();
 138  5
         return channel.read( ByteBuffer.wrap( buffer ) );
 139  
     }
 140  
 
 141  
     public InputStream getInputStream()
 142  
         throws IOException
 143  
     {
 144  1
         open();
 145  1
         return stream;
 146  
     }
 147  
 
 148  
 }