/** * Helper method loads the next character data into the given CharacterDescriptor * from the given DataInputStream as formatted by a strokes data file. * * @param loadInto the CharacterDescriptor instance to load data into * @param dataStream the stream to load data from * @throws IOException */ private void loadNextCharacterDataFromStream(CharacterDescriptor loadInto) { char character = (char)strokeDataStream.ReadInt16(); // character is the first two bytes int characterType = (int)strokeDataStream.ReadByte(); // character type is the first byte int strokeCount = (int)strokeDataStream.ReadByte(); // number of strokes is next // the number of strokes is deducible from // where we are in the stream, but the stream // wasn't originally ordered by stroke count... int subStrokeCount = 0; double[] directions = loadInto.Directions; double[] lengths = loadInto.Lengths; // format of substroke data is [sub stroke count per stroke]([direction][length])+ // there will be a direction,length pair for each of the substrokes for (int i = 0; i < strokeCount; i++) { // for each stroke // read the number of sub strokes in the stroke int numSubStrokesInStroke = (int)strokeDataStream.ReadByte(); for (int j = 0; j < numSubStrokesInStroke; j++) { // for each sub stroke read out the direction and length double direction = StrokesIO.ReadDirection(strokeDataStream); double length = StrokesIO.ReadLength(strokeDataStream); directions[subStrokeCount] = direction; lengths[subStrokeCount] = length; subStrokeCount++; } } loadInto.Character = character; loadInto.CharacterType = characterType; loadInto.StrokeCount = strokeCount; loadInto.SubStrokeCount = subStrokeCount; }
/** * Writes the entry into the strokes byte array. * Entries are written one after another. There are no delimiting tokens. * The format of an entry in the byte array is as follows: * * 2 bytes for the character * 1 byte for the type (generic, traditional, simplified) * * 1 byte for the number of Strokes * 1 byte for the number of SubStrokes * Because of the above, maximum number of Strokes/SubStrokes is 2^7 - 1 = 127. * This should definitely be enough for Strokes, probably enough for SubStrokes. * In any case, this limitation is less than the limitation imposed by the defined constants currently. * * Then for each Stroke: * 1 byte for the number of SubStrokes in the Stroke * * Then for each SubStroke: * 2 bytes for direction * 2 bytes for length * * Could probably get by with 1 byte for number of Strokes and SubStrokes if needed. * Any change to this method will need to be matched by changes to StrokesRepository#compareToNextInStream. * * @param character the Character that this entry is for * @param type the type of the Character (generic, traditiona, simplified, should be one of the constants) * @param strokeCount the number of Strokes in this Character entry * @param subStrokeCount the number of SubStrokes in this Character entry. */ private void writeStrokeData(BinaryWriter dataOut, char character, int type, int strokeCount, int subStrokeCount) { // Write out the non-SubStroke data. dataOut.Write((short)character); dataOut.Write((byte)type); dataOut.Write((byte)strokeCount); int subStrokeArrayIndex = 0; for (int strokes = 0; strokes < strokeCount; strokes++) { int numSubStrokeForStroke = this.subStrokesPerStroke[strokes]; // Write out the number of SubStrokes in this Stroke. dataOut.Write((byte)numSubStrokeForStroke); for (int substrokes = 0; substrokes < numSubStrokeForStroke; substrokes++) { StrokesIO.WriteDirection(this.subStrokeDirections[subStrokeArrayIndex], dataOut); StrokesIO.WriteLength(this.subStrokeLengths[subStrokeArrayIndex], dataOut); subStrokeArrayIndex++; } } }