FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(storage): use JsonUtils for StorageObject serialization in resumable writes and read channels by lqiu96 · Pull Request #13976 · googleapis/google-cloud-java · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,11 @@
import com.google.common.hash.HashingInputStream;
import com.google.common.io.BaseEncoding;
import com.google.common.primitives.Ints;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.StringReader;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
Expand Down Expand Up @@ -381,7 +378,6 @@ private static String getHeaderValue(@NonNull HttpHeaders headers, @NonNull Stri
@Immutable
static final class ApiaryReadRequest implements Serializable {
private static final long serialVersionUID = -4059435314115374448L;
private static final Gson gson = new Gson();
@NonNull private transient StorageObject object;
@NonNull private final Map<StorageRpc.Option, ?> options;
@NonNull private final ByteRangeSpec byteRangeSpec;
Expand Down Expand Up @@ -450,7 +446,7 @@ private String getObjectJson() {
if (objectJson == null) {
synchronized (this) {
if (objectJson == null) {
objectJson = gson.toJson(object);
objectJson = JsonUtils.objectToJson(object);
}
}
}
Expand All @@ -464,8 +460,7 @@ private void writeObject(ObjectOutputStream out) throws IOException {

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
JsonReader jsonReader = gson.newJsonReader(new StringReader(this.objectJson));
this.object = gson.fromJson(jsonReader, StorageObject.class);
this.object = JsonUtils.jsonToObject(this.objectJson, StorageObject.class);
}
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@
import com.google.cloud.storage.spi.v1.StorageRpc;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.StringReader;
import java.util.Map;
import java.util.Objects;
import org.checkerframework.checker.lock.qual.GuardedBy;
Expand All @@ -37,8 +34,6 @@

final class JsonResumableWrite implements Serializable {
private static final long serialVersionUID = 7934407897802252292L;
private static final Gson gson = new Gson();

@MonotonicNonNull private transient StorageObject object;
@MonotonicNonNull private transient Hasher hasher;
@MonotonicNonNull private transient Crc32cValue<?> cumulativeCrc32c;
Expand Down Expand Up @@ -152,7 +147,7 @@ private String getObjectJson() {
if (objectJson == null) {
synchronized (this) {
if (objectJson == null) {
objectJson = gson.toJson(object);
objectJson = JsonUtils.objectToJson(object);
base64CumulativeCrc32c = Utils.crc32cCodec.encode(cumulativeCrc32c.getValue());
}
}
Expand All @@ -167,8 +162,7 @@ private void writeObject(ObjectOutputStream out) throws IOException {

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
JsonReader jsonReader = gson.newJsonReader(new StringReader(this.objectJson));
this.object = gson.fromJson(jsonReader, StorageObject.class);
this.object = JsonUtils.jsonToObject(this.objectJson, StorageObject.class);
if (base64CumulativeCrc32c != null) {
Integer decode = Utils.crc32cCodec.decode(base64CumulativeCrc32c);
if (decode == 0) {
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ final class JsonUtils {

private JsonUtils() {}

static <T> String objectToJson(T object) {
if (object == null) {
return null;
}
try {
return jop.getJsonFactory().toString(object);
} catch (IOException e) {
throw StorageException.coalesce(e);
}
}

static <T> T jsonToObject(String json, Class<T> clazz) {
if (json == null) {
return null;
}
try {
return jop.parseAndClose(new StringReader(json), clazz);
} catch (IOException e) {
throw StorageException.coalesce(e);
}
}

/**
* Given a GenericJson src, and a list of {@code fieldsForOutput} create a new GenericJson where
* every field specified in {@code fieldsForOutput} is present. If a field exists in {@code src}
Expand Down
Loading

Back | FazBrowse Home | New Git URL