All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bumptech.glide.load.engine.DecodePath Maven / Gradle / Ivy

Go to download

A fast and efficient image loading library for Android focused on smooth scrolling.

There is a newer version: 5.0.0-rc01
Show newest version
package com.bumptech.glide.load.engine;

import android.support.v4.util.Pools.Pool;
import android.util.Log;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.data.DataRewinder;
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Attempts to decode and transcode  resource type from a given data type.
 *
 * @param      The type of data ResourceType that will be decoded from.
 * @param  The type of intermediate resource that will be decoded.
 * @param     The final type of resource that will be transcoded from ResourceType and
 *                       returned to the caller.
 */
public class DecodePath {
  private static final String TAG = "DecodePath";
  private final Class dataClass;
  private final List> decoders;
  private final ResourceTranscoder transcoder;
  private final Pool> listPool;
  private final String failureMessage;

  public DecodePath(Class dataClass, Class resourceClass,
      Class transcodeClass,
      List> decoders,
      ResourceTranscoder transcoder, Pool> listPool) {
    this.dataClass = dataClass;
    this.decoders = decoders;
    this.transcoder = transcoder;
    this.listPool = listPool;
    failureMessage = "Failed DecodePath{" + dataClass.getSimpleName() + "->"
        + resourceClass.getSimpleName() + "->" + transcodeClass.getSimpleName() + "}";
  }

  public Resource decode(DataRewinder rewinder, int width, int height,
      Options options, DecodeCallback callback) throws GlideException {
    Resource decoded = decodeResource(rewinder, width, height, options);
    Resource transformed = callback.onResourceDecoded(decoded);
    return transcoder.transcode(transformed);
  }

  private Resource decodeResource(DataRewinder rewinder, int width,
      int height, Options options) throws GlideException {
    List exceptions = listPool.acquire();
    try {
      return decodeResourceWithList(rewinder, width, height, options, exceptions);
    } finally {
      listPool.release(exceptions);
    }
  }

  private Resource decodeResourceWithList(DataRewinder rewinder, int width,
      int height, Options options, List exceptions) throws GlideException {
    Resource result = null;
    for (int i = 0, size = decoders.size(); i < size; i++) {
      ResourceDecoder decoder = decoders.get(i);
      try {
        DataType data = rewinder.rewindAndGet();
        if (decoder.handles(data, options)) {
          data = rewinder.rewindAndGet();
          result = decoder.decode(data, width, height, options);
        }
      } catch (IOException e) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
          Log.v(TAG, "Failed to decode data for " + decoder, e);
        }
        exceptions.add(e);
      }

      if (result != null) {
        break;
      }
    }

    if (result == null) {
      throw new GlideException(failureMessage, new ArrayList<>(exceptions));
    }
    return result;
  }

  @Override
  public String toString() {
    return "DecodePath{" + " dataClass=" + dataClass + ", decoders=" + decoders + ", transcoder="
        + transcoder + '}';
  }

  interface DecodeCallback {
    Resource onResourceDecoded(Resource resource);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy