001package org.hl7.fhir.utilities; 002 003import java.io.File; 004import java.io.FileNotFoundException; 005import java.io.IOException; 006import java.io.InputStream; 007 008import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; 009 010 011public abstract class ByteProvider { 012 013 public abstract byte[] getBytes() throws FileNotFoundException, IOException; 014 015 // this one needs to be deprecated - or try to to use it - get to the source 016 public static ByteProvider forStream(InputStream stream) throws IOException { 017 return new ByteProviderBytes(FileUtilities.streamToBytes(stream)); 018 } 019 020 public static ByteProvider forBytes(byte[] bytes) { 021 return new ByteProviderBytes(bytes); 022 } 023 024 public static ByteProvider forFile(File ff) { 025 return new ByteProviderFile(ff); 026 } 027 028 public static ByteProvider forFile(String src) throws IOException { 029 return new ByteProviderFile(ManagedFileAccess.file(src)); 030 } 031 032 private static class ByteProviderBytes extends ByteProvider { 033 034 private byte[] cnt; 035 036 protected ByteProviderBytes(byte[] cnt) { 037 this.cnt = cnt; 038 } 039 040 @Override 041 public byte[] getBytes() { 042 return cnt; 043 } 044 045 } 046 047 private static class ByteProviderFile extends ByteProvider { 048 049 private File file; 050 051 protected ByteProviderFile(File file) { 052 this.file = file; 053 } 054 055 @Override 056 public byte[] getBytes() throws FileNotFoundException, IOException { 057 return FileUtilities.fileToBytes(file); 058 } 059 060 } 061}