Skip to content
This repository was archived by the owner on Feb 16, 2022. It is now read-only.

Commit 1fe9a63

Browse files
Merge pull request #32 from StaticDefault/master
Increase writer performance
2 parents 2ef9da3 + 5581547 commit 1fe9a63

File tree

5 files changed

+229
-214
lines changed

5 files changed

+229
-214
lines changed

src/main/java/com/realtimetech/kson/element/JsonArray.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.realtimetech.kson.element;
22

3+
import java.io.IOException;
34
import java.util.ArrayList;
45

56
import com.realtimetech.kson.annotation.Ignore;
6-
import com.realtimetech.kson.writer.KsonWriter;
77

88
public class JsonArray extends ArrayList<Object> implements JsonValue {
99

@@ -12,22 +12,18 @@ public class JsonArray extends ArrayList<Object> implements JsonValue {
1212
*/
1313
private static final long serialVersionUID = 5513748119461105760L;
1414

15-
@Ignore
16-
protected KsonWriter ksonWriter = null;
17-
1815
@Override
19-
public String toString(boolean useKsonStandard) {
20-
if (ksonWriter == null)
21-
this.ksonWriter = new KsonWriter();
22-
23-
this.ksonWriter.setUseKson(useKsonStandard);
24-
25-
return this.ksonWriter.toString(this);
16+
public String toString(boolean useKsonStandard) throws IOException {
17+
return WRITER_POOL.writer().toString(this, useKsonStandard);
2618
}
2719

2820
@Override
2921
public String toString() {
30-
return toKsonString();
22+
try {
23+
return toKsonString();
24+
} catch (IOException e) {
25+
return null;
26+
}
3127
}
3228

3329
@Ignore

src/main/java/com/realtimetech/kson/element/JsonObject.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
package com.realtimetech.kson.element;
22

3+
import java.io.IOException;
34
import java.util.HashMap;
45

56
import com.realtimetech.kson.annotation.Ignore;
6-
import com.realtimetech.kson.writer.KsonWriter;
77

88
public class JsonObject extends HashMap<Object, Object> implements JsonValue {
99
/**
1010
* 기본 Serial UID
1111
*/
1212
private static final long serialVersionUID = -6357620110797218097L;
1313

14-
@Ignore
15-
protected KsonWriter ksonWriter = null;
16-
1714
@Override
18-
public String toString(boolean useKsonStandard) {
19-
if (ksonWriter == null)
20-
this.ksonWriter = new KsonWriter();
21-
22-
this.ksonWriter.setUseKson(useKsonStandard);
15+
public String toString(boolean useKsonStandard) throws IOException {
16+
return WRITER_POOL.writer().toString(this, useKsonStandard);
17+
}
2318

24-
return this.ksonWriter.toString(this);
19+
@Override
20+
public String toString() {
21+
try {
22+
return toKsonString();
23+
} catch (IOException e) {
24+
return null;
25+
}
2526
}
2627

2728
@Override
@@ -80,11 +81,6 @@ public boolean containsKey(Object key) {
8081
return result;
8182
}
8283

83-
@Override
84-
public String toString() {
85-
return toKsonString();
86-
}
87-
8884
@Ignore
8985
private int unique = RANDOM.nextInt();
9086

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
package com.realtimetech.kson.element;
22

3+
import java.io.IOException;
34
import java.util.Random;
45

6+
import com.realtimetech.kson.builder.KsonBuilder;
7+
import com.realtimetech.kson.util.pool.KsonPool;
8+
59
public interface JsonValue {
610
static final Random RANDOM = new Random();
11+
static final KsonPool WRITER_POOL = new KsonPool(new KsonBuilder());
712

8-
default public String toKsonString() {
13+
default public String toKsonString() throws IOException {
914
return toString(true);
1015
}
1116

12-
default public String toJsonString() {
17+
default public String toJsonString() throws IOException {
1318
return toString(false);
1419
}
1520

1621
public int unique();
1722

1823
public void unique(int unique);
1924

20-
public String toString(boolean useKsonStandard);
25+
public String toString(boolean useKsonStandard)throws IOException ;
2126

2227
public int actualHash();
2328
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.realtimetech.kson.util.string;
2+
3+
public class StringWriter {
4+
private int raiseSize;
5+
6+
private char[] chars;
7+
8+
private int currentIndex;
9+
private int scope;
10+
11+
private int currentSize;
12+
13+
public StringWriter() {
14+
this(1024);
15+
}
16+
17+
public StringWriter(int raiseSize) {
18+
this.raiseSize = raiseSize;
19+
this.currentIndex = -1;
20+
this.scope = 0;
21+
this.currentSize = 0;
22+
23+
this.raise(0);
24+
}
25+
26+
private void raise(int needSize) {
27+
this.scope = needSize / raiseSize;
28+
29+
char[] oldObjects = this.chars;
30+
31+
this.currentSize = (this.scope + 1) * this.raiseSize;
32+
this.chars = new char[this.currentSize];
33+
34+
if (oldObjects != null) {
35+
System.arraycopy(oldObjects, 0, this.chars, 0, currentIndex + 1);
36+
}
37+
}
38+
39+
public void write(char object) {
40+
int need = this.currentIndex + 1;
41+
if (need >= this.currentSize) {
42+
raise(need);
43+
}
44+
45+
this.currentIndex = need;
46+
47+
this.chars[this.currentIndex] = object;
48+
}
49+
50+
51+
public void write(char[] src) {
52+
this.write(src, 0, src.length);
53+
}
54+
55+
public void write(char[] src, int offset, int length) {
56+
int size = length - offset;
57+
int need = this.currentIndex + size;
58+
if (need >= this.currentSize) {
59+
raise(need);
60+
}
61+
62+
for (int index = 1; index <= size; index++) {
63+
this.chars[this.currentIndex + index] = src[offset + index - 1];
64+
}
65+
66+
this.currentIndex = need;
67+
}
68+
69+
public void reset() {
70+
this.currentIndex = -1;
71+
}
72+
73+
public String toString() {
74+
return new String(this.chars, 0, this.currentIndex + 1);
75+
}
76+
}

0 commit comments

Comments
 (0)