View Javadoc
1   package org.apache.maven.plugins.assembly.io;
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  /**
31   * file location implementation.
32   *
33   */
34  class FileLocation
35      implements Location
36  {
37  
38      private File file;
39      private FileChannel channel;
40      private final String specification;
41      private FileInputStream stream;
42  
43      /**
44       * @param file {@link File}
45       * @param specification spec
46       */
47      FileLocation( File file, String specification )
48      {
49          this.file = file;
50          this.specification = specification;
51      }
52  
53      /**
54       * @param specification spec
55       */
56      FileLocation( String specification )
57      {
58          this.specification = specification;
59      }
60  
61      /** {@inheritDoc} */
62      public void close()
63      {
64          if ( ( channel != null ) && channel.isOpen() )
65          {
66              try
67              {
68                  channel.close();
69              }
70              catch ( IOException e )
71              {
72                  //swallow it.
73              }
74          }
75  
76          if ( stream != null )
77          {
78              try
79              {
80                  stream.close();
81              }
82              catch ( IOException e )
83              {
84                  // swallow it.
85              }
86          }
87      }
88  
89      /** {@inheritDoc} */
90      public File getFile()
91          throws IOException
92      {
93          initFile();
94  
95          return unsafeGetFile();
96      }
97  
98      /**
99       * @return {@link File}
100      */
101     File unsafeGetFile()
102     {
103         return file;
104     }
105 
106     /**
107      * initialize file.
108      * @throws IOException in case of error
109      */
110     private void initFile()
111         throws IOException
112     {
113         if ( file == null )
114         {
115             file = new File( specification );
116         }
117     }
118 
119     /**
120      * @param file {@link File}
121      */
122     protected void setFile( File file )
123     {
124         if ( channel != null )
125         {
126             throw new IllegalStateException( "Location is already open; cannot setFile(..)." );
127         }
128 
129         this.file = file;
130     }
131 
132     /** {@inheritDoc} */
133     public String getSpecification()
134     {
135         return specification;
136     }
137 
138     /** {@inheritDoc} */
139     public void open()
140         throws IOException
141     {
142         if ( stream == null )
143         {
144             initFile();
145 
146             stream = new FileInputStream( file );
147             channel = stream.getChannel();
148         }
149     }
150 
151     /** {@inheritDoc} */
152     public int read( ByteBuffer buffer )
153         throws IOException
154     {
155         open();
156         return channel.read( buffer );
157     }
158 
159     /** {@inheritDoc} */
160     public int read( byte[] buffer )
161         throws IOException
162     {
163         open();
164         return channel.read( ByteBuffer.wrap( buffer ) );
165     }
166 
167     /** {@inheritDoc} */
168     public InputStream getInputStream()
169         throws IOException
170     {
171         open();
172         return stream;
173     }
174 
175 }