Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 5f551bd

Browse files
authored
Merge pull request #69 from dinooliva/serialize
Changes serialize() to take an OutputStream as a parameter.
2 parents cba3ca9 + 658232d commit 5f551bd

File tree

8 files changed

+45
-47
lines changed

8 files changed

+45
-47
lines changed

core/java/com/google/instrumentation/stats/StatsContext.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
package com.google.instrumentation.stats;
1515

16-
import java.nio.ByteBuffer;
16+
import java.io.IOException;
17+
import java.io.OutputStream;
1718

1819
/**
1920
* An immutable context for stats operations.
@@ -51,12 +52,12 @@ public final StatsContext with(
5152
/**
5253
* Serializes the {@link StatsContext} into the on-the-wire representation.
5354
*
54-
* <p>The inverse of {@link StatsContextFactory#deserialize(ByteBuffer)} and should be based on
55-
* the {@link StatsContext} protobuf representation.
55+
* <p>The inverse of {@link StatsContextFactory#deserialize(java.io.InputStream)} and should be
56+
* based on the {@link StatsContext} protobuf representation.
5657
*
57-
* @return serialized bytes.
58+
* @param output the {@link OutputStream} to add the serialized form of this {@link StatsContext}.
5859
*/
59-
public abstract ByteBuffer serialize();
60+
public abstract void serialize(OutputStream output) throws IOException;
6061

6162
/**
6263
* Builder for {@link StatsContext}.

core/java/com/google/instrumentation/stats/StatsContextFactory.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
package com.google.instrumentation.stats;
1515

16-
import java.nio.ByteBuffer;
17-
import javax.annotation.Nullable;
16+
import java.io.InputStream;
17+
import java.io.IOException;
1818

1919
/**
2020
* Factory class for {@link StatsContext}.
@@ -23,14 +23,13 @@ public abstract class StatsContextFactory {
2323
/**
2424
* Creates a {@link StatsContext} from the given on-the-wire encoded representation.
2525
*
26-
* <p>Should be the inverse of {@link StatsContext#serialize()}. The serialized representation
27-
* should be based on the {@link StatsContext} protobuf representation.
26+
* <p>Should be the inverse of {@link StatsContext#serialize(java.io.OutputStream)}. The
27+
* serialized representation should be based on the {@link StatsContext} protobuf representation.
2828
*
29-
* @param buffer on-the-wire representation of a {@link StatsContext}
30-
* @return a {@link StatsContext} deserialized from {@code buffer}
29+
* @param input on-the-wire representation of a {@link StatsContext}
30+
* @return a {@link StatsContext} deserialized from {@code input}
3131
*/
32-
@Nullable
33-
public abstract StatsContext deserialize(ByteBuffer buffer);
32+
public abstract StatsContext deserialize(InputStream input) throws IOException;
3433

3534
/**
3635
* Returns the default {@link StatsContext}.

core/java/com/google/instrumentation/stats/StatsManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
*/
2020
public abstract class StatsManager {
2121
/**
22-
* Pull model for stats. Registers a {@link View} that will collect data to be accessed via
23-
* {@link #getView(ViewDescriptor)}.
22+
* Pull model for stats. Registers a {@link ViewDescriptor} that will collect data to be accessed
23+
* via {@link #getView(ViewDescriptor)}.
2424
*/
25-
public abstract void registerView(ViewDescriptor view);
25+
public abstract void registerView(ViewDescriptor viewDescriptor);
2626

2727
/**
2828
* Returns the current stats data, {@link View}, associated with the given {@link ViewDescriptor}.

core/javatests/com/google/instrumentation/stats/StatsContextFactoryTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import static com.google.common.truth.Truth.assertThat;
1717

18-
import java.nio.ByteBuffer;
18+
import java.io.ByteArrayInputStream;
1919
import org.junit.Test;
2020
import org.junit.runner.RunWith;
2121
import org.junit.runners.JUnit4;
@@ -26,8 +26,8 @@
2626
@RunWith(JUnit4.class)
2727
public class StatsContextFactoryTest {
2828
@Test
29-
public void testDeserializeEmpty() {
30-
assertThat(Stats.getStatsContextFactory().deserialize(ByteBuffer.wrap(new byte[0])))
29+
public void testDeserializeEmpty() throws Exception {
30+
assertThat(Stats.getStatsContextFactory().deserialize(new ByteArrayInputStream(new byte[0])))
3131
.isEqualTo(Stats.getStatsContextFactory().getDefault());
3232
}
3333
}

core/javatests/com/google/instrumentation/stats/StatsContextTest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
import static com.google.common.truth.Truth.assertThat;
1717

1818
import com.google.common.testing.EqualsTester;
19+
20+
import java.io.ByteArrayInputStream;
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
1923
import org.junit.Test;
2024
import org.junit.runner.RunWith;
2125
import org.junit.runners.JUnit4;
@@ -104,7 +108,7 @@ public void testRecordAllMeasurements() {
104108
}
105109

106110
@Test
107-
public void testSerialize() {
111+
public void testSerialize() throws Exception {
108112
testSerialization(DEFAULT.builder().build());
109113
testSerialization(DEFAULT.with(K1, V1));
110114
testSerialization(DEFAULT.with(K1, V1, K2, V2, K3, V3));
@@ -137,8 +141,11 @@ public void testToString() {
137141
assertThat(DEFAULT.with(K1, V10).toString()).isNotEqualTo(DEFAULT.with(K1, V1).toString());
138142
}
139143

140-
private static void testSerialization(StatsContext expected) {
141-
StatsContext actual = Stats.getStatsContextFactory().deserialize(expected.serialize());
144+
private static void testSerialization(StatsContext expected) throws Exception {
145+
ByteArrayOutputStream output = new ByteArrayOutputStream();
146+
expected.serialize(output);
147+
ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
148+
StatsContext actual = Stats.getStatsContextFactory().deserialize(input);
142149
assertThat(actual).isEqualTo(expected);
143150
}
144151
}

core_impl/java/com/google/instrumentation/stats/StatsContextFactoryImpl.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
package com.google.instrumentation.stats;
1515

16-
import java.nio.ByteBuffer;
16+
import java.io.InputStream;
17+
import java.io.IOException;
1718
import java.util.HashMap;
18-
import javax.annotation.Nullable;
1919

2020
/**
2121
* Native Implementation of {@link StatsContextFactory}.
@@ -31,9 +31,8 @@ public StatsContextFactoryImpl() {}
3131
* <p>The encoded tags are of the form: {@code <tag prefix> + 'key' + <tag delim> + 'value'}*
3232
*/
3333
@Override
34-
@Nullable
35-
public StatsContextImpl deserialize(ByteBuffer buffer) {
36-
return StatsSerializer.deserialize(buffer);
34+
public StatsContextImpl deserialize(InputStream input) throws IOException {
35+
return StatsSerializer.deserialize(input);
3736
}
3837

3938
@Override

core_impl/java/com/google/instrumentation/stats/StatsContextImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
package com.google.instrumentation.stats;
1515

16-
import java.nio.ByteBuffer;
16+
import java.io.IOException;
17+
import java.io.OutputStream;
1718
import java.util.HashMap;
1819

1920
/**
@@ -42,8 +43,8 @@ public StatsContextImpl record(MeasurementMap stats) {
4243
* <p>The encoded tags are of the form: {@code <tag prefix> + 'key' + <tag delim> + 'value'}*
4344
*/
4445
@Override
45-
public ByteBuffer serialize() {
46-
return StatsSerializer.serialize(this);
46+
public void serialize(OutputStream output) throws IOException {
47+
StatsSerializer.serialize(this, output);
4748
}
4849

4950
@Override

core_impl/java/com/google/instrumentation/stats/StatsSerializer.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
package com.google.instrumentation.stats;
1515

1616
import com.google.instrumentation.stats.proto.StatsContextProto;
17-
import com.google.protobuf.InvalidProtocolBufferException;
1817

19-
import java.nio.ByteBuffer;
18+
import java.io.InputStream;
19+
import java.io.IOException;
20+
import java.io.OutputStream;
2021
import java.util.HashMap;
2122
import java.util.Map.Entry;
22-
import javax.annotation.Nullable;
2323

2424
/**
2525
* Native implementation {@link StatsContext} serialization.
@@ -31,7 +31,7 @@ final class StatsSerializer {
3131

3232
// Serializes a StatsContext by transforming it into a StatsContextProto. The
3333
// encoded tags are of the form: (<tag prefix> + 'key' + <tag delim> + 'value')*
34-
static ByteBuffer serialize(StatsContextImpl context) {
34+
static void serialize(StatsContextImpl context, OutputStream output) throws IOException {
3535
StringBuilder builder = new StringBuilder();
3636
for (Entry<String, String> tag : context.tags.entrySet()) {
3737
builder
@@ -40,23 +40,14 @@ static ByteBuffer serialize(StatsContextImpl context) {
4040
.append(TAG_DELIM)
4141
.append(tag.getValue());
4242
}
43-
return ByteBuffer.wrap(StatsContextProto.StatsContext
44-
.newBuilder().setTags(builder.toString()).build().toByteArray());
43+
StatsContextProto.StatsContext.newBuilder().setTags(builder.toString()).build().writeTo(output);
4544
}
4645

4746
// Deserializes based on an serialized StatsContextProto. The encoded tags are of the form:
4847
// (<tag prefix> + 'key' + <tag delim> + 'value')*
49-
@Nullable
50-
static StatsContextImpl deserialize(ByteBuffer buffer) {
51-
try {
52-
StatsContextProto.StatsContext context =
53-
StatsContextProto.StatsContext.parser().parseFrom(buffer.array());
54-
return new StatsContextImpl(tagsFromString(context.getTags()));
55-
} catch (IllegalArgumentException e) {
56-
return null;
57-
} catch (InvalidProtocolBufferException e) {
58-
return null;
59-
}
48+
static StatsContextImpl deserialize(InputStream input) throws IOException {
49+
StatsContextProto.StatsContext context = StatsContextProto.StatsContext.parseFrom(input);
50+
return new StatsContextImpl(tagsFromString(context.getTags()));
6051
}
6152

6253
private static HashMap<String, String> tagsFromString(String encoded) {

0 commit comments

Comments
 (0)