public ByteBuffer run(Retriever retriever) { if (!retriever.getState().equals(Retriever.RETRIEVER_STATE_SUCCESSFUL)) return null; HTTPRetriever htr = (HTTPRetriever) retriever; if (htr.getResponseCode() == HttpURLConnection.HTTP_NO_CONTENT) { // Mark tile as missing to avoid excessive attempts MercatorTiledImageLayer.this.levels.markResourceAbsent(tile); return null; } if (htr.getResponseCode() != HttpURLConnection.HTTP_OK) return null; URLRetriever r = (URLRetriever) retriever; ByteBuffer buffer = r.getBuffer(); String suffix = WWIO.makeSuffixForMimeType(htr.getContentType()); if (suffix == null) { return null; // TODO: log error } String path = tile.getPath().substring(0, tile.getPath().lastIndexOf(".")); path += suffix; final File outFile = WorldWind.getDataFileStore().newFile(path); if (outFile == null) return null; try { WWIO.saveBuffer(buffer, outFile); return buffer; } catch (IOException e) { e.printStackTrace(); // TODO: log error return null; } }
/** * Lookup the <code>id</code> in the cache. If found, return results. Otherwise, call delegate to * fetch content. * * @param url the url of the resource to retrieve * @param id the id of the resource to retrieve * @return the contents, or null if not found * @throws IOException if an error occurred retrieving the contents (other than not-found) */ public synchronized byte[] retrieve(String url, String id) throws IOException { SoftReference ref = (SoftReference) cache.get(id); byte[] res = (ref != null) ? (byte[]) ref.get() : null; if (log.isDebugEnabled()) log.debug( "Memory cache('" + id + "'): " + (res != null ? "found" : ref != null ? "expired" : "not found")); if (res != null || delegate == null) return res; res = delegate.retrieve(url, id); if (res == null) return null; if (log.isDebugEnabled()) log.debug("Caching '" + id + "'"); cache.put(id, new SoftReference(res)); return res; }