package twinfeats.pilot.pdb;import java.io.*;import java.util.*;/** * Class for reading and writing USRobotics/3Com Pilot and IBM WorkPad .pdb * files. * @version     1.0 Dec 17 1997 * @author      Kent L. Smotherman */public class PDB {    public PDB() {    }    /**     * Read and parse a PDB file     * @param filename      Name of PDB file to read     * exception Exception  For any exception during the read process     */    public void readPDB(String filename) throws Exception {        FileInputStream fis=null;        DataInputStream dis=null;        String s;        byte[] b = new byte[32];        int i,l;        short n;        File f=null;        try {            f = new File(filename);            fis = new FileInputStream(f);            dis = new DataInputStream(fis);            dis.read(b,0,32);            mName = new String(b,0);            mFlags = dis.readShort();            mVersion = dis.readShort();            dis.readInt();            dis.readInt();            dis.readInt();            mModNum = dis.readInt();            dis.readInt();            dis.readInt();            dis.read(b,0,4);            mType = new String(b,0,0,4);            dis.read(b,0,4);            mCreator = new String(b,0,0,4);            dis.readInt();            dis.readInt();            n = dis.readShort();            mRecords = new Vector((int)n);            mHdrs = new RecordHdr[n];            for (i=0;i<n;i++) {                mHdrs[i] = new RecordHdr(dis.readInt(),dis.readInt());            }            dis.readShort();            for (i=0;i<n;i++) {                if (i < n-1)    //not last record                    l = mHdrs[i+1].mOffset-mHdrs[i].mOffset;                else                    l = (int)f.length()-mHdrs[i].mOffset;                b = new byte[l];                dis.read(b,0,l);                mRecords.addElement(new PDBRecord(mHdrs[i].mID,b,false));            }        }        catch (Exception e1) {            System.out.println("Error opening "+filename);            throw e1;        }        finally {            try {                if (fis != null)                    fis.close();            }            catch (Exception e2) {            }        }    }    /**     * Write a PDB file to disk     * @param filename      Name of PDB file to write     * exception Exception  For any exception during the write process     */    public void writePDB(String filename) throws Exception {        FileOutputStream fos=null;        DataOutputStream dos=null;        PDBRecord record;        int offset;        try {            fos = new FileOutputStream(filename);            dos = new DataOutputStream(fos);        }        catch (Exception e1) {            String s = "Error opening "+filename;            System.out.println(s);            throw e1;        }        try {            dos.writeBytes(fillField(mName,32,(char)0));            dos.writeShort(mFlags);            dos.writeShort(mVersion);            int t = (int)((new Date()).getTime()/1000)+2082844800;            dos.writeInt(t);            dos.writeInt(t);            dos.writeInt(0);            dos.writeInt(mModNum);            dos.writeInt(0);            dos.writeInt(0);            dos.writeBytes(fillField(mType,4,(char)0));            dos.writeBytes(fillField(mCreator,4,(char)0));            dos.writeInt(0);            dos.writeInt(0);            dos.writeShort((short)mRecords.size());            offset =80+mRecords.size()*8;            for (int i=0;i<mRecords.size();i++) {                record = (PDBRecord)mRecords.elementAt(i);                dos.writeInt(offset);                dos.writeInt(record.getID());                offset += record.getLength();            }            dos.writeShort(0);            for (int i=0;i<mRecords.size();i++) {                record = (PDBRecord)mRecords.elementAt(i);                dos.write(record.getData(),0,record.getLength());            }        }        catch (Exception e2) {            System.out.println("Error writing to "+filename);            throw e2;        }        finally {            dos.close();        }    }    /**     * Utility function to pad a field on the left to a fixed length     * @param data      The field data     * @param length    The fixed field length     * @param c         The character to left-pad the data     */    public static String fillField(String data, int length, char c) {        if (data.length() > length)            return data.substring(0,length);        for (int i=data.length();i<length;i++)            data += c;        return data;    }    /**     * Utility function to make a 16 bit integer into a 2 byte String     * @param data      16 bit integer     * @returns         String     */     public static String makeString(int data) {        byte[] b = new byte[2];        b[0] = (byte)(data>>8);        b[1] = (byte)(data&0xff);        return new String(b);     }         /**     * Retrieve the name of the PDB file that was read     * @returns         Name of PDB file read     */    public String getName() {        return mName;    }        /**     * Retrieve the PDB header flags     * @returns          header flags     */    public short getFlags() {        return mFlags;    }        /**     * Retrieve the PDB version     * @returns          version     */    public short getVersion () {        return mVersion;    }        /**     * Retrieve the PDB modification number     * @returns         modification number     */    public int getModNum() {        return mModNum;    }        /**     * Retrieve the PDB type     * @returns         type     */    public String getType() {        return mType;    }        /**     * Retrieve the PDB creator     * @returns         creator     */    public String getCreator() {        return mCreator;    }        /**     * Retrieve vector of all PDB records     * @returns         Vector of PDBRecord's     */    public Vector getRecords() {        return mRecords;    }        String  mName;    short   mFlags;    short   mVersion;    int     mModNum;    String  mType;    String  mCreator;    public Vector  mRecords;    RecordHdr[] mHdrs;}class RecordHdr {    public RecordHdr(int offset, int id) {        UpdateRecordHdr(offset,id);    }    public void UpdateRecordHdr(int offset,int id) {        mOffset = offset;        mID = id;    }    int mOffset;    int mID;}
