Created
May 13, 2014 10:49
-
-
Save mylesjao/5facd60d49b872c5cc98 to your computer and use it in GitHub Desktop.
guava ByteSource with timeout configuration support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ConfigurableUrlByteSource extends ByteSource{ | |
private final URL url; | |
private int connectTimeout = 0; | |
private int readTimeout = 0; | |
public ConfigurableUrlByteSource(URL url) { | |
this.url = Preconditions.checkNotNull(url, "url is null"); | |
} | |
@Override | |
public InputStream openStream() throws IOException { | |
URLConnection connection = url.openConnection(); | |
connection.setConnectTimeout(connectTimeout); | |
connection.setReadTimeout(readTimeout); | |
return connection.getInputStream(); | |
} | |
@Override | |
public String toString() { | |
return "ConfigurableUrlByteSource [url=" + url + ", connectTimeout=" + connectTimeout | |
+ ", readTimeout=" + readTimeout + "]"; | |
} | |
public int getConnectTimeout() { | |
return connectTimeout; | |
} | |
public void setConnectTimeout(int connectTimeout) { | |
this.connectTimeout = connectTimeout; | |
} | |
public int getReadTimeout() { | |
return readTimeout; | |
} | |
public void setReadTimeout(int readTimeout) { | |
this.readTimeout = readTimeout; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment