You will probably find the first three bytes of the MP3 file are:
49 44 33
This is the 'magic number' for an MP3 with ID3v2 tags .... at least according to wikipedia
EDIT
OK, so I have looked at my system, and the MP3's I have contain the magic number:
73 68 51
which in ascii is 'ID3'.
Note that you have some problems with your byte manipulation.... when you test byte values against int values you need to make sure you do the conversion right.... the test:
byte x = ....;
if (x == 255) {...}
will never be true for any value of 'x' because (byte)x
will have the range -128 to +127.
To make this test work you need to do:
if ((x & 0xff) == 255) { .... }
I have modified your method to test things on my system, and have tried a WAV file and a few MP3's. This is the code I have:
public static final String getValid(File f) throws IOException {
FileInputStream fis = new FileInputStream(f);
byte[] buff = new byte[12];
int bytes = 0, pos = 0;
while (pos < buff.length && (bytes = fis.read(buff, pos, buff.length - pos)) > 0) {
pos += bytes;
}
fis.close();
// this is your test.... which should bitmask the value too:
if ((buff[0] & 0x000000ff) == 255) {
return "MP3 " + f;
}
// My testing indicates this is the MP3 magic number
if ( 'I' == (char)buff[0]
&& 'D' == (char)buff[1]
&& '3' == (char)buff[2]) {
return "MP3 ID3 Magic" + f;
}
// This is the magic number from wikipedia (spells '1,!')
if (49 == buff[0] && 44 == buff[1] && 33 == buff[2]) {
return "MP3 ID3v2" + f;
}
if ( 'W' == (char)buff[8]
&& 'A' == (char)buff[9]
&& 'V' == (char)buff[10]
&& 'E' == (char)buff[11]) {
return "WAVE " + f;
}
return "unknown " + f;
}