2
2
package com .marcospassos .phpserializer ;
3
3
4
4
import java .lang .reflect .Modifier ;
5
+ import java .util .function .Consumer ;
6
+ import com .marcospassos .phpserializer .state .FinishedState ;
7
+ import com .marcospassos .phpserializer .state .WritingSerializableObjectState ;
5
8
import com .marcospassos .phpserializer .state .WritingValueState ;
6
9
7
10
/**
@@ -18,14 +21,16 @@ public class Writer
18
21
private StringBuffer buffer ;
19
22
20
23
/**
21
- * The current state of the writer .
24
+ * The reference counter .
22
25
*/
23
- private WriterState state ;
26
+ private int pointer = 1 ;
24
27
25
28
/**
26
- * The reference counter .
29
+ * The current state of the writer .
27
30
*/
28
- private int pointer = 1 ;
31
+ private WriterState state ;
32
+
33
+ private Writer subWriter ;
29
34
30
35
/**
31
36
* Creates a new writer using an internal buffer.
@@ -60,21 +65,39 @@ public String getResult()
60
65
* Writes a custom serialized object to the buffer.
61
66
*
62
67
* @param className The fully-qualified name of the class.
63
- * @param data The serialized data.
68
+ *
69
+ * @return A writer to be used to custom write the serializable object.
64
70
*/
65
- public void writeObject (String className , String data )
71
+ public Writer writeSerializableObjectStart (String className )
66
72
{
67
- setState (state .value ());
73
+ setState (state .serializableBegin ());
68
74
69
75
buffer .append ("C:" );
70
76
buffer .append (className .length ());
71
77
buffer .append (":\" " );
72
78
buffer .append (className );
73
79
buffer .append ("\" :" );
80
+
81
+ subWriter = new Writer ();
82
+
83
+ return subWriter ;
84
+ }
85
+
86
+ void writeSerializableObjectEnd () {
87
+ setState (state .serializableEnd ());
88
+
89
+ if (!(subWriter .state instanceof FinishedState )) {
90
+ throw new IllegalStateException ();
91
+ }
92
+
93
+ String data = subWriter .getResult ();
94
+
74
95
buffer .append (data .length ());
75
96
buffer .append (":{" );
76
97
buffer .append (data );
77
98
buffer .append ("}" );
99
+
100
+ pointer += subWriter .getPointer ();
78
101
}
79
102
80
103
/**
@@ -336,7 +359,7 @@ public int getPointer()
336
359
*
337
360
* @param state The new state.
338
361
*/
339
- private void setState (WriterState state )
362
+ protected void setState (WriterState state )
340
363
{
341
364
if (state .isReferable ()) {
342
365
pointer ++;
0 commit comments