This repository has been archived by the owner on Jul 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
/
Serializer.java
547 lines (480 loc) · 14.9 KB
/
Serializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
* $Id$
* Copyright (C) 2006 Klaus Reimer <k@ailis.de>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
package de.ailis.pherialize;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import de.ailis.pherialize.exceptions.SerializeException;
/**
* Serializes Java objects in a PHP serializer format string.
*
* @author Klaus Reimer (k.reimer@iplabs.de)
* @version $Revision$
*/
public class Serializer
{
/** The object history for resolving references */
private List history;
/**
* Constructor
*/
public Serializer()
{
super();
this.history = new ArrayList();
}
/**
* Serializes the specified object.
*
* @param object
* The object
* @return The serialized data
*/
public String serialize(Object object)
{
StringBuffer buffer;
buffer = new StringBuffer();
serializeObject(object, buffer);
return buffer.toString();
}
/**
* This method is used internally for recursively scanning the object while
* serializing. It just calls the other serializeObject method defaulting
* to allowing references.
*
* @param object
* The object to serialize
* @param buffer
* The string buffer to append serialized data to
*/
private void serializeObject(Object object, StringBuffer buffer)
{
serializeObject(object, buffer, true);
}
/**
* This method is used internally for recursively scanning the object while
* serializing. If references are allowed or not can be specified with the
* last parameter. For example Array/Map-Keys are not allowed to be a
* reference.
*
* @param object
* The object to serialize
* @param buffer
* The string buffer to append serialized data to
* @param allowReference
* If reference is allowed for this object
*/
private void serializeObject(Object object, StringBuffer buffer,
boolean allowReference)
{
if (object == null)
{
serializeNull(buffer);
}
else if (allowReference && serializeReference(object, buffer))
{
return;
}
else if (object instanceof String)
{
serializeString((String) object, buffer);
}
else if (object instanceof Character)
{
serializeCharacter((Character) object, buffer);
}
else if (object instanceof Integer)
{
serializeInteger(((Integer) object).intValue(), buffer);
}
else if (object instanceof Short)
{
serializeInteger(((Short) object).intValue(), buffer);
}
else if (object instanceof Byte)
{
serializeInteger(((Byte) object).intValue(), buffer);
}
else if (object instanceof Long)
{
serializeLong(((Long) object).longValue(), buffer);
}
else if (object instanceof Double)
{
serializeDouble(((Double) object).doubleValue(), buffer);
}
else if (object instanceof Float)
{
serializeDouble(((Float) object).doubleValue(), buffer);
}
else if (object instanceof Boolean)
{
serializeBoolean((Boolean) object, buffer);
}
else if (object instanceof Mixed)
{
serializeMixed((Mixed) object, buffer);
return;
}
else if (object instanceof Object[])
{
serializeArray((Object[]) object, buffer);
return;
}
else if (object instanceof Collection)
{
serializeCollection((Collection) object, buffer);
return;
}
else if (object instanceof Map)
{
serializeMap((Map) object, buffer);
return;
}
else if (object instanceof Serializable)
{
serializeSerializable((Serializable) object, buffer);
return;
}
else
{
throw new SerializeException("Unable to serialize "
+ object.getClass().getName());
}
this.history.add(object);
}
/**
* Tries to serialize a reference if the specified object was already
* serialized. It returns true in this case. If the object was not
* serialized before then false is returned.
*
* @param object
* The object to serialize
* @param buffer
* The string buffer to append serialized data to
* @return If a reference was serialized or not
*/
private boolean serializeReference(Object object, StringBuffer buffer)
{
Iterator iterator;
int index;
boolean isReference;
// Don't allow references for simple types because here PHP and
// Java are VERY different and the best way it to simply disallow
// References for these types
if (object instanceof Number || object instanceof Boolean ||
object instanceof String)
{
return false;
}
iterator = this.history.iterator();
index = 0;
isReference = false;
while (iterator.hasNext())
{
if (iterator.next() == object)
{
buffer.append("R:");
buffer.append(index + 1);
buffer.append(';');
isReference = true;
break;
}
index++;
}
return isReference;
}
/**
* Serializes the specified mixed object and appends it to the serialization
* buffer.
*
* @param mixed
* The object to serialize
* @param buffer
* The string buffer to append serialized data to
*/
private void serializeMixed(Mixed mixed, StringBuffer buffer)
{
serializeObject(mixed.getValue(), buffer);
}
/**
* Serializes the specified string and appends it to the serialization
* buffer.
*
* @param string
* The string to serialize
* @param buffer
* The string buffer to append serialized data to
*/
private void serializeString(String string, StringBuffer buffer)
{
buffer.append("s:");
buffer.append(string.length());
buffer.append(":\"");
buffer.append(string);
buffer.append("\";");
}
/**
* Serializes the specified character and appends it to the serialization
* buffer.
*
* @param value
* The value to serialize
* @param buffer
* The string buffer to append serialized data to
*/
private void serializeCharacter(Character value, StringBuffer buffer)
{
buffer.append("s:1:\"");
buffer.append(value);
buffer.append("\";");
}
/**
* Adds a serialized NULL to the serialization buffer.
*
* @param buffer
* The string buffer to append serialized data to
*/
private void serializeNull(StringBuffer buffer)
{
buffer.append("N;");
}
/**
* Serializes the specified integer number and appends it to the
* serialization buffer.
*
* @param number
* The integer number to serialize
* @param buffer
* The string buffer to append serialized data to
*/
private void serializeInteger(int number, StringBuffer buffer)
{
buffer.append("i:");
buffer.append(number);
buffer.append(';');
}
/**
* Serializes the specified lonf number and appends it to the serialization
* buffer.
*
* @param number
* The lonf number to serialize
* @param buffer
* The string buffer to append serialized data to
*/
private void serializeLong(long number, StringBuffer buffer)
{
if ((number >= Integer.MIN_VALUE) && (number <= Integer.MAX_VALUE))
{
buffer.append("i:");
}
else
{
buffer.append("d:");
}
buffer.append(number);
buffer.append(';');
}
/**
* Serializes the specfied double number and appends it to the serialization
* buffer.
*
* @param number
* The number to serialize
* @param buffer
* The string buffer to append serialized data to
*/
private void serializeDouble(double number, StringBuffer buffer)
{
buffer.append("d:");
buffer.append(number);
buffer.append(';');
}
/**
* Serializes the specfied boolean and appends it to the serialization
* buffer.
*
* @param value
* The value to serialize
* @param buffer
* The string buffer to append serialized data to
*/
private void serializeBoolean(Boolean value, StringBuffer buffer)
{
buffer.append("b:");
buffer.append(value.booleanValue() ? 1 : 0);
buffer.append(';');
}
/**
* Serializes the specfied collection and appends it to the serialization
* buffer.
*
* @param collection
* The collection to serialize
* @param buffer
* The string buffer to append serialized data to
*/
private void serializeCollection(Collection collection, StringBuffer buffer)
{
Iterator iterator;
int index;
this.history.add(collection);
buffer.append("a:");
buffer.append(collection.size());
buffer.append(":{");
iterator = collection.iterator();
index = 0;
while (iterator.hasNext())
{
serializeObject(Integer.valueOf(index), buffer, false);
this.history.remove(this.history.size() - 1);
serializeObject(iterator.next(), buffer);
index++;
}
buffer.append('}');
}
/**
* Serializes the specfied array and appends it to the serialization
* buffer.
*
* @param array
* The array to serialize
* @param buffer
* The string buffer to append serialized data to
*/
private void serializeArray(Object[] array, StringBuffer buffer)
{
int max;
this.history.add(array);
buffer.append("a:");
max = array.length;
buffer.append(max);
buffer.append(":{");
for (int i = 0; i < max; i++)
{
serializeObject(Integer.valueOf(i), buffer, false);
this.history.remove(this.history.size() - 1);
serializeObject(array[i], buffer);
}
buffer.append('}');
}
/**
* Serializes the specfied map and appends it to the serialization buffer.
*
* @param map
* The map to serialize
* @param buffer
* The string buffer to append serialized data to
*/
private void serializeMap(Map map, StringBuffer buffer)
{
Iterator iterator;
Object key;
this.history.add(map);
buffer.append("a:");
buffer.append(map.size());
buffer.append(":{");
iterator = map.keySet().iterator();
while (iterator.hasNext())
{
key = iterator.next();
serializeObject(key, buffer, false);
this.history.remove(this.history.size() - 1);
serializeObject(map.get(key), buffer);
}
buffer.append('}');
}
/**
* Serializes a serializable object
*
* @param object
* The serializable object
* @param buffer
* The string buffer to append serialized data to
*/
private void serializeSerializable(Serializable object, StringBuffer buffer)
{
String className;
Class c;
Field[] fields;
int i, max;
Field field;
String key;
Object value;
StringBuffer fieldBuffer;
int fieldCount;
this.history.add(object);
c = object.getClass();
className = c.getSimpleName().toLowerCase();
buffer.append("O:");
buffer.append(className.length());
buffer.append(":\"");
buffer.append(className);
buffer.append("\":");
fieldBuffer = new StringBuffer();
fieldCount = 0;
while (c != null)
{
fields = c.getDeclaredFields();
for (i = 0, max = fields.length; i < max; i++)
{
field = fields[i];
if (Modifier.isStatic(field.getModifiers())) continue;
if (Modifier.isVolatile(field.getModifiers())) continue;
try
{
field.setAccessible(true);
key = field.getName();
value = field.get(object);
serializeObject(key, fieldBuffer);
this.history.remove(this.history.size() - 1);
serializeObject(value, fieldBuffer);
fieldCount++;
}
catch (SecurityException e)
{
// Field is just ignored when this exception is thrown
}
catch (IllegalArgumentException e)
{
// Field is just ignored when this exception is thrown
}
catch (IllegalAccessException e)
{
// Field is just ignored when this exception is thrown
}
}
c = c.getSuperclass();
}
buffer.append(fieldCount);
buffer.append(":{");
buffer.append(fieldBuffer);
buffer.append("}");
}
}