package baf.sci; import java.io.*; public class InternalResourceIndex extends ResourceIndex { int resfilenum, loc; /** Creates a new InternalResourceIndex from the given data */ public InternalResourceIndex(int restype, int resnum, int resfilenum, int loc) { super(restype, resnum); this.resfilenum = resfilenum; this.loc = loc; } /** Crates a new InternalResourceIndex, reading all necessary * data off the input strap as it is stored in an SCI * resource.map file. */ public InternalResourceIndex(InputStream in) throws IOException { // Unfortunately, DataInputStream is big-endian and the stream // we're reading is little-endian. We must convert to ints // by hand. int id = in.read(); id |= in.read() << 0x08; if (id == 0xffff) throw new EOFException(); restype = Resource.getType(id); resnum = Resource.getNum(id); loc = in.read(); loc |= in.read() << 0x08; loc |= in.read() << 0x10; loc |= in.read() << 0x18; resfilenum = loc >> 26; loc &= 0x3ffffff; } public byte[] getArray() throws ResourceNotFoundException { try { String filename = ""+resfilenum; while (filename.length() < 3) filename = '0'+filename; filename = "resource."+filename; File f = new File(parent.parent.parent.file, filename); FileInputStream fin = new FileInputStream(f); fin.skip(loc); BufferedInputStream in = new BufferedInputStream(fin); // Read the header int readID = readWord(in); if (readID != Resource.getID(restype, resnum)) { throw new ResourceNotFoundException (restype, resnum, "Index mismatch - "+ Resource.getID(restype, resnum) + " != "+readID); } int compressedsize = readWord(in) - 4; int size = readWord(in); int algorithm = readWord(in); // Read the content byte b[] = new byte[size]; switch (algorithm) { case 0: // no compression in.read(b, 0, size); return b; default: throw new ResourceNotFoundException(restype, resnum, "Unknown compression type "+ algorithm ); } } catch (IOException e) { throw new ResourceNotFoundException(restype, resnum, e.toString()); } } int readWord(InputStream in) throws IOException { int word = in.read(); word |= in.read() << 8; return word; } }