Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,39 @@ public void testJsonHttpContent_wrapped() throws Exception {
assertEquals(SIMPLE_WRAPPED, out.toString("UTF-8"));
}

public void testJsonHttpContent_getLengthBeforeWriteTo() throws Exception {
JsonFactory factory = newFactory();
Simple simple = new Simple();
simple.a = "b";
JsonHttpContent content = new JsonHttpContent(factory, simple);
ByteArrayOutputStream out = new ByteArrayOutputStream();
long length = content.getLength();
content.writeTo(out);
assertEquals(SIMPLE, out.toString("UTF-8"));
assertEquals(length, out.size());
}

public void testJsonHttpContent_getLengthAfterWriteTo() throws Exception {
JsonFactory factory = newFactory();
Simple simple = new Simple();
simple.a = "b";
JsonHttpContent content = new JsonHttpContent(factory, simple);
ByteArrayOutputStream out = new ByteArrayOutputStream();
content.writeTo(out);
assertEquals(SIMPLE, out.toString("UTF-8"));
assertEquals(content.getLength(), out.size());
}

public void testJsonHttpContent_getLengthWhenEmpty() throws Exception {
JsonFactory factory = newFactory();
Simple simple = new Simple();
JsonHttpContent content = new JsonHttpContent(factory, simple);
ByteArrayOutputStream out = new ByteArrayOutputStream();
long length = content.getLength();
content.writeTo(out);
assertEquals(length, 2);
}

public static class V {
@Key
Void v;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.api.client.json.JsonGenerator;
import com.google.api.client.util.Preconditions;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

Expand Down Expand Up @@ -58,6 +59,9 @@ public class JsonHttpContent extends AbstractHttpContent {
/** Wrapper key for the JSON content or {@code null} for none. */
private String wrapperKey;

/** Serialized data in bytes. */
private byte[] bytes;

/**
* @param jsonFactory JSON factory to use
* @param data JSON key name/value data
Expand All @@ -70,6 +74,27 @@ public JsonHttpContent(JsonFactory jsonFactory, Object data) {
}

public void writeTo(OutputStream out) throws IOException {
if (bytes == null) {
serialize();
}
out.write(bytes);
}

/**
* Returns the length of serialized data byte array.
*
* @since 1.29
*/
@Override
public long getLength() throws IOException {
if (bytes == null) {
serialize();
}
return bytes.length;
}

private void serialize() throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
JsonGenerator generator = jsonFactory.createJsonGenerator(out, getCharset());
if (wrapperKey != null) {
generator.writeStartObject();
Expand All @@ -80,6 +105,7 @@ public void writeTo(OutputStream out) throws IOException {
generator.writeEndObject();
}
generator.flush();
this.bytes = out.toByteArray();
}

@Override
Expand Down