@@ -0,0 +1,2901 @@
+/*%0A * Copyright (c) 2015-present, Facebook, Inc.%0A * All rights reserved.%0A *%0A * This source code is licensed under the BSD-style license found in the%0A * LICENSE file in the root directory of this source tree. An additional grant%0A * of patent rights can be found in the PATENTS file in the same directory.%0A */%0A%0Apackage com.facebook.imagepipeline.producers;%0A%0Aimport java.io.ByteArrayInputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.util.concurrent.Executor;%0A%0Aimport android.net.Uri;%0Aimport android.util.Base64;%0A%0Aimport com.facebook.common.internal.Preconditions;%0Aimport com.facebook.common.internal.VisibleForTesting;%0Aimport com.facebook.imagepipeline.memory.PooledByteBufferFactory;%0Aimport com.facebook.imagepipeline.request.ImageRequest;%0A%0A/**%0A * Producer for data URIs.%0A *%0A * %3Cp%3EData URIs embed the data in the URI itself. They don't point to a file location;%0A * the URI is the data. Data can be encoded in either base-64 or escaped ASCII.%0A * See the %3Ca href=%22http://tools.ietf.org/html/rfc2397%22%3Espec%3C/a%3E for full details.%0A *%0A * %3Cp%3EData URIs are intended for small pieces of data only, since the URI lives on the Java%0A * heap. For large data, use a another URI type.%0A *%0A * %3Cp%3ECharsets specified in the URI are ignored. Only UTF-8 encoding is currently supported.%0A */%0Apublic class DataFetchProducer extends LocalFetchProducer %7B%0A%0A  private static final String PRODUCER_NAME = %22DataFetchProducer%22;%0A%0A  public DataFetchProducer(%0A      Executor executor,%0A      PooledByteBufferFactory pooledByteBufferFactory) %7B%0A    super(executor, pooledByteBufferFactory);%0A  %7D%0A%0A  @Override%0A  protected InputStream getInputStream(ImageRequest imageRequest) throws IOException %7B%0A    return new ByteArrayInputStream(getData(imageRequest.getSourceUri().toString()));%0A  %7D%0A%0A  @Override%0A  protected int getLength(ImageRequest imageRequest) %7B%0A    return getData(imageRequest.getSourceUri().toString()).length;%0A  %7D%0A%0A  @Override%0A  protected String getProducerName() %7B%0A    return PRODUCER_NAME;%0A  %7D%0A%0A  @VisibleForTesting%0A  static byte%5B%5D getData(String uri) %7B%0A    /*%0A     * Format of a data URL:%0A     * data:mime/type;param=value;param=value;base64,actual_data%0A     * everything is optional except the actual data, which is either%0A     * base-64 or escaped ASCII encoded.%0A     */%0A    Preconditions.checkArgument(uri.substring(0, 5).equals(%22data:%22));%0A    int commaPos = uri.indexOf(',');%0A%0A    String dataStr = uri.substring(commaPos + 1, uri.length());%0A    if (isBase64(uri.substring(0, commaPos))) %7B%0A      return Base64.decode(dataStr, Base64.DEFAULT);%0A    %7D else %7B%0A      String str = Uri.decode(dataStr);%0A      byte%5B%5D b = str.getBytes();%0A      return b;%0A    %7D%0A  %7D%0A%0A  @VisibleForTesting%0A  static boolean isBase64(String prefix) %7B%0A    if (!prefix.contains(%22;%22)) %7B%0A      return false;%0A    %7D%0A    String%5B%5D parameters = prefix.split(%22;%22);%0A    return parameters%5Bparameters.length - 1%5D.equals(%22base64%22);%0A  %7D%0A%7D%0A

Add support for data URIs in [Part 1]