01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.components;
04:
05: import java.text.DecimalFormat;
06: import java.io.*;
07: import fitnesse.util.StreamReader;
08: import fit.Counts;
09:
10: public class FitProtocol {
11: public static final DecimalFormat format = new DecimalFormat(
12: "0000000000");
13:
14: public static void writeData(String data, OutputStream output)
15: throws Exception {
16: byte[] bytes = data.getBytes("UTF-8");
17: writeData(bytes, output);
18: }
19:
20: public static void writeData(byte[] bytes, OutputStream output)
21: throws IOException {
22: int length = bytes.length;
23: writeSize(length, output);
24: output.write(bytes);
25: output.flush();
26: }
27:
28: public static void writeSize(int length, OutputStream output)
29: throws IOException {
30: String formattedLength = format.format(length);
31: byte[] lengthBytes = formattedLength.getBytes();
32: output.write(lengthBytes);
33: output.flush();
34: }
35:
36: public static void writeCounts(Counts count, OutputStream output)
37: throws IOException {
38: writeSize(0, output);
39: writeSize(count.right, output);
40: writeSize(count.wrong, output);
41: writeSize(count.ignores, output);
42: writeSize(count.exceptions, output);
43: }
44:
45: public static int readSize(StreamReader reader) throws Exception {
46: String sizeString = reader.read(10);
47: if (sizeString.length() < 10)
48: throw new Exception(
49: "A size value could not be read. Fragment=|"
50: + sizeString + "|");
51: else
52: return format.parse(sizeString).intValue();
53: }
54:
55: public static String readDocument(StreamReader reader, int size)
56: throws Exception {
57: return reader.read(size);
58: }
59:
60: public static Counts readCounts(StreamReader reader)
61: throws Exception {
62: Counts counts = new Counts();
63: counts.right = readSize(reader);
64: counts.wrong = readSize(reader);
65: counts.ignores = readSize(reader);
66: counts.exceptions = readSize(reader);
67: return counts;
68: }
69: }
|