4122 |
22 Sep 16 |
nicklas |
1 |
package net.sf.basedb.opengrid.filetransfer; |
4122 |
22 Sep 16 |
nicklas |
2 |
|
4122 |
22 Sep 16 |
nicklas |
3 |
import java.io.ByteArrayOutputStream; |
4122 |
22 Sep 16 |
nicklas |
4 |
import java.io.OutputStream; |
4122 |
22 Sep 16 |
nicklas |
5 |
import java.io.UnsupportedEncodingException; |
4122 |
22 Sep 16 |
nicklas |
6 |
|
4122 |
22 Sep 16 |
nicklas |
7 |
|
4122 |
22 Sep 16 |
nicklas |
8 |
/** |
4122 |
22 Sep 16 |
nicklas |
Download target implementation for getting the |
4122 |
22 Sep 16 |
nicklas |
remote file as a byte array. |
4122 |
22 Sep 16 |
nicklas |
11 |
|
4122 |
22 Sep 16 |
nicklas |
@author nicklas |
4122 |
22 Sep 16 |
nicklas |
@since 1.0 |
4122 |
22 Sep 16 |
nicklas |
14 |
*/ |
4122 |
22 Sep 16 |
nicklas |
15 |
public class ByteArrayDownloadTarget |
4124 |
23 Sep 16 |
nicklas |
16 |
extends AbstractFileTransfer |
4124 |
23 Sep 16 |
nicklas |
17 |
implements DownloadTarget |
4122 |
22 Sep 16 |
nicklas |
18 |
{ |
4122 |
22 Sep 16 |
nicklas |
19 |
|
4122 |
22 Sep 16 |
nicklas |
20 |
private final int limit; |
4122 |
22 Sep 16 |
nicklas |
21 |
private ByteArrayOutputStream data; |
4122 |
22 Sep 16 |
nicklas |
22 |
|
4122 |
22 Sep 16 |
nicklas |
23 |
/** |
4122 |
22 Sep 16 |
nicklas |
Create a new byte array download target with "unlimited" size. |
4122 |
22 Sep 16 |
nicklas |
25 |
|
4122 |
22 Sep 16 |
nicklas |
@param name The name of the "file" |
4122 |
22 Sep 16 |
nicklas |
27 |
*/ |
4122 |
22 Sep 16 |
nicklas |
28 |
public ByteArrayDownloadTarget(String name) |
4122 |
22 Sep 16 |
nicklas |
29 |
{ |
4122 |
22 Sep 16 |
nicklas |
30 |
super(name); |
4122 |
22 Sep 16 |
nicklas |
31 |
this.limit = 0; |
4122 |
22 Sep 16 |
nicklas |
32 |
} |
4122 |
22 Sep 16 |
nicklas |
33 |
|
4122 |
22 Sep 16 |
nicklas |
34 |
@Override |
4122 |
22 Sep 16 |
nicklas |
35 |
public OutputStream getOutputStream() |
4122 |
22 Sep 16 |
nicklas |
36 |
{ |
4122 |
22 Sep 16 |
nicklas |
37 |
if (data == null) data = new ByteArrayOutputStream(8192); |
4122 |
22 Sep 16 |
nicklas |
38 |
return data; |
4122 |
22 Sep 16 |
nicklas |
39 |
} |
4122 |
22 Sep 16 |
nicklas |
40 |
|
4122 |
22 Sep 16 |
nicklas |
41 |
/** |
4122 |
22 Sep 16 |
nicklas |
Get the size of the downloaded data. |
4122 |
22 Sep 16 |
nicklas |
43 |
*/ |
4124 |
23 Sep 16 |
nicklas |
44 |
public int getDownloadedSize() |
4122 |
22 Sep 16 |
nicklas |
45 |
{ |
4122 |
22 Sep 16 |
nicklas |
46 |
return data == null ? 0 : data.size(); |
4122 |
22 Sep 16 |
nicklas |
47 |
} |
4122 |
22 Sep 16 |
nicklas |
48 |
|
4122 |
22 Sep 16 |
nicklas |
49 |
/** |
4122 |
22 Sep 16 |
nicklas |
Get the downloaded data as a byte array. Note |
4122 |
22 Sep 16 |
nicklas |
that a copy is made for each call to this method. |
4124 |
23 Sep 16 |
nicklas |
If no data has been downloaded null is returned. |
4122 |
22 Sep 16 |
nicklas |
53 |
*/ |
4122 |
22 Sep 16 |
nicklas |
54 |
public byte[] getData() |
4122 |
22 Sep 16 |
nicklas |
55 |
{ |
4124 |
23 Sep 16 |
nicklas |
56 |
return getDownloadedSize() == 0 ? null : data.toByteArray(); |
4122 |
22 Sep 16 |
nicklas |
57 |
} |
4122 |
22 Sep 16 |
nicklas |
58 |
|
4122 |
22 Sep 16 |
nicklas |
59 |
/** |
4122 |
22 Sep 16 |
nicklas |
Get the downloaded data as a string. If no |
4122 |
22 Sep 16 |
nicklas |
data has been downloaded null is returned. |
4122 |
22 Sep 16 |
nicklas |
62 |
*/ |
4122 |
22 Sep 16 |
nicklas |
63 |
public String getString(String charset) |
4122 |
22 Sep 16 |
nicklas |
64 |
{ |
4122 |
22 Sep 16 |
nicklas |
65 |
try |
4122 |
22 Sep 16 |
nicklas |
66 |
{ |
4124 |
23 Sep 16 |
nicklas |
67 |
return getDownloadedSize() == 0 ? null : data.toString(charset); |
4122 |
22 Sep 16 |
nicklas |
68 |
} |
4122 |
22 Sep 16 |
nicklas |
69 |
catch (UnsupportedEncodingException ex) |
4122 |
22 Sep 16 |
nicklas |
70 |
{ |
4122 |
22 Sep 16 |
nicklas |
71 |
throw new RuntimeException(ex); |
4122 |
22 Sep 16 |
nicklas |
72 |
} |
4122 |
22 Sep 16 |
nicklas |
73 |
} |
4122 |
22 Sep 16 |
nicklas |
74 |
} |