org.xhtmlrenderer.util.StreamResource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of flying-saucer-core Show documentation
Show all versions of flying-saucer-core Show documentation
Flying Saucer is a CSS 2.1 renderer written in Java. This artifact contains the core rendering and layout code as well as Java2D output.
package org.xhtmlrenderer.util;
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
/**
* User: pdoubleya
* Date: May 15, 2009
*/
public class StreamResource implements AutoCloseable {
private final String _uri;
private URLConnection _conn;
private InputStream _inputStream;
public StreamResource(final String uri) {
_uri = uri;
}
public void connect() {
try {
_conn = new URL(_uri).openConnection();
// If using Java 5+ you can set timeouts for the URL connection--useful if the remote
// server is down etc.; the default timeout is pretty long
//
//uc.setConnectTimeout(10 * 1000);
//uc.setReadTimeout(30 * 1000);
//
// TODO:CLEAN-JDK1.4
// Since we target 1.4, we use a couple of system properties--note these are only supported
// in the Sun JDK implementation--see the Net properties guide in the JDK
// e.g. file:///usr/java/j2sdk1.4.2_17/docs/guide/net/properties.html
System.setProperty("sun.net.client.defaultConnectTimeout", String.valueOf(10 * 1000));
System.setProperty("sun.net.client.defaultReadTimeout", String.valueOf(30 * 1000));
_conn.setRequestProperty("Accept", "*/*");
_conn.connect();
_conn.getContentLength();
} catch (java.net.MalformedURLException e) {
XRLog.exception("bad URL given: " + _uri, e);
} catch (FileNotFoundException e) {
XRLog.exception("item at URI " + _uri + " not found");
} catch (IOException e) {
XRLog.exception("IO problem for " + _uri, e);
}
}
public BufferedInputStream bufferedStream() throws IOException {
_inputStream = _conn.getInputStream();
return new BufferedInputStream(_inputStream);
}
@Override
public void close() {
IOUtil.close(_inputStream);
}
}