mirror of
https://github.com/fankes/moshi.git
synced 2025-10-18 23:49:21 +08:00
Save an allocation by using Okio instead of substring.
This commit is contained in:
@@ -19,8 +19,8 @@ import java.io.IOException;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.lang.reflect.Type;
|
||||
import okio.Buffer;
|
||||
import okio.BufferedSink;
|
||||
import okio.BufferedSource;
|
||||
import okio.Sink;
|
||||
|
||||
/**
|
||||
* Converts Java values to JSON, and JSON values to Java.
|
||||
@@ -38,10 +38,9 @@ public abstract class JsonAdapter<T> {
|
||||
|
||||
public abstract void toJson(JsonWriter writer, T value) throws IOException;
|
||||
|
||||
public final void toJson(Sink sink, T value) throws IOException {
|
||||
public final void toJson(BufferedSink sink, T value) throws IOException {
|
||||
JsonWriter writer = new JsonWriter(sink);
|
||||
toJson(writer, value);
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
public final String toJson(T value) throws IOException {
|
||||
|
@@ -19,7 +19,6 @@ import java.io.Closeable;
|
||||
import java.io.Flushable;
|
||||
import java.io.IOException;
|
||||
import okio.BufferedSink;
|
||||
import okio.Okio;
|
||||
import okio.Sink;
|
||||
|
||||
import static com.squareup.moshi.JsonScope.DANGLING_NAME;
|
||||
@@ -181,11 +180,11 @@ public final class JsonWriter implements Closeable, Flushable {
|
||||
/**
|
||||
* Creates a new instance that writes a JSON-encoded stream to {@code sink}.
|
||||
*/
|
||||
public JsonWriter(Sink sink) {
|
||||
public JsonWriter(BufferedSink sink) {
|
||||
if (sink == null) {
|
||||
throw new NullPointerException("sink == null");
|
||||
}
|
||||
this.sink = Okio.buffer(sink);
|
||||
this.sink = sink;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -518,13 +517,13 @@ public final class JsonWriter implements Closeable, Flushable {
|
||||
continue;
|
||||
}
|
||||
if (last < i) {
|
||||
sink.writeUtf8(value.substring(last, i));
|
||||
sink.writeUtf8(value, last, i);
|
||||
}
|
||||
sink.writeUtf8(replacement);
|
||||
last = i + 1;
|
||||
}
|
||||
if (last < length) {
|
||||
sink.writeUtf8(value.substring(last, length));
|
||||
sink.writeUtf8(value, last, length);
|
||||
}
|
||||
sink.writeByte('"');
|
||||
}
|
||||
|
Reference in New Issue
Block a user