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