1 package org.apache.maven.plugins.assembly.io;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
45
46
47 FileLocation( File file, String specification )
48 {
49 this.file = file;
50 this.specification = specification;
51 }
52
53
54
55
56 FileLocation( String specification )
57 {
58 this.specification = specification;
59 }
60
61
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
73 }
74 }
75
76 if ( stream != null )
77 {
78 try
79 {
80 stream.close();
81 }
82 catch ( IOException e )
83 {
84
85 }
86 }
87 }
88
89
90 public File getFile()
91 throws IOException
92 {
93 initFile();
94
95 return unsafeGetFile();
96 }
97
98
99
100
101 File unsafeGetFile()
102 {
103 return file;
104 }
105
106
107
108
109
110 private void initFile()
111 throws IOException
112 {
113 if ( file == null )
114 {
115 file = new File( specification );
116 }
117 }
118
119
120
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
133 public String getSpecification()
134 {
135 return specification;
136 }
137
138
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
152 public int read( ByteBuffer buffer )
153 throws IOException
154 {
155 open();
156 return channel.read( buffer );
157 }
158
159
160 public int read( byte[] buffer )
161 throws IOException
162 {
163 open();
164 return channel.read( ByteBuffer.wrap( buffer ) );
165 }
166
167
168 public InputStream getInputStream()
169 throws IOException
170 {
171 open();
172 return stream;
173 }
174
175 }