feisty meow concerns codebase  2.140
URLDownloader.java
Go to the documentation of this file.
1 package org.gffs.network;
2 
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.OutputStream;
8 import java.net.URL;
9 import java.net.URLConnection;
10 
11 import org.gffs.io.IOUtils;
12 
13 public abstract class URLDownloader
14 {
15  static final private int CONNECTION_TIMEOUT = 1000 * 8;
16  static final private int READ_TIMEOUT = 1000 * 8;
17 
18  static public InputStream connect(URL url) throws IOException
19  {
20  URLConnection connection = url.openConnection();
21  connection.setConnectTimeout(CONNECTION_TIMEOUT);
22  connection.setReadTimeout(READ_TIMEOUT);
23  connection.connect();
24  return connection.getInputStream();
25  }
26 
27  static public void download(URL source, File target) throws IOException
28  {
29  InputStream in = null;
30  OutputStream out = null;
31 
32  try {
33  in = connect(source);
34  out = new FileOutputStream(target);
35  IOUtils.copy(in, out);
36  } finally {
37  IOUtils.close(in);
38  IOUtils.close(out);
39  }
40  }
41 }
static void close(Closeable closeable)
Definition: IOUtils.java:33
static void copy(InputStream in, OutputStream out)
Definition: IOUtils.java:24
static InputStream connect(URL url)
static void download(URL source, File target)