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

com.bumptech.glide.load.model.ByteArrayLoader 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.model;

import android.support.annotation.NonNull;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.signature.EmptySignature;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;

/**
 * A base class to convert byte arrays to input streams so they can be decoded. This class is
 * abstract because there is no simple/quick way to generate an id from the bytes themselves, so
 * subclass must include an id.
 *
 * @param  The type of data that will be loaded from a given byte array.
 */
public class ByteArrayLoader implements ModelLoader {
  private final Converter converter;

  public ByteArrayLoader(Converter converter) {
    this.converter = converter;
  }

  @Override
  public LoadData buildLoadData(byte[] model, int width, int height,
      Options options) {
    // TODO: compare the actual bytes?
    return new LoadData<>(EmptySignature.obtain(), new Fetcher<>(model, converter));
  }

  @Override
  public boolean handles(byte[] model) {
    return true;
  }

  /**
   * Converts between a byte array a desired model class.
   * @param  The type of data to convert to.
   */
  public interface Converter {
    Data convert(byte[] model);
    Class getDataClass();
  }

  private static class Fetcher implements DataFetcher {
    private final byte[] model;
    private final Converter converter;

    public Fetcher(byte[] model, Converter converter) {
      this.model = model;
      this.converter = converter;
    }

    @Override
    public void loadData(Priority priority, DataCallback callback) {
      Data result = converter.convert(model);
      callback.onDataReady(result);
    }

    @Override
    public void cleanup() {
      // Do nothing.
    }

    @Override
    public void cancel() {
      // Do nothing.
    }

    @NonNull
    @Override
    public Class getDataClass() {
      return converter.getDataClass();
    }

    @NonNull
    @Override
    public DataSource getDataSource() {
      return DataSource.LOCAL;
    }
  }

  /**
   * Factory for {@link com.bumptech.glide.load.model.ByteArrayLoader} and
   * {@link java.nio.ByteBuffer}.
   */
  public static class ByteBufferFactory implements ModelLoaderFactory {

    @Override
    public ModelLoader build(MultiModelLoaderFactory multiFactory) {
      return new ByteArrayLoader<>(new Converter() {
        @Override
        public ByteBuffer convert(byte[] model) {
          return ByteBuffer.wrap(model);
        }

        @Override
        public Class getDataClass() {
          return ByteBuffer.class;
        }
      });
    }

    @Override
    public void teardown() {
      // Do nothing.
    }
  }

  /**
   * Factory for {@link ByteArrayLoader} and {@link java.io.InputStream}.
   */
  public static class StreamFactory implements ModelLoaderFactory {

    @Override
    public ModelLoader build(MultiModelLoaderFactory multiFactory) {
      return new ByteArrayLoader<>(new Converter() {
        @Override
        public InputStream convert(byte[] model) {
          return new ByteArrayInputStream(model);
        }

        @Override
        public Class getDataClass() {
          return InputStream.class;
        }
      });
    }

    @Override
    public void teardown() {
      // Do nothing.
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy