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

Commit

Permalink
Merge pull request #17 from StaticDefault/master
Browse files Browse the repository at this point in the history
Clean garbages code in KsonWriter
  • Loading branch information
StaticDefaultTester2 authored Oct 8, 2019
2 parents 95e4035 + 0eb4239 commit 1c77aa6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 30 deletions.
7 changes: 5 additions & 2 deletions src/main/java/com/realtimetech/kson/util/stack/FastStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class FastStack<T> {
private T[] objects;

private int currentIndex;
private int currentSize;

private int scope;

private int startIndex;
Expand All @@ -31,7 +33,8 @@ private void raiseArrays() {

T[] oldObjects = this.objects;

this.objects = (T[]) new Object[scope * this.raiseSize];
this.currentSize = this.scope * this.raiseSize;
this.objects = (T[]) new Object[this.currentSize];

if (oldObjects != null) {
for (int i = 0; i <= currentIndex; i++) {
Expand All @@ -41,7 +44,7 @@ private void raiseArrays() {
}

public void push(T object) {
if (this.currentIndex + 2 >= this.scope * this.raiseSize) {
if (this.currentIndex + 2 >= this.currentSize) {
raiseArrays();
}

Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/realtimetech/kson/util/string/StringMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class StringMaker {
private int currentIndex;
private int scope;

private int currentSize;

public StringMaker() {
this(10);
}
Expand All @@ -18,7 +20,8 @@ public StringMaker(int raiseSize) {
this.raiseSize = raiseSize;
this.currentIndex = -1;
this.scope = 0;

this.currentSize = 0;

this.raiseArrays();
}

Expand All @@ -27,7 +30,8 @@ private void raiseArrays() {

char[] oldObjects = this.chars;

this.chars = new char[scope * this.raiseSize];
this.currentSize = this.scope * this.raiseSize;
this.chars = new char[this.currentSize];

if (oldObjects != null) {
for (int i = 0; i <= currentIndex; i++) {
Expand All @@ -37,7 +41,7 @@ private void raiseArrays() {
}

public void add(char object) {
if (this.currentIndex + 2 >= this.scope * this.raiseSize) {
if (this.currentIndex + 2 >= this.currentSize) {
raiseArrays();
}

Expand Down
90 changes: 65 additions & 25 deletions src/main/java/com/realtimetech/kson/writer/KsonWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@
import com.realtimetech.kson.element.JsonObject;
import com.realtimetech.kson.element.JsonValue;
import com.realtimetech.kson.util.stack.FastStack;
import com.realtimetech.kson.util.string.StringMaker;

public class KsonWriter {
private final char[] NULL_CHARS = new char[] { 'n', 'u', 'l', 'l' };

private boolean useKson;

private FastStack<char[]> charsStack;
private StringMaker stringMaker;

private char[] characters;
private char[] stringBuffer;
private int charIndex;

public KsonWriter() {
this.charsStack = new FastStack<char[]>(100);
this.stringMaker = new StringMaker(10);
this.useKson = true;
this.characters = new char[0];
this.stringBuffer = new char[0];
}

public boolean isUseKson() {
Expand All @@ -31,9 +33,11 @@ public void setUseKson(boolean useKson) {

public String toString(JsonValue jsonValue) {
this.charsStack.reset();
int calc = prepareConvert(jsonValue);
int calc = this.prepareConvert(jsonValue);

this.characters = new char[calc];
if(this.characters.length != calc) {
this.characters = new char[calc];
}
this.charIndex = 0;
this.convertString(jsonValue);

Expand All @@ -43,48 +47,84 @@ public String toString(JsonValue jsonValue) {
private char[] convertValueToChars(Object value) {
if (String.class.isInstance(value)) {
String string = (String) value;
stringMaker.reset();

stringMaker.add('\"');

int size = 2;
char[] charArray = string.toCharArray();
for (int i = 0; i < string.length(); i++) {
char character = charArray[i];
switch (character) {
case '"':
stringMaker.add('\\');
stringMaker.add('\"');
size += 2;
break;
case '\\':
size += 2;
break;
case '\b':
size += 2;
break;
case '\f':
size += 2;
break;
case '\n':
size += 2;
break;
case '\r':
size += 2;
break;
case '\t':
size += 2;
break;
default:
size += 1;
}
}

if(this.stringBuffer.length != size) {
this.stringBuffer = new char[size];
}
int index = 0;

this.stringBuffer[index++] = '\"';
for (int i = 0; i < string.length(); i++) {
char character = charArray[i];
switch (character) {
case '"':
this.stringBuffer[index++] = '\\';
this.stringBuffer[index++] = '\"';
break;
case '\\':
stringMaker.add('\\');
stringMaker.add('\\');
this.stringBuffer[index++] = '\\';
this.stringBuffer[index++] = '\\';
break;
case '\b':
stringMaker.add('\\');
stringMaker.add('b');
this.stringBuffer[index++] = '\\';
this.stringBuffer[index++] = 'b';
break;
case '\f':
stringMaker.add('\\');
stringMaker.add('f');
this.stringBuffer[index++] = '\\';
this.stringBuffer[index++] = 'f';
break;
case '\n':
stringMaker.add('\\');
stringMaker.add('n');
this.stringBuffer[index++] = '\\';
this.stringBuffer[index++] = 'n';
break;
case '\r':
stringMaker.add('\\');
stringMaker.add('r');
this.stringBuffer[index++] = '\\';
this.stringBuffer[index++] = 'r';
break;
case '\t':
stringMaker.add('\\');
stringMaker.add('t');
this.stringBuffer[index++] = '\\';
this.stringBuffer[index++] = 't';
break;
default:
stringMaker.add(character);
this.stringBuffer[index++] = character;
}
}
stringMaker.add('\"');
this.stringBuffer[index++] = '\"';

return stringMaker.toArray();
return this.stringBuffer;
} else if (value == null) {
return NULL_CHARS;
} else if (useKson) {
if (value instanceof Float) {
return (value.toString() + "F").toCharArray();
Expand Down

0 comments on commit 1c77aa6

Please sign in to comment.