1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.sentrysoftware.wbem.sblim.cimclient.internal.cim;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 import java.io.IOException;
48 import java.io.StringReader;
49
50
51
52
53
54 public class DTStringReader {
55
56 private String iDateTimeStr;
57
58 private StringReader iReader;
59
60 private int iPos = 0;
61
62 private boolean iUnsignificant;
63
64
65
66
67
68
69 public DTStringReader(String pDateTimeStr) {
70 this.iDateTimeStr = pDateTimeStr;
71 this.iReader = new StringReader(pDateTimeStr);
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86 public int read(int pLen, String pFieldName, boolean pAllowUnsignificant)
87 throws IllegalArgumentException {
88 char[] buf = new char[pLen];
89 int read;
90 try {
91 read = this.iReader.read(buf);
92 this.iPos += pLen;
93 } catch (IOException e) {
94 String msg = "Failed to read " + pFieldName + " field from " + this.iDateTimeStr + '!';
95 throw new IllegalArgumentException(msg);
96 }
97 if (read != pLen) {
98 String msg = "Length of " + pFieldName + " field should be " + pLen + " but only"
99 + read + " characters could be read!";
100 throw new IllegalArgumentException(msg);
101 }
102
103 if (pAllowUnsignificant) {
104 int cnt = 0;
105 for (int i = 0; i < buf.length; i++)
106 if (buf[i] == '*') ++cnt;
107 if (cnt == buf.length) {
108 this.iUnsignificant = true;
109 return -1;
110 }
111 }
112 this.iUnsignificant = false;
113 String field = new String(buf);
114 int res;
115 try {
116 res = Integer.parseInt(field);
117 } catch (NumberFormatException e) {
118 String msg = "Illegal " + pFieldName + " field \"" + field + "\" in \""
119 + this.iDateTimeStr + "\"!";
120 throw new IllegalArgumentException(msg);
121 }
122 if (res < 0) throw new IllegalArgumentException("Negative value is not allowed for "
123 + pFieldName + " in " + this.iDateTimeStr + "!");
124
125 if (field.indexOf('+') != -1) throw new IllegalArgumentException(
126 "Plus sign is not allowed for " + pFieldName + " in " + this.iDateTimeStr + "!");
127 return res;
128 }
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147 public int readAndCheck(int pLen, String pFieldName, int pMin, int pMax,
148 boolean pAllowUnsignificant) throws IllegalArgumentException {
149 int val = read(pLen, pFieldName, pAllowUnsignificant);
150 if (pAllowUnsignificant && val == -1) return val;
151 if (val < pMin || val > pMax) throw new IllegalArgumentException(pFieldName
152 + " must be between " + pMin + " and " + pMax + ", but " + val + " was read from "
153 + this.iDateTimeStr + " !");
154 return val;
155 }
156
157
158
159
160
161
162 public char read() {
163 try {
164 int i = this.iReader.read();
165 if (i > 0) {
166 ++this.iPos;
167 return (char) i;
168 }
169 return 0;
170 } catch (IOException e) {
171 return 0;
172 }
173 }
174
175
176
177
178
179
180
181
182 public void read(char c) throws IllegalArgumentException {
183 if (read() != c) {
184 String msg = "'" + c + "' expected at position " + getPos() + " in "
185 + this.iDateTimeStr + "!";
186 throw new IllegalArgumentException(msg);
187 }
188 }
189
190
191
192
193
194
195 public int getPos() {
196 return this.iPos;
197 }
198
199
200
201
202
203
204 public boolean isUnsignificant() {
205 return this.iUnsignificant;
206 }
207
208 }