Subversion Repositories svn.mios

Rev

Rev 628 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
628 adamjking 1
/*
2
 * @(#)SysexFile.java   beta8   2006/04/23
3
 *
4
 * Copyright (C) 2008    Adam King
5
 *
6
 * This application is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This application is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this application; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
 
21
package org.midibox.midi;
22
 
23
import java.io.File;
24
import java.io.FileInputStream;
25
import java.io.IOException;
26
import java.io.InputStream;
27
 
28
public class SysexFile {
29
    private byte[] data;
30
 
31
    public boolean read(String sFilename) {
32
        try {
33
 
34
            File file = new File(sFilename);
35
 
36
            InputStream is = new FileInputStream(file);
37
 
38
            // Get the size of the file
39
            long length = file.length();
40
 
41
            // You cannot create an array using a long type.
42
            // It needs to be an int type.
43
            // Before converting to an int type, check
44
            // to ensure that file is not larger than Integer.MAX_VALUE.
45
            if (length > Integer.MAX_VALUE) {
46
                // File is too large
47
            }
48
 
49
            // Create the byte array to hold the data
50
            data = new byte[(int) length];
51
 
52
            // Read in the bytes
53
            int offset = 0;
54
            int numRead = 0;
55
            while (offset < data.length
56
                    && (numRead = is.read(data, offset, data.length - offset)) >= 0) {
57
                offset += numRead;
58
            }
59
 
60
            // Ensure all the bytes have been read in
61
            if (offset < data.length) {
62
                throw new IOException("Could not completely read file "
63
                        + file.getName());
64
            }
65
 
66
            // Close the input stream and return bytes
67
            is.close();
68
        } catch (Exception e) {
69
            System.out.println(e.toString());
70
            return false;
71
        }
72
        return true;
73
    }
74
 
75
    public byte[] getData() {
76
        return data;
77
    }
846 tk 78
 
628 adamjking 79
}