Package org.jeegen.jee6.util
Class Download
- java.lang.Object
-
- org.jeegen.jee6.util.Download
-
- All Implemented Interfaces:
Serializable
public class Download extends Object implements Serializable
This class helps downloading binary file via the HTTP protocol.- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static long
copy(InputStream is, OutputStream os)
This method copies data from anInputStream
directly into anOutputStream
.byte[]
downloadArray()
This method downloads a HTTP resource and returns it as an byte array.BufferedImage
downloadImage()
This method returns aBufferedImage
.boolean
getFollowRedirect()
This method returns a flag whether this download instance should follow redirectsString
getMimeType()
This getter returns the mime type of the downloaded byte array.int
getTimeout()
This method returns the connection timeout.String
getUrl()
This method returns the usedURL
.static BufferedImage
parse(byte[] buffer)
This method converts an image as byte array into aBufferedImage
.static byte[]
read(InputStream is, int len)
This method reads a defined length from anInputStream
into a byte array.static InetAddress
resolve(String fqhn)
This method resolves a full qualified hostname into an IP address.void
setFollowRedirect(boolean redirect)
This method sets the behavior concerning redirects.void
setTimeout(int timeout)
This method sets a new timeout.
-
-
-
Constructor Detail
-
Download
public Download(String url, int timeout) throws MalformedURLException
The constructor specifies the URL.- Parameters:
url
- The URL for downloading.timeout
- The timeout for a download request in milliseconds.- Throws:
MalformedURLException
- Thrown if the URL is malformed.
-
Download
public Download(String url) throws MalformedURLException
The constructor specifies the URL.- Parameters:
url
- The URL for downloading.- Throws:
MalformedURLException
- Thrown if the URL is malformed.
-
-
Method Detail
-
downloadImage
public BufferedImage downloadImage() throws IOException
This method returns aBufferedImage
.- Returns:
- The downloaded image.
- Throws:
IOException
- Thrown if something went wrong.
-
downloadArray
public byte[] downloadArray() throws IOException
This method downloads a HTTP resource and returns it as an byte array.- Returns:
- The downloaded byte array.
- Throws:
IOException
- Thrown if womething went wrong.
-
parse
public static BufferedImage parse(byte[] buffer) throws IOException
This method converts an image as byte array into aBufferedImage
.- Parameters:
buffer
- The image as byte array.- Returns:
- The resulting
BufferedImage
. - Throws:
IOException
- Thrown if something went wrong.
-
getMimeType
public String getMimeType()
This getter returns the mime type of the downloaded byte array. This is only defined after usingdownloadArray()
, otherwise the return value is always null.- Returns:
- The mime type of the
downloadArray()
method call or null otherwise.
-
getTimeout
public int getTimeout()
This method returns the connection timeout.- Returns:
- The connection timeout.
-
setTimeout
public void setTimeout(int timeout)
This method sets a new timeout.- Parameters:
timeout
- The new timeout.
-
getFollowRedirect
public boolean getFollowRedirect()
This method returns a flag whether this download instance should follow redirects- Returns:
- Flag if this instance should process redirects.
-
setFollowRedirect
public void setFollowRedirect(boolean redirect)
This method sets the behavior concerning redirects.- Parameters:
redirect
- The flag which sets the redirect behavior of this instance.
-
resolve
public static InetAddress resolve(String fqhn) throws UnknownHostException
This method resolves a full qualified hostname into an IP address.- Parameters:
fqhn
- The hostname to resolve.- Returns:
- The IP address if resolvable.
- Throws:
UnknownHostException
- If the hostname cannot be resolved.
-
read
public static byte[] read(InputStream is, int len) throws IOException
This method reads a defined length from anInputStream
into a byte array. TheInputStream
is closed after reading.- Parameters:
is
- TheInputStream
len
- The length of data inside theInputStream
.- Returns:
- The resulting byte array
- Throws:
IOException
- on read error.
-
copy
public static long copy(InputStream is, OutputStream os) throws IOException
This method copies data from anInputStream
directly into anOutputStream
. The amount of written bytes is returned. Both streams are not closed after copy.- Parameters:
is
- TheInputStream
to read from.os
- TheOutputStream
to write to.- Returns:
- The amount of written bytes copied.
- Throws:
IOException
- If some IO went wrong.
-
-