Java Code Examples for java.net.URLConnection#getExpiration()

The following examples show how to use java.net.URLConnection#getExpiration() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UniformResourceStorage.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @see org.eclipse.core.resources.IStorage#getContents()
 */
public InputStream getContents() throws CoreException {
	try {
		URLConnection connection = getURI().toURL().openConnection();
		connection.connect();
		expires = System.currentTimeMillis();
		long expiration = connection.getExpiration();
		long date = connection.getDate();
		if (expiration != 0 && date != 0 && expiration > date) {
			expires += (expiration - date);
		} else {
			expires += 10 * 1000; // 10 sec
		}
		timestamp = connection.getLastModified();
		return connection.getInputStream();
	} catch (IOException e) {
		throw new CoreException(new Status(IStatus.ERROR, CorePlugin.PLUGIN_ID, IStatus.OK, "Open stream error", e)); //$NON-NLS-1$
	}
}
 
Example 2
Source File: HttpAsset.java    From robe with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Loads http from the http
 *
 * @return
 */
private byte[] loadAssetFromURL() {
    try {
        URLConnection cnn = this.asset.openConnection();
        cnn.connect();
        int b = -1;
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        while ((b = cnn.getInputStream().read()) != -1)
            stream.write(b);
        stream.flush();
        stream.close();

        this.lastModified = cnn.getLastModified();
        this.expireAt = cnn.getExpiration();
        this.ETAG = cnn.getHeaderField(HttpHeaders.ETAG);

        return stream.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
 
Example 3
Source File: OsmTileLoader.java    From amodeus with GNU General Public License v2.0 5 votes vote down vote up
protected void loadTileMetadata(Tile tile, URLConnection urlConn) {
    String str = urlConn.getHeaderField("X-VE-TILEMETA-CaptureDatesRange");
    if (str != null) {
        tile.putValue("capture-date", str);
    }
    str = urlConn.getHeaderField("X-VE-Tile-Info");
    if (str != null) {
        tile.putValue("tile-info", str);
    }

    Long lng = urlConn.getExpiration();
    if (lng.equals(0L)) {
        try {
            str = urlConn.getHeaderField("Cache-Control");
            if (str != null) {
                for (String token : str.split(",")) {
                    if (token.startsWith("max-age=")) {
                        lng = Long.parseLong(token.substring(8)) * 1000 + System.currentTimeMillis();
                    }
                }
            }
        } catch (NumberFormatException e) {
            // ignore malformed Cache-Control headers
            if (JMapViewer.DEBUG) {
                System.err.println(e.getMessage());
            }
        }
    }
    if (!lng.equals(0L)) {
        tile.putValue("expires", lng.toString());
    }
}
 
Example 4
Source File: UniformResourceStorage.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
/**
 * isValid
 * 
 * @return boolean
 */
public boolean isValid() {
	if (timestamp == -1) {
		return false;
	}
	if (expires >= System.currentTimeMillis()) {
		return true;
	}
	try {
		URLConnection connection = getURI().toURL().openConnection();
		if (connection instanceof HttpURLConnection) {
			connection.setIfModifiedSince(timestamp);
			((HttpURLConnection) connection).setRequestMethod("HEAD"); //$NON-NLS-1$
		}
		connection.connect();
		if (connection instanceof HttpURLConnection) {
			HttpURLConnection httpConnection = (HttpURLConnection) connection;
			long lastModified = httpConnection.getLastModified();
			if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED || (lastModified != 0 && timestamp >= lastModified)) {
				expires = System.currentTimeMillis();
				long expiration = connection.getExpiration();
				long date = connection.getDate();
				if (expiration != 0 && date != 0 && expiration > date) {
					expires += (expiration - date);
				} else {
					expires += 10 * 1000; // 10 sec
				}
				return true;
			}
		}
	} catch (IOException e) {
		IdeLog.logError(CorePlugin.getDefault(), e);
	}
	return false;
}
 
Example 5
Source File: DefaultDownloader.java    From BigApp_Discuz_Android with Apache License 2.0 4 votes vote down vote up
/**
 * Download bitmap to outputStream by uri.
 *
 * @param uri          file path, assets path(assets/xxx) or http url.
 * @param outputStream
 * @param task
 * @return The expiry time stamp or -1 if failed to download.
 */
@Override
public long downloadToStream(String uri, OutputStream outputStream, final BitmapUtils.BitmapLoadTask<?> task) {

    if (task == null || task.isCancelled() || task.getTargetContainer() == null) return -1;

    URLConnection urlConnection = null;
    BufferedInputStream bis = null;

    OtherUtils.trustAllHttpsURLConnection();

    long result = -1;
    long fileLen = 0;
    long currCount = 0;
    try {
        if (uri.startsWith("/")) {
            FileInputStream fileInputStream = new FileInputStream(uri);
            fileLen = fileInputStream.available();
            bis = new BufferedInputStream(fileInputStream);
            result = System.currentTimeMillis() + this.getDefaultExpiry();
        } else if (uri.startsWith("assets/")) {
            InputStream inputStream = this.getContext().getAssets().open(uri.substring(7, uri.length()));
            fileLen = inputStream.available();
            bis = new BufferedInputStream(inputStream);
            result = Long.MAX_VALUE;
        } else {
            final URL url = new URL(uri);
            urlConnection = url.openConnection();
            urlConnection.setConnectTimeout(this.getDefaultConnectTimeout());
            urlConnection.setReadTimeout(this.getDefaultReadTimeout());
            bis = new BufferedInputStream(urlConnection.getInputStream());
            result = urlConnection.getExpiration();
            result = result < System.currentTimeMillis() ? System.currentTimeMillis() + this.getDefaultExpiry() : result;
            fileLen = urlConnection.getContentLength();
        }

        if (task.isCancelled() || task.getTargetContainer() == null) return -1;

        byte[] buffer = new byte[4096];
        int len = 0;
        BufferedOutputStream out = new BufferedOutputStream(outputStream);
        while ((len = bis.read(buffer)) != -1) {
            out.write(buffer, 0, len);
            currCount += len;
            if (task.isCancelled() || task.getTargetContainer() == null) return -1;
            task.updateProgress(fileLen, currCount);
        }
        out.flush();
    } catch (Throwable e) {
        result = -1;
        LogUtils.e(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(bis);
    }
    return result;
}
 
Example 6
Source File: DefaultDownloader.java    From android-open-project-demo with Apache License 2.0 4 votes vote down vote up
/**
 * Download bitmap to outputStream by uri.
 *
 * @param uri          file path, assets path(assets/xxx) or http url.
 * @param outputStream
 * @param task
 * @return The expiry time stamp or -1 if failed to download.
 */
@Override
public long downloadToStream(String uri, OutputStream outputStream, final BitmapUtils.BitmapLoadTask<?> task) {

    if (task == null || task.isCancelled() || task.getTargetContainer() == null) return -1;

    URLConnection urlConnection = null;
    BufferedInputStream bis = null;

    OtherUtils.trustAllHttpsURLConnection();

    long result = -1;
    long fileLen = 0;
    long currCount = 0;  
    try {
    	//分三种方式获取图片
        if (uri.startsWith("/")) { 
        	//sd卡获取
            FileInputStream fileInputStream = new FileInputStream(uri);
            fileLen = fileInputStream.available();
            bis = new BufferedInputStream(fileInputStream);
            result = System.currentTimeMillis() + this.getDefaultExpiry();
        } else if (uri.startsWith("assets/")) { 
        	//资产文件夹
            InputStream inputStream = this.getContext().getAssets().open(uri.substring(7, uri.length()));
            fileLen = inputStream.available();
            bis = new BufferedInputStream(inputStream);
            result = Long.MAX_VALUE;
        } else {  
        	//网络
            final URL url = new URL(uri);
            urlConnection = url.openConnection();
            urlConnection.setConnectTimeout(this.getDefaultConnectTimeout());
            urlConnection.setReadTimeout(this.getDefaultReadTimeout());
            bis = new BufferedInputStream(urlConnection.getInputStream());
            result = urlConnection.getExpiration();
            result = result < System.currentTimeMillis() ? System.currentTimeMillis() + this.getDefaultExpiry() : result;
            fileLen = urlConnection.getContentLength();
        }

        if (task.isCancelled() || task.getTargetContainer() == null) return -1;

        byte[] buffer = new byte[4096];
        int len = 0;
        BufferedOutputStream out = new BufferedOutputStream(outputStream);
        while ((len = bis.read(buffer)) != -1) {
            out.write(buffer, 0, len);
            currCount += len;
            if (task.isCancelled() || task.getTargetContainer() == null) return -1;
            task.updateProgress(fileLen, currCount);     //回调
        }
        out.flush();
    } catch (Throwable e) {
        result = -1;
        LogUtils.e(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(bis);
    }
    return result;
}