| 1 | |
package org.jaudiotagger.audio.real; |
| 2 | |
|
| 3 | |
import java.io.ByteArrayInputStream; |
| 4 | |
import java.io.DataInputStream; |
| 5 | |
import java.io.IOException; |
| 6 | |
import java.io.RandomAccessFile; |
| 7 | |
|
| 8 | |
import org.jaudiotagger.audio.exceptions.CannotReadException; |
| 9 | |
import org.jaudiotagger.audio.generic.Utils; |
| 10 | |
|
| 11 | |
public class RealChunk { |
| 12 | |
|
| 13 | |
protected static final String RMF = ".RMF"; |
| 14 | |
protected static final String PROP = "PROP"; |
| 15 | |
protected static final String MDPR = "MDPR"; |
| 16 | |
protected static final String CONT = "CONT"; |
| 17 | |
protected static final String DATA = "DATA"; |
| 18 | |
protected static final String INDX = "INDX"; |
| 19 | |
|
| 20 | |
private final String id; |
| 21 | |
private final int size; |
| 22 | |
private final byte[] bytes; |
| 23 | |
|
| 24 | |
public static RealChunk readChunk(RandomAccessFile raf) |
| 25 | |
throws CannotReadException, IOException { |
| 26 | 388 | final String id = Utils.readString(raf, 4); |
| 27 | 388 | final int size = Utils.readUint32AsInt(raf); |
| 28 | 388 | if (size < 8) { |
| 29 | 0 | throw new CannotReadException( |
| 30 | |
"Corrupt file: RealAudio chunk length at position " |
| 31 | |
+ (raf.getFilePointer() - 4) |
| 32 | |
+ " cannot be less than 8"); |
| 33 | |
} |
| 34 | 388 | if (size > (raf.length() - raf.getFilePointer() + 8)) { |
| 35 | 0 | throw new CannotReadException( |
| 36 | |
"Corrupt file: RealAudio chunk length of " + size |
| 37 | |
+ " at position " + (raf.getFilePointer() - 4) |
| 38 | |
+ " extends beyond the end of the file"); |
| 39 | |
} |
| 40 | 388 | final byte[] bytes = new byte[size - 8]; |
| 41 | 388 | raf.readFully(bytes); |
| 42 | 388 | return new RealChunk(id, size, bytes); |
| 43 | |
} |
| 44 | |
|
| 45 | |
public RealChunk(String id, int size, byte[] bytes) { |
| 46 | 388 | super(); |
| 47 | 388 | this.id = id; |
| 48 | 388 | this.size = size; |
| 49 | 388 | this.bytes = bytes; |
| 50 | 388 | } |
| 51 | |
|
| 52 | |
public DataInputStream getDataInputStream() { |
| 53 | 120 | return new DataInputStream(new ByteArrayInputStream(getBytes())); |
| 54 | |
} |
| 55 | |
|
| 56 | |
public boolean isCONT() { |
| 57 | 148 | return CONT.equals(id); |
| 58 | |
} |
| 59 | |
|
| 60 | |
public boolean isPROP() { |
| 61 | 0 | return PROP.equals(id); |
| 62 | |
} |
| 63 | |
|
| 64 | |
public byte[] getBytes() { |
| 65 | 120 | return bytes; |
| 66 | |
} |
| 67 | |
|
| 68 | |
public String getId() { |
| 69 | 0 | return id; |
| 70 | |
} |
| 71 | |
|
| 72 | |
public int getSize() { |
| 73 | 0 | return size; |
| 74 | |
} |
| 75 | |
|
| 76 | |
@Override |
| 77 | |
public String toString() { |
| 78 | 0 | return id + "\t" + size; |
| 79 | |
} |
| 80 | |
} |