/** * Returns an InputStream from the internal InputStream Supplier if it's not null. Otherwise * returns an InputStream for the internal buffer reference if valid and null otherwise. * * <p>The caller has to close the InputStream after using it. */ public InputStream getInputStream() { if (mInputStreamSupplier != null) { return mInputStreamSupplier.get(); } CloseableReference<PooledByteBuffer> pooledByteBufferRef = CloseableReference.cloneOrNull(mPooledByteBufferRef); if (pooledByteBufferRef != null) { try { return new PooledByteBufferInputStream(pooledByteBufferRef.get()); } finally { CloseableReference.closeSafely(pooledByteBufferRef); } } return null; }
private EncodedImage buildEncodedImage( PooledByteBuffer imageBytes, ExifInterface exifInterface) { Pair<Integer, Integer> dimensions = BitmapUtil.decodeDimensions(new PooledByteBufferInputStream(imageBytes)); int rotationAngle = getRotationAngle(exifInterface); int width = dimensions != null ? dimensions.first : EncodedImage.UNKNOWN_WIDTH; int height = dimensions != null ? dimensions.second : EncodedImage.UNKNOWN_HEIGHT; EncodedImage encodedImage; CloseableReference<PooledByteBuffer> closeableByteBuffer = CloseableReference.of(imageBytes); try { encodedImage = new EncodedImage(closeableByteBuffer); } finally { CloseableReference.closeSafely(closeableByteBuffer); } encodedImage.setImageFormat(DefaultImageFormats.JPEG); encodedImage.setRotationAngle(rotationAngle); encodedImage.setWidth(width); encodedImage.setHeight(height); return encodedImage; }
@Test public void testReadFromStream() throws Exception { InputStream is = new PooledByteBufferInputStream(mPooledByteBuffer); byte[] tmp = new byte[BUFFER_LENGTH + 1]; int bytesRead = is.read(tmp, 0, tmp.length); assertEquals(BUFFER_LENGTH, bytesRead); for (int i = 0; i < BUFFER_LENGTH; i++) { assertEquals(BYTES[i], tmp[i]); } assertEquals(-1, is.read()); }
@Override public BitmapRegionDecoder intercept(Chain chain) throws IOException { final Uri uri = chain.uri(); BitmapRegionDecoder decoder = chain.chain(uri); if (decoder != null){ return decoder; } if (UriUtil.isNetworkUri(uri)){ ImagePipeline imagePipeline = ImagePipelineFactory.getInstance().getImagePipeline(); ImageRequest request = ImageRequest.fromUri(uri); DataSource<CloseableReference<PooledByteBuffer>> dataSource = imagePipeline.fetchEncodedImage(request,null); try { CloseableReference<PooledByteBuffer> ref = DataSources.waitForFinalResult(dataSource); if (ref == null){ return null; } PooledByteBuffer result = ref.get(); if (BuildConfig.DEBUG) { Log.d("FrescoInterceptor", "从我这加载"); } try { InputStream inputStream = new PooledByteBufferInputStream(result); Closeables.closeQuietly(inputStream); return BitmapRegionDecoder.newInstance(inputStream,false); } catch (IOException e) { ImageRequest imageRequest=ImageRequest.fromUri(uri); CacheKey cacheKey= DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(imageRequest,null); BinaryResource resource = ImagePipelineFactory.getInstance().getMainFileCache().getResource(cacheKey); File file=((FileBinaryResource)resource).getFile(); if (BuildConfig.DEBUG) { Log.d("FrescoInterceptor", file.getName()); } return Interceptors.fixJPEGDecoder(file,e); } } catch (Throwable throwable) { if (BuildConfig.DEBUG) { Log.d("FrescoInterceptor", "intercept: 加载失败了"); } throwable.printStackTrace(); return null; } } return null; }