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

Commit 9c927a5

Browse files
author
Dino Oliva
committed
Updates deserialize to take an InputStream (to match serialize).
1 parent 118c921 commit 9c927a5

File tree

5 files changed

+21
-37
lines changed

5 files changed

+21
-37
lines changed

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

Lines changed: 5 additions & 6 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}.
@@ -26,11 +26,10 @@ public abstract class StatsContextFactory {
2626
* <p>Should be the inverse of {@link StatsContext#serialize(java.io.OutputStream)}. The
2727
* 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/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: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
import com.google.common.testing.EqualsTester;
1919

20+
import java.io.ByteArrayInputStream;
2021
import java.io.ByteArrayOutputStream;
2122
import java.io.IOException;
22-
import java.nio.ByteBuffer;
2323
import org.junit.Test;
2424
import org.junit.runner.RunWith;
2525
import org.junit.runners.JUnit4;
@@ -108,7 +108,7 @@ public void testRecordAllMeasurements() {
108108
}
109109

110110
@Test
111-
public void testSerialize() {
111+
public void testSerialize() throws Exception {
112112
testSerialization(DEFAULT.builder().build());
113113
testSerialization(DEFAULT.with(K1, V1));
114114
testSerialization(DEFAULT.with(K1, V1, K2, V2, K3, V3));
@@ -141,14 +141,10 @@ public void testToString() {
141141
assertThat(DEFAULT.with(K1, V10).toString()).isNotEqualTo(DEFAULT.with(K1, V1).toString());
142142
}
143143

144-
private static void testSerialization(StatsContext expected) {
144+
private static void testSerialization(StatsContext expected) throws Exception {
145145
ByteArrayOutputStream output = new ByteArrayOutputStream();
146-
try {
147-
expected.serialize(output);
148-
} catch (IOException exn) {
149-
// ignore
150-
}
151-
ByteBuffer input = ByteBuffer.wrap(output.toByteArray());
146+
expected.serialize(output);
147+
ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
152148
StatsContext actual = Stats.getStatsContextFactory().deserialize(input);
153149
assertThat(actual).isEqualTo(expected);
154150
}

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/StatsSerializer.java

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

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

18+
import java.io.InputStream;
1919
import java.io.IOException;
2020
import java.io.OutputStream;
21-
import java.nio.ByteBuffer;
2221
import java.util.HashMap;
2322
import java.util.Map.Entry;
24-
import javax.annotation.Nullable;
2523

2624
/**
2725
* Native implementation {@link StatsContext} serialization.
@@ -47,17 +45,9 @@ static void serialize(StatsContextImpl context, OutputStream output) throws IOEx
4745

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

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

0 commit comments

Comments
 (0)