GzipCacheImpl extends GenericCacheImpl<ICacheEntry> implements Serializable,
		IGzipCache { @Override public InputStream getInputStream(final String key, final URI source, final MutableInt retLength) throws IOException { final String sourceMethod = "getInputStream"; final boolean isTraceLogging = log.isLoggable(Level.FINER); if (isTraceLogging) { log.entering(sourceClass, sourceMethod, new Object[] { key, source, retLength }); } InputStream in = null, result = null; CacheEntry tryCacheEntry = (CacheEntry) super.get(key); URLConnection connection = source.toURL().openConnection(); try { long lastModified = connection.getLastModified(); if (tryCacheEntry != null) { byte[] bytes = tryCacheEntry.bytes; File file = tryCacheEntry.file; if (bytes != null) { if (lastModified != tryCacheEntry.lastModified) { cacheMap.remove(key, tryCacheEntry); } else { retLength.setValue(tryCacheEntry.bytes.length); result = new ByteArrayInputStream(tryCacheEntry.bytes); } } else if (file != null) { if (Math.abs(lastModified - file.lastModified()) > 1000) { cacheMap.remove(key, tryCacheEntry); cacheManager.deleteFileDelayed(file.getName()); } else { try { retLength.setValue(file.length()); result = new FileInputStream(file); } catch (FileNotFoundException ex) { cacheMap.remove(key, tryCacheEntry); } } } if (result != null) { log.exiting(sourceClass, sourceMethod, result); return result; } } CacheEntry newCacheEntry = new CacheEntry(); CacheEntry oldCacheEntry = (CacheEntry) cacheMap.putIfAbsent(key, newCacheEntry); final CacheEntry cacheEntry = oldCacheEntry != null ? oldCacheEntry : newCacheEntry; synchronized (cacheEntry) { if (cacheEntry.ex != null) { throw cacheEntry.ex; } byte[] bytes = cacheEntry.bytes; if (bytes != null) { retLength.setValue(bytes.length); result = new ByteArrayInputStream(bytes); } else if (cacheEntry.file != null) { retLength.setValue(cacheEntry.file.length()); result = new FileInputStream(cacheEntry.file); } else { try { in = connection.getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); VariableGZIPOutputStream compress = new VariableGZIPOutputStream(bos, 10240); compress.setLevel(Deflater.BEST_COMPRESSION); CopyUtil.copy(in, compress); cacheEntry.lastModified = lastModified; cacheEntry.bytes = bos.toByteArray(); result = new ByteArrayInputStream(cacheEntry.bytes); retLength.setValue(cacheEntry.bytes.length); String path = source.getPath(); int idx = path.lastIndexOf("/"); String fname = (idx != -1) ? path.substring(idx + 1) : path; cacheManager.createCacheFileAsync( fname + ".gzip.", new ByteArrayInputStream(cacheEntry.bytes), new ICacheManager.CreateCompletionCallback() { @Override public void completed(String filename, Exception e) { if (e != null && log.isLoggable(Level.SEVERE)) { log.logp(Level.SEVERE, sourceClass, sourceMethod, e.getMessage(), e); return; } File cacheFile = new File(cacheManager.getCacheDir(), filename); cacheFile.setLastModified(cacheEntry.lastModified); cacheEntry.file = cacheFile; cacheEntry.bytes = null; } }); } catch (Throwable t) { cacheEntry.ex = (t instanceof IOException) ? (IOException) t : new IOException(t); cacheMap.remove(key, cacheEntry); throw cacheEntry.ex; } } } } finally { if (in != null) { in.close(); } else { connection.getInputStream().close(); } } if (isTraceLogging) { log.exiting(sourceClass, sourceClass, result); } return result; }  @Override InputStream getInputStream(final String key, final URI source, final MutableInt retLength); @Override void setAggregator(IAggregator aggregator); @Override IGenericCache newInstance();  }
@Test public void testGetInputStream() throws Exception { GzipCacheImpl impl = new GzipCacheImpl(); EasyMock.replay(mockAggregator, mockCacheManager); impl.setAggregator(mockAggregator); MutableInt retLength = new MutableInt(); InputStream is = impl.getInputStream("key", tempfile.toURI(), retLength); Assert.assertTrue(is instanceof ByteArrayInputStream); Assert.assertEquals(testData, unzipInputStream(is, retLength.getValue())); IGzipCache.ICacheEntry cacheEntry = impl.get("key"); Assert.assertNotNull(Whitebox.getInternalState(cacheEntry, "bytes")); Assert.assertEquals(Long.valueOf(tempfile.lastModified()), (Long)Whitebox.getInternalState(cacheEntry, "lastModified")); Assert.assertNull(Whitebox.getInternalState(cacheEntry, "ex")); Assert.assertNull(Whitebox.getInternalState(cacheEntry, "file")); is = impl.getInputStream("key", tempfile.toURI(), retLength); Assert.assertTrue(is instanceof ByteArrayInputStream); Assert.assertEquals(testData, unzipInputStream(is, retLength.getValue())); latch1.countDown(); latch2.await(); is = impl.getInputStream("key", tempfile.toURI(), retLength); Assert.assertTrue(is instanceof FileInputStream); Assert.assertEquals(testData, unzipInputStream(is, retLength.getValue())); Assert.assertSame(cacheEntry, impl.get("key")); Assert.assertNull(Whitebox.getInternalState(cacheEntry, "bytes")); Assert.assertNull(Whitebox.getInternalState(cacheEntry, "ex")); File cacheFile = (File)Whitebox.getInternalState(cacheEntry, "file"); Assert.assertTrue(cacheFile.getName().startsWith("source.gzip.")); Assert.assertTrue(cacheFile.getName().endsWith(".cache")); Assert.assertEquals(tempfile.lastModified(), cacheFile.lastModified()); }