1    package com.instantbank.common.uiutils;
2    
3    import java.io.ByteArrayOutputStream;
4    import java.io.IOException;
5    import java.io.FileInputStream;
6    import java.io.File;
7    
8    /**
9     *  Extracts de content of a file as an array of bytes.
10    *
11    * @author InstantBank (Rodrigo Lopez)
12    * @created September 2002
13    */
14   public class ByteArrayFromFile {
15   
16     /**
17      *  Buffer size for bytes reading from the file.
18      */
19     static final int BUFSIZE = 512;
20   
21     /**
22      *  Bytes buffer where the read operation puts read bytes.
23      */
24     private byte[] buff = new byte[BUFSIZE];
25   
26     /**
27      *  Output Stream where read bytes are appended.
28      */
29     private ByteArrayOutputStream outbyte = new ByteArrayOutputStream();
30   
31   
32     /**
33      *  Constructor for the ByteArrayFromFile object.
34      *
35      * @param file Name of the file whose bytes are to be delivered.
36      * @throws IOException
37      */
38     public ByteArrayFromFile(String file) throws IOException {
39       this(new File(file));
40     }
41   
42   
43     /**
44      *  Constructor for the ByteArrayFromFile object.
45      *
46      * @param file File object whose bytes are to be delivered.
47      * @throws IOException
48      */
49   
50     public ByteArrayFromFile(File file) throws IOException {
51       FileInputStream in = new FileInputStream(file);
52       for(int readSize = in.read(buff); readSize >= 0; readSize = in.read(buff)) {
53         outbyte.write(buff, 0, readSize);
54       }
55       in.close();
56     }
57   
58   
59     /**
60      *  Returns the content of the file as an array of bytes.
61      *
62      * @return The bytes value
63      */
64     public byte[] getBytes() {
65       return outbyte.toByteArray();
66     }
67   }
68   
69