01: /*
02: * Created on Nov 11, 2005
03: */
04: package uk.org.ponder.conversion;
05:
06: import uk.org.ponder.streamutil.read.LexUtil;
07: import uk.org.ponder.streamutil.read.PushbackRIS;
08: import uk.org.ponder.streamutil.read.StringRIS;
09: import uk.org.ponder.stringutil.CharWrap;
10: import uk.org.ponder.util.UniversalRuntimeException;
11:
12: public class StringArrayParser implements LeafObjectParser {
13: public static String[] EMPTY_STRINGL = {};
14:
15: public static StringArrayParser instance = new StringArrayParser();
16:
17: public Object parse(String toparse) {
18: PushbackRIS lr = new PushbackRIS(new StringRIS(toparse));
19: int size = LexUtil.readInt(lr);
20: if (size > toparse.length() / 2) {
21: throw UniversalRuntimeException
22: .accumulate(new IllegalArgumentException(),
23: "Possible DOS attack! Impossibly long string array encoded");
24: }
25: String[] togo = new String[size];
26: LexUtil.expect(lr, ":");
27: CharWrap readbuffer = new CharWrap();
28: for (int i = 0; i < size; ++i) {
29: try {
30: int length = LexUtil.readInt(lr);
31: if (size > toparse.length()) {
32: throw UniversalRuntimeException
33: .accumulate(new IllegalArgumentException(),
34: "Possible DOS attack! Impossibly long string encoded");
35: }
36: LexUtil.expect(lr, ":");
37: readbuffer.ensureCapacity(length);
38: lr.read(readbuffer.storage, 0, length);
39: togo[i] = new String(readbuffer.storage, 0, length);
40: } catch (Exception e) {
41: throw UniversalRuntimeException.accumulate(e,
42: "Error reading integer vector at position " + i
43: + " of expected " + size);
44: }
45: }
46: return togo;
47: }
48:
49: public String render(Object torendero) {
50: String[] torender = (String[]) torendero;
51: CharWrap togo = new CharWrap();
52: togo.append(torender.length).append(':');
53: for (int i = 0; i < torender.length; ++i) {
54: String string = torender[i];
55: togo.append(string.length()).append(':').append(string);
56: }
57: return togo.toString();
58: }
59:
60: public Object copy(Object tocopy) {
61: return tocopy;
62: }
63:
64: }
|