@Test public void shouldReadNonZippedInputStream() throws IOException { final String content = "non-zipped content"; when(delegate.get(any())) .then((Answer<InputStream>) invocation -> new ByteArrayInputStream(content.getBytes())); ContentCache contentCache = new ZippedContentCache(delegate); final InputStream stream = contentCache.get(() -> StringUtils.EMPTY); verify(delegate, times(2)).get(any()); assertEquals(ByteArrayInputStream.class, stream.getClass()); assertEquals(content, IOUtils.toString(stream, "UTF-8")); }
@Override @VolumeMetered public InputStream get(ContentCacheKey key) { return ofNullable(delegate.get(key)) .map(entry -> { try { return new GZIPInputStream(entry); } catch (ZipException e) { try { entry.close(); } catch (IOException closeException) { LOGGER.debug("Unable to close stream", e); } return delegate.get(key); } catch (IOException e) { throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e); } }) .orElse(null); }
ZippedContentCache implements ContentCache { @Override @VolumeMetered public InputStream get(ContentCacheKey key) { return ofNullable(delegate.get(key)) .map(entry -> { try { return new GZIPInputStream(entry); } catch (ZipException e) { try { entry.close(); } catch (IOException closeException) { LOGGER.debug("Unable to close stream", e); } return delegate.get(key); } catch (IOException e) { throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e); } }) .orElse(null); } }
ZippedContentCache implements ContentCache { @Override @VolumeMetered public InputStream get(ContentCacheKey key) { return ofNullable(delegate.get(key)) .map(entry -> { try { return new GZIPInputStream(entry); } catch (ZipException e) { try { entry.close(); } catch (IOException closeException) { LOGGER.debug("Unable to close stream", e); } return delegate.get(key); } catch (IOException e) { throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e); } }) .orElse(null); } ZippedContentCache(ContentCache delegate); }
ZippedContentCache implements ContentCache { @Override @VolumeMetered public InputStream get(ContentCacheKey key) { return ofNullable(delegate.get(key)) .map(entry -> { try { return new GZIPInputStream(entry); } catch (ZipException e) { try { entry.close(); } catch (IOException closeException) { LOGGER.debug("Unable to close stream", e); } return delegate.get(key); } catch (IOException e) { throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e); } }) .orElse(null); } ZippedContentCache(ContentCache delegate); @Override @Timed boolean has(ContentCacheKey key); @Override @VolumeMetered InputStream get(ContentCacheKey key); @Override @VolumeMetered OutputStream put(ContentCacheKey key, TimeToLive timeToLive); @Override @Timed void evict(ContentCacheKey key); @Override @Timed void evictMatch(ContentCacheKey key); @Override @Timed void move(ContentCacheKey from, ContentCacheKey to, TimeToLive toTimeToLive); @Override @Timed void clear(); }
ZippedContentCache implements ContentCache { @Override @VolumeMetered public InputStream get(ContentCacheKey key) { return ofNullable(delegate.get(key)) .map(entry -> { try { return new GZIPInputStream(entry); } catch (ZipException e) { try { entry.close(); } catch (IOException closeException) { LOGGER.debug("Unable to close stream", e); } return delegate.get(key); } catch (IOException e) { throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e); } }) .orElse(null); } ZippedContentCache(ContentCache delegate); @Override @Timed boolean has(ContentCacheKey key); @Override @VolumeMetered InputStream get(ContentCacheKey key); @Override @VolumeMetered OutputStream put(ContentCacheKey key, TimeToLive timeToLive); @Override @Timed void evict(ContentCacheKey key); @Override @Timed void evictMatch(ContentCacheKey key); @Override @Timed void move(ContentCacheKey from, ContentCacheKey to, TimeToLive toTimeToLive); @Override @Timed void clear();  }