From 5ff8dc8e005f5312af98655786c1c8438a617efb Mon Sep 17 00:00:00 2001 From: Daniel Martin Date: Tue, 25 Nov 2014 12:58:34 -0500 Subject: Make ByteStrings serializable with java serialization. --- .../java/com/google/protobuf/LiteralByteStringTest.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'java/src/test') diff --git a/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java b/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java index 475f7ffb..ff39ca3f 100644 --- a/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java @@ -32,9 +32,12 @@ package com.google.protobuf; import junit.framework.TestCase; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; @@ -393,4 +396,17 @@ public class LiteralByteStringTest extends TestCase { assertSame("empty concatenated with " + classUnderTest + " must give " + classUnderTest, ByteString.EMPTY.concat(stringUnderTest), stringUnderTest); } + + public void testJavaSerialization() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(out); + oos.writeObject(stringUnderTest); + oos.close(); + byte[] pickled = out.toByteArray(); + InputStream in = new ByteArrayInputStream(pickled); + ObjectInputStream ois = new ObjectInputStream(in); + Object o = ois.readObject(); + assertTrue("Didn't get a ByteString back", o instanceof ByteString); + assertEquals("Should get an equal ByteString back", stringUnderTest, o); + } } -- cgit v1.2.3