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

org.snapscript.common.store.StoreReader Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version
package org.snapscript.common.store;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class StoreReader {

   private final Store store;
   private final int read;
   
   public StoreReader(Store store) {
      this(store, 8192);
   }
   
   public StoreReader(Store store, int read) {
      this.store = store;
      this.read = read;
   }
   
   public byte[] getBytes(String path) {
      InputStream source = store.getInputStream(path);
      
      try {
         if(source != null) {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream(read);
            
            try {
               byte[] array = new byte[read];
               int count = 0;
               
               while((count = source.read(array)) != -1) {
                  buffer.write(array, 0, count);
               }
               return buffer.toByteArray();
            } finally {
               source.close();
            }
         }
      } catch(Exception e) {
         throw new StoreException("Could not read resource '" + path + "'", e);
      }
      return null;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy