Lines 59-93
Link Here
|
59 |
write(outputStream, object, null); |
59 |
write(outputStream, object, null); |
60 |
} |
60 |
} |
61 |
|
61 |
|
|
|
62 |
public static void writeInt(OutputStream outputStream, int val) throws IOException { |
63 |
if ((int) ((byte) val & 0xFF) == val) { |
64 |
outputStream.write(T_BYTE); |
65 |
outputStream.write((byte)val); |
66 |
} else if ((int) ((byte) (val >> 8) & 0xFF) == val >> 8) { |
67 |
outputStream.write(T_SHORT); |
68 |
outputStream.write((byte)(val & 0xff)); |
69 |
outputStream.write((byte)((val & 0xff00) >> 8)); |
70 |
} else { |
71 |
outputStream.write(T_INTEGER); |
72 |
outputStream.write((byte)(val & 0xff)); |
73 |
outputStream.write((byte)((val & 0xff00) >> 8)); |
74 |
outputStream.write((byte)((val & 0xff0000) >> 16)); |
75 |
val = (val >> 24) & 0xff; |
76 |
outputStream.write((byte)val); |
77 |
} |
78 |
} |
79 |
|
62 |
public static void write(OutputStream outputStream, Object object, MdrStorage storage) throws IOException { |
80 |
public static void write(OutputStream outputStream, Object object, MdrStorage storage) throws IOException { |
63 |
if (object == null) { |
81 |
if (object == null) { |
64 |
outputStream.write(T_NULL); |
82 |
outputStream.write(T_NULL); |
65 |
|
83 |
|
66 |
} else if (object instanceof String) { |
84 |
} else if (object instanceof String) { |
|
|
85 |
String val = (String)object; |
67 |
outputStream.write(T_STRING); |
86 |
outputStream.write(T_STRING); |
68 |
byte val[] = ((String) object).getBytes("UTF-8"); |
87 |
int len = val.length(); |
69 |
write(outputStream, new Integer(val.length)); |
88 |
writeInt(outputStream, len); |
70 |
outputStream.write(val); |
89 |
for (int i = 0; i < len; i++) { |
71 |
|
90 |
char ch = val.charAt(i); |
72 |
} else if (object instanceof Integer) { |
91 |
if (ch <= 0x7f) { |
73 |
int val = ((Integer) object).intValue(); |
92 |
outputStream.write((byte)ch); |
74 |
int size; |
93 |
} else if (ch <= 0x7ff) { |
75 |
if ((int) ((byte) val & 0xFF) == val) { |
94 |
outputStream.write((byte)(0xc0 | (ch >> 6))); |
76 |
outputStream.write(T_BYTE); |
95 |
outputStream.write((byte)(0x80 | (ch & 0x3f))); |
77 |
size = 1; |
96 |
} else { |
78 |
} else if ((int) ((byte) (val >> 8) & 0xFF) == val >> 8) { |
97 |
outputStream.write((byte)(0xe0 | (ch >> 12))); |
79 |
outputStream.write(T_SHORT); |
98 |
outputStream.write((byte)(0x80 | ((ch >> 6) & 0x3f))); |
80 |
size = 2; |
99 |
outputStream.write((byte)(0x80 | (ch & 0x3f))); |
81 |
} else { |
100 |
} |
82 |
outputStream.write(T_INTEGER); |
|
|
83 |
size = 4; |
84 |
} |
85 |
byte res[] = new byte[size]; |
86 |
for (int i = 0; i < size; i++) { |
87 |
res[i] = (byte) ((val >> (i * 8)) & 0xFF); |
88 |
} |
101 |
} |
89 |
outputStream.write(res); |
102 |
} else if (object instanceof Integer) { |
90 |
|
103 |
int val = ((Integer)object).intValue(); |
|
|
104 |
writeInt(outputStream, val); |
91 |
} else if (object instanceof Boolean) { |
105 |
} else if (object instanceof Boolean) { |
92 |
outputStream.write(T_BOOLEAN); |
106 |
outputStream.write(T_BOOLEAN); |
93 |
outputStream.write(((Boolean) object).booleanValue() ? 1 : 0); |
107 |
outputStream.write(((Boolean) object).booleanValue() ? 1 : 0); |
Lines 174-211
Link Here
|
174 |
return read(inputStream, storable, null); |
188 |
return read(inputStream, storable, null); |
175 |
} |
189 |
} |
176 |
|
190 |
|
|
|
191 |
public static int readInt(InputStream is) throws IOException { |
192 |
return readInt(is, is.read()); |
193 |
} |
194 |
|
195 |
public static int readInt(InputStream inputStream, int type) throws IOException { |
196 |
switch (type) { |
197 |
case T_BYTE: { |
198 |
int ch = inputStream.read(); |
199 |
if (ch < 0) |
200 |
throw new IOException("EOF"); |
201 |
return ch; |
202 |
} |
203 |
case T_SHORT: { |
204 |
int ch1, ch2; |
205 |
ch1 = inputStream.read(); |
206 |
ch2 = inputStream.read(); |
207 |
if ((ch1 | ch2) < 0) { |
208 |
throw new IOException("EOF"); |
209 |
} |
210 |
return (ch2 << 8) | ch1; |
211 |
} |
212 |
case T_INTEGER: { |
213 |
int ch1, ch2, ch3, ch4; |
214 |
ch1 = inputStream.read(); |
215 |
ch2 = inputStream.read(); |
216 |
ch3 = inputStream.read(); |
217 |
ch4 = inputStream.read(); |
218 |
if ((ch1 | ch2 | ch3 | ch4) < 0) { |
219 |
throw new IOException("EOF"); |
220 |
} |
221 |
return (ch4 << 24) + (ch3 << 16) + (ch2 << 8) + ch1; |
222 |
} |
223 |
default: |
224 |
throw new IOException("Unknown int format: " + type); |
225 |
} |
226 |
} |
227 |
|
177 |
public static Object read(InputStream inputStream, StorableBaseObject storable, String className) throws IOException { |
228 |
public static Object read(InputStream inputStream, StorableBaseObject storable, String className) throws IOException { |
178 |
int type; |
229 |
int type; |
179 |
switch (type = inputStream.read()) { |
230 |
switch (type = inputStream.read()) { |
180 |
case T_NULL: { |
231 |
case T_NULL: { |
181 |
return null; |
232 |
return null; |
182 |
} case T_STRING: { |
233 |
} case T_STRING: { |
183 |
int length = ((Integer) read(inputStream, storable)).intValue(); |
234 |
int length = readInt(inputStream); |
184 |
byte val[] = new byte[length]; |
235 |
if (length == 0) |
185 |
inputStream.read(val); |
236 |
return ""; |
186 |
return new String(val, "UTF-8"); |
237 |
StringBuffer sb = new StringBuffer(length); |
187 |
} case T_INTEGER: |
238 |
do { |
188 |
case T_BYTE: |
239 |
int b = inputStream.read() & 0xff; |
189 |
case T_SHORT: { |
240 |
if (b >= 0xe0) { |
190 |
int size; |
241 |
b = (b & 0x0f) << 12; |
191 |
if (type == T_BYTE) { |
242 |
b |= (inputStream.read() & 0x3f) << 6; |
192 |
size = 1; |
243 |
b |= inputStream.read() & 0x3f; |
193 |
} else if (type == T_SHORT) { |
244 |
} else if (b >= 0xc0) { |
194 |
size = 2; |
245 |
b = (b & 0x1f) << 6; |
195 |
} else { |
246 |
b |= inputStream.read() & 0x3f; |
196 |
size = 4; |
247 |
} |
197 |
} |
248 |
sb.append((char)b); |
198 |
byte val[] = new byte[size]; |
249 |
} while (--length > 0); |
199 |
inputStream.read(val); |
250 |
return new String(sb); |
200 |
int res = 0; |
251 |
} |
201 |
// System.out.println("length:"); |
252 |
case T_BYTE: |
202 |
for (int i = 0; i < size; i++) { |
253 |
case T_SHORT: |
203 |
// System.out.println("" + ((256 + val[i]) % 256)); |
254 |
case T_INTEGER: |
204 |
res |= ((256 + val[i]) % 256) << (i * 8); |
255 |
return new Integer(readInt(inputStream, type)); |
205 |
} |
256 |
case T_BOOLEAN: { |
206 |
// System.out.println("" + res); |
|
|
207 |
return new Integer(res); |
208 |
} case T_BOOLEAN: { |
209 |
return new Boolean(inputStream.read() == 1); |
257 |
return new Boolean(inputStream.read() == 1); |
210 |
} case T_MAP: { |
258 |
} case T_MAP: { |
211 |
//if (storable == null) Thread.dumpStack(); |
259 |
//if (storable == null) Thread.dumpStack(); |