[lug] magic number
    Jason Davis 
    mohadib at openactive.org
       
    Sat Aug  6 22:07:23 MDT 2005
    
    
  
Hello,
So I'm writing an app that uses a files magic number to 
know if it is a MP3. I got it working mostly , however , I think I 
might be confused.
So say I have a Riff-Wave-Mp3 file..
this would be
0       string          RIFF            RIFF (little-endian) data
>8      string          WAVE            \b, WAVE audio
>>20    leshort         85              \b, MPEG Layer 3
exmple:
mohadib at dune:~/music$ file G.\ Love\ \&\ Special\ Sauce/G\ Love\ and\
the\ Special\ Sauce\ -\ Cold\ Beverage.mp3
G. Love & Special Sauce/G Love and the Special Sauce - Cold
Beverage.mp3: RIFF (little-endian) data, WAVE audio, MPEG Layer 3,
stereo 44100 Hz
so , in the above example should the 28th  and 29th byte 
be a short that equals 85?
java test case:
RandomAccessFile raf = new RandomAccessFile(new File(paths[i]) , "r");
byte[] magic = new byte[4];
raf.read(magic);
/* test for string RIFF - 4 bytes starting from 0 */
if(new String(magic , 0 , 4).equals("RIFF")){  //true
  raf.seek(8);  // >8 from 0 is 8 :)
  /* look for string WAVE */
  byte[] shortByte = new byte[4];
  raf.read(shortByte);
  if(new String(shortByte , 0 , 4).equals("WAVE")){ //true
		
    raf.seek(28); // >>20 from 8 , go to byte 28?
    byte[] sb = new byte[2];
    raf.read(sb);
    short[] sa = toShortArray2(sb);
    if(sa[0] == 0x55) System.out.println("GOT MP3"); //false   
    else if(sa[0] == 85) System.out.println("GOT MP3"); //false
}
public static short[] toShortArray2(byte []b) {
  final short[] ret = new short[b.length/2];
  ByteBuffer.wrap(b).asShortBuffer().get(ret);
  return ret;
}
Thanks for looking,
jd
-- 
long l = Long.valueOf(new String(new byte[] {'1' , '2' , '3' ,
'4'})).longValue();
    
    
More information about the LUG
mailing list