1 package org.eclipse.aether.spi.connector.transport;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.nio.Buffer;
26 import java.nio.ByteBuffer;
27 import java.util.Objects;
28 import java.util.concurrent.atomic.AtomicBoolean;
29
30 import org.eclipse.aether.transfer.TransferCancelledException;
31
32
33
34
35 public abstract class AbstractTransporter
36 implements Transporter
37 {
38
39 private final AtomicBoolean closed;
40
41
42
43
44 protected AbstractTransporter()
45 {
46 closed = new AtomicBoolean();
47 }
48
49 public void peek( PeekTask task )
50 throws Exception
51 {
52 Objects.requireNonNull( "task", "task cannot be null" );
53
54 failIfClosed( task );
55 implPeek( task );
56 }
57
58
59
60
61
62
63
64 protected abstract void implPeek( PeekTask task )
65 throws Exception;
66
67 public void get( GetTask task )
68 throws Exception
69 {
70 Objects.requireNonNull( "task", "task cannot be null" );
71
72 failIfClosed( task );
73 implGet( task );
74 }
75
76
77
78
79
80
81
82 protected abstract void implGet( GetTask task )
83 throws Exception;
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 protected void utilGet( GetTask task, InputStream is, boolean close, long length, boolean resume )
102 throws IOException, TransferCancelledException
103 {
104 OutputStream os = null;
105 try
106 {
107 os = task.newOutputStream( resume );
108 task.getListener().transportStarted( resume ? task.getResumeOffset() : 0L, length );
109 copy( os, is, task.getListener() );
110 os.close();
111 os = null;
112
113 if ( close )
114 {
115 is.close();
116 is = null;
117 }
118 }
119 finally
120 {
121 try
122 {
123 if ( os != null )
124 {
125 os.close();
126 }
127 }
128 catch ( final IOException e )
129 {
130
131 }
132 finally
133 {
134 try
135 {
136 if ( close && is != null )
137 {
138 is.close();
139 }
140 }
141 catch ( final IOException e )
142 {
143
144 }
145 }
146 }
147 }
148
149 public void put( PutTask task )
150 throws Exception
151 {
152 Objects.requireNonNull( "task", "task cannot be null" );
153
154 failIfClosed( task );
155 implPut( task );
156 }
157
158
159
160
161
162
163
164 protected abstract void implPut( PutTask task )
165 throws Exception;
166
167
168
169
170
171
172
173
174
175
176
177
178
179 protected void utilPut( PutTask task, OutputStream os, boolean close )
180 throws IOException, TransferCancelledException
181 {
182 InputStream is = null;
183 try
184 {
185 task.getListener().transportStarted( 0, task.getDataLength() );
186 is = task.newInputStream();
187 copy( os, is, task.getListener() );
188
189 if ( close )
190 {
191 os.close();
192 }
193 else
194 {
195 os.flush();
196 }
197
198 os = null;
199
200 is.close();
201 is = null;
202 }
203 finally
204 {
205 try
206 {
207 if ( close && os != null )
208 {
209 os.close();
210 }
211 }
212 catch ( final IOException e )
213 {
214
215 }
216 finally
217 {
218 try
219 {
220 if ( is != null )
221 {
222 is.close();
223 }
224 }
225 catch ( final IOException e )
226 {
227
228 }
229 }
230 }
231 }
232
233 public void close()
234 {
235 if ( closed.compareAndSet( false, true ) )
236 {
237 implClose();
238 }
239 }
240
241
242
243
244 protected abstract void implClose();
245
246 private void failIfClosed( TransportTask task )
247 {
248 if ( closed.get() )
249 {
250 throw new IllegalStateException( "transporter closed, cannot execute task " + task );
251 }
252 }
253
254 private static void copy( OutputStream os, InputStream is, TransportListener listener )
255 throws IOException, TransferCancelledException
256 {
257 ByteBuffer buffer = ByteBuffer.allocate( 1024 * 32 );
258 byte[] array = buffer.array();
259 for ( int read = is.read( array ); read >= 0; read = is.read( array ) )
260 {
261 os.write( array, 0, read );
262 ( (Buffer) buffer ).rewind();
263 ( (Buffer) buffer ).limit( read );
264 listener.transportProgressed( buffer );
265 }
266 }
267
268 }