/* * GNTRecord.java * * Created on November 17, 2003, 7:06 PM */ package gnt; import palm.conduit.*; import java.io.*; /** * * @author Mike Kirschman */ public class GNTRecord extends AbstractRecord { // The internal delimeters: private static final String optStart = "&&GNTOPTSTART&&"; private static final String optEnd = "&&GNTOPTEND&&"; private static final String fileStart = "&&GNTFILESTART&&"; private static final String fileEnd = "&&GNTFILEEND&&"; private static final String fileDelim = "&&GNTFILEDELIM&&"; private static GNTOptions options = null; protected static GNTFile[] files = null; private static int recordIdx = 0; /** Creates a new instance of GNTRecord */ public GNTRecord() { } public boolean equals(Object o) { if (o == null) return false; GNTRecord r = (GNTRecord) o; if (! options.equals(r.options)) return false; if (files.length != r.files.length) return false; for (int i = 0; i < files.length; i++) if (! files[i].equals(r.files[i])) return false; return true; } public static void resetRecordCount() { recordIdx = 0; } /** Read the file options and individual file data from the DataInputStream * (used for reading from Palm DB.) (Note: The text of the file is read * in the readTextData method.) * * @param DataOutputStream The stream to write the data to. * @return Nothing. * @throws IOException If the writing to the dataOutputStream throws an * IOException. */ public void readData(DataInputStream dataInputStream) throws IOException { if (recordIdx == 0) { options = new GNTOptions(); options.readData(dataInputStream); files = new GNTFile[options.getNumRecs()]; for (int i = 0; i < files.length; i++) { files[i] = new GNTFile(); files[i].readData(dataInputStream); } } else { files[recordIdx - 1].readFileText(dataInputStream); } recordIdx++; } /** Write the file options and individual file data to the DataOutputStream * (used for printing to Palm DB.) (Note: The text of the file is written * in the writeTextData method.) * * @param DataOutputStream The stream to write the data to. * @return Nothing. * @throws IOException If the writing to the dataOutputStream throws an * IOException. */ public void writeData(DataOutputStream dataOutputStream) throws IOException { // write the options struct options.writeData(dataOutputStream); // write the individual file structs for (int i = 0; i < files.length; i++){ files[i].writeData(dataOutputStream); } } public void printRecord(PrintWriter out, String delimeter) throws IOException{ out.print(optStart); // Write the options data one at a time with a delimeter in between options.printRecord(out, delimeter); out.print(optEnd); out.print(fileStart); // Write each file's header-data one at a time with a delimeter in between for (int i = 0; i < files.length; i++){ files[i].printRecord(out, delimeter); out.print(fileDelim); } out.print(fileEnd); } public void readRecord(String data, String delimeter){ int begIdx, endIdx; // divide the data into the options and file parts begIdx = data.indexOf(optStart) + optStart.length(); endIdx = data.lastIndexOf(optEnd); String optionData = data.substring(begIdx, endIdx); begIdx = data.indexOf(fileStart) + fileStart.length(); endIdx = data.lastIndexOf(fileEnd); String fileData = data.substring(begIdx, endIdx); // Call GNTOptions' readRecord options = new GNTOptions(); options.readRecord(optionData, delimeter); // Create the GNTFile array and read the data files = new GNTFile[options.getNumRecs()]; begIdx = 0; endIdx = fileData.indexOf(fileDelim); for (int i = 0; i < files.length && begIdx > -1; i++){ files[i] = new GNTFile(); files[i].readRecord(fileData.substring(begIdx, endIdx), delimeter); begIdx = endIdx + fileDelim.length(); endIdx = fileData.indexOf(fileDelim, begIdx); } } }