| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
package com.drew.imaging.riff; |
| 22 |
|
| 23 |
import com.drew.lang.SequentialReader; |
| 24 |
import com.drew.lang.annotations.NotNull; |
| 25 |
|
| 26 |
import java.io.IOException; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class RiffReader |
| 41 |
{ |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public void processRiff(@NotNull final SequentialReader reader, |
| 52 |
@NotNull final RiffHandler handler) throws RiffProcessingException, IOException |
| 53 |
{ |
| 54 |
|
| 55 |
reader.setMotorolaByteOrder(false); |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
final String fileFourCC = reader.getString(4); |
| 60 |
|
| 61 |
if (!fileFourCC.equals("RIFF")) |
| 62 |
throw new RiffProcessingException("Invalid RIFF header: " + fileFourCC); |
| 63 |
|
| 64 |
|
| 65 |
final int fileSize = reader.getInt32(); |
| 66 |
int sizeLeft = fileSize; |
| 67 |
|
| 68 |
final String identifier = reader.getString(4); |
| 69 |
sizeLeft -= 4; |
| 70 |
|
| 71 |
if (!handler.shouldAcceptRiffIdentifier(identifier)) |
| 72 |
return; |
| 73 |
|
| 74 |
|
| 75 |
processChunks(reader, sizeLeft, handler); |
| 76 |
} |
| 77 |
|
| 78 |
public void processChunks(SequentialReader reader, int sectionSize, RiffHandler handler) throws IOException |
| 79 |
{ |
| 80 |
try { |
| 81 |
while (reader.getPosition() < sectionSize) { |
| 82 |
String fourCC = new String(reader.getBytes(4)); |
| 83 |
int size = reader.getInt32(); |
| 84 |
if (size <= 0) { |
| 85 |
handler.addError("Invalid chunk size: " + size); |
| 86 |
break; |
| 87 |
} |
| 88 |
if (fourCC.equals("LIST") || fourCC.equals("RIFF")) { |
| 89 |
String listName = new String(reader.getBytes(4)); |
| 90 |
if (size < 4) { |
| 91 |
handler.addError("Chunk size too small."); |
| 92 |
} else { |
| 93 |
if (handler.shouldAcceptList(listName)) { |
| 94 |
processChunks(reader, size - 4, handler); |
| 95 |
} else { |
| 96 |
reader.skip(size - 4); |
| 97 |
} |
| 98 |
} |
| 99 |
} else if (fourCC.equals("IDIT")) { |
| 100 |
|
| 101 |
if (size < 2) { |
| 102 |
handler.addError("Chunk size too small."); |
| 103 |
} else { |
| 104 |
handler.processChunk(fourCC, reader.getBytes(size - 2)); |
| 105 |
reader.skip(2); |
| 106 |
} |
| 107 |
} else { |
| 108 |
if (handler.shouldAcceptChunk(fourCC)) { |
| 109 |
|
| 110 |
handler.processChunk(fourCC, reader.getBytes(size)); |
| 111 |
} else { |
| 112 |
reader.skip(size); |
| 113 |
} |
| 114 |
|
| 115 |
if ((size & 1) == 1) { |
| 116 |
reader.skip(1); |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} catch (IOException e) { |
| 121 |
handler.addError(e.getMessage()); |
| 122 |
} |
| 123 |
} |
| 124 |
} |