Home | History | Annotate | Download | only in res
      1 package com.xtremelabs.robolectric.res;
      2 
      3 import java.io.File;
      4 import java.io.FileInputStream;
      5 import java.io.FileNotFoundException;
      6 import java.io.InputStream;
      7 
      8 public class RawResourceLoader {
      9 
     10     private ResourceExtractor resourceExtractor;
     11     private File resourceDir;
     12 
     13     public RawResourceLoader(ResourceExtractor resourceExtractor, File resourceDir) {
     14         this.resourceExtractor = resourceExtractor;
     15         this.resourceDir = resourceDir;
     16     }
     17 
     18     public InputStream getValue(int resourceId) {
     19         String resourceFileName = resourceExtractor.getResourceName(resourceId);
     20         String resourceName = resourceFileName.substring("/raw".length());
     21 
     22         File rawResourceDir = new File(resourceDir, "raw");
     23 
     24         try {
     25             File[] files = rawResourceDir.listFiles();
     26             for (int i = 0; i < files.length; i++) {
     27                 File file = files[i];
     28                 String name = file.getName();
     29                 int dotIndex = name.indexOf(".");
     30                 String fileBaseName = null;
     31                 if (dotIndex >= 0) {
     32                     fileBaseName = name.substring(0, dotIndex);
     33                 } else {
     34                     fileBaseName = name;
     35                 }
     36                 if (fileBaseName.equals(resourceName)) {
     37                     return new FileInputStream(file);
     38                 }
     39             }
     40         } catch (FileNotFoundException e) {
     41             return null;
     42         }
     43         return null;
     44     }
     45 
     46 }
     47