1
2 package jsdsi.sexp;
3
4 import java.io.IOException;
5 import java.io.OutputStream;
6 import java.io.Serializable;
7 import java.io.Writer;
8
9 /***
10 * A simple string of data.
11 *
12 * @author Sameer Ajmani
13 * @version $Revision: 1.1.6.1 $ $Date: 2005/11/08 03:12:52 $
14 */
15 class SimpleString implements Serializable {
16 private byte data[];
17
18 /***
19 * Creates a new SimpleString.
20 *
21 * @param d the content of the string
22 */
23 SimpleString(byte[] d) {
24 data = d;
25 }
26
27 /***
28 * Create a new SimpleString.
29 *
30 * @param s the content of the string
31 */
32 SimpleString(String s) {
33 data = Sexp.encodeString(s);
34 }
35
36 /***
37 * Returns the content of this string as a String.
38 *
39 * @return the content of this string as a String.
40 */
41 public String toString() {
42 return Sexp.decodeString(data);
43 }
44
45 /***
46 * Returns the content of this string as a byte array.
47 *
48 * @return the content of this string as a byte array.
49 */
50 public byte[] toByteArray() {
51 return data;
52 }
53
54 /***
55 * Writes this string to a byte stream in canonical form.
56 */
57 public void writeCanonical(OutputStream out) throws IOException {
58 out.write(Sexp.encodeString(Integer.toString(data.length)));
59 out.write(':');
60 out.write(data);
61 }
62
63 /***
64 * Writes this string to a character stream in readable form.
65 *
66 * @param offset spaces indented from left.
67 * @param width total width of window, in characters.
68 * @param last spaces reserved on right (e.g., for closing parens).
69 */
70 public void writeReadable(Writer out, int offset, int width, int last)
71 throws IOException {
72 if (canBeToken())
73 writeToken(out, offset, width, last);
74 else if (canBeQuoted())
75 writeQuoted(out, offset, width, last);
76 else if (canBeHex())
77 writeHex(out, offset, width, last);
78 else
79 writeBase64(out, offset, width, last);
80 }
81
82 int getReadableLen() {
83 if (canBeToken())
84 return data.length;
85 else if (canBeQuoted())
86 return data.length + 2;
87 else if (canBeHex())
88 return 2 * data.length + 2;
89 else
90 return 4 * ((data.length + 2) / 3) + 2;
91 }
92
93 boolean canBeToken() {
94 if (!tokenChecked) {
95 tokenChecked = true;
96 tokenResult = canBeTokenImpl();
97 }
98 return tokenResult;
99 }
100 private boolean tokenChecked = false;
101 private boolean tokenResult = false;
102
103 private boolean canBeTokenImpl() {
104 if (data.length == 0)
105 return false;
106 if (Sexp.isDecimalDigit(data[0]))
107 return false;
108 for (int i = 0; i < data.length; i++) {
109 if (!Sexp.isTokenChar(data[i]))
110 return false;
111 }
112 return true;
113 }
114
115 boolean canBeQuoted() {
116 if (!quotedChecked) {
117 quotedChecked = true;
118 quotedResult = canBeQuotedImpl();
119 }
120 return quotedResult;
121 }
122 private boolean quotedChecked = false;
123 private boolean quotedResult = false;
124
125 boolean canBeQuotedImpl() {
126 for (int i = 0; i < data.length; i++) {
127 if (!Sexp.isTokenChar(data[i]) && !(data[i] == ' '))
128 return false;
129 }
130 return true;
131 }
132
133 boolean canBeHex() {
134 return data.length < 8;
135 }
136
137 void writeToken(Writer out, int offset, int width, int last)
138 throws IOException {
139 Sexp.writeWrapped(Sexp.decodeString(data), out, offset, width, last);
140 }
141
142 void writeQuoted(Writer out, int offset, int width, int last)
143 throws IOException {
144 // TODO: encode escape sequences?
145 out.write('\"');
146 writeToken(out, offset, width, last + 1);
147 out.write('\"');
148 }
149
150 void writeHex(Writer out, int offset, int width, int last)
151 throws IOException {
152 // TODO: insert newlines
153 out.write('#');
154 Sexp.writeHex(data, out, offset, width, last);
155 out.write('#');
156 }
157
158 void writeBase64(Writer out, int offset, int width, int last)
159 throws IOException {
160 // TODO: insert newlines
161 out.write('|');
162 Sexp.writeBase64(data, out, offset, width, last);
163 out.write('|');
164 }
165 }
This page was automatically generated by Maven