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/BoundedByteString.java | 17 +++++++++++++++++ java/src/main/java/com/google/protobuf/ByteString.java | 3 ++- .../java/com/google/protobuf/LiteralByteString.java | 2 ++ .../main/java/com/google/protobuf/RopeByteString.java | 16 ++++++++++++++++ .../java/com/google/protobuf/LiteralByteStringTest.java | 16 ++++++++++++++++ 5 files changed, 53 insertions(+), 1 deletion(-) (limited to 'java/src') diff --git a/java/src/main/java/com/google/protobuf/BoundedByteString.java b/java/src/main/java/com/google/protobuf/BoundedByteString.java index 2828e9c7..8cb6f463 100644 --- a/java/src/main/java/com/google/protobuf/BoundedByteString.java +++ b/java/src/main/java/com/google/protobuf/BoundedByteString.java @@ -30,6 +30,9 @@ package com.google.protobuf; +import java.io.InvalidObjectException; +import java.io.IOException; +import java.io.ObjectInputStream; import java.util.NoSuchElementException; /** @@ -122,6 +125,20 @@ class BoundedByteString extends LiteralByteString { targetOffset, numberToCopy); } + // ================================================================= + // Serializable + + private static final long serialVersionUID = 1L; + + Object writeReplace() { + return new LiteralByteString(toByteArray()); + } + + private void readObject(ObjectInputStream in) throws IOException { + throw new InvalidObjectException( + "BoundedByteStream instances are not to be serialized directly"); + } + // ================================================================= // ByteIterator diff --git a/java/src/main/java/com/google/protobuf/ByteString.java b/java/src/main/java/com/google/protobuf/ByteString.java index 7da56127..bd9c2b55 100644 --- a/java/src/main/java/com/google/protobuf/ByteString.java +++ b/java/src/main/java/com/google/protobuf/ByteString.java @@ -34,6 +34,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.util.ArrayList; @@ -57,7 +58,7 @@ import java.util.NoSuchElementException; * @author carlanton@google.com Carl Haverl * @author martinrb@google.com Martin Buchholz */ -public abstract class ByteString implements Iterable { +public abstract class ByteString implements Iterable, Serializable { /** * When two strings to be concatenated have a combined length shorter than diff --git a/java/src/main/java/com/google/protobuf/LiteralByteString.java b/java/src/main/java/com/google/protobuf/LiteralByteString.java index 127c574d..767b9f35 100644 --- a/java/src/main/java/com/google/protobuf/LiteralByteString.java +++ b/java/src/main/java/com/google/protobuf/LiteralByteString.java @@ -51,6 +51,8 @@ import java.util.NoSuchElementException; */ class LiteralByteString extends ByteString { + private static final long serialVersionUID = 1L; + protected final byte[] bytes; /** diff --git a/java/src/main/java/com/google/protobuf/RopeByteString.java b/java/src/main/java/com/google/protobuf/RopeByteString.java index d1655b80..fa23c9dd 100644 --- a/java/src/main/java/com/google/protobuf/RopeByteString.java +++ b/java/src/main/java/com/google/protobuf/RopeByteString.java @@ -30,8 +30,10 @@ package com.google.protobuf; +import java.io.InvalidObjectException; import java.io.IOException; import java.io.InputStream; +import java.io.ObjectInputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.io.ByteArrayInputStream; @@ -771,6 +773,20 @@ class RopeByteString extends ByteString { } } + // ================================================================= + // Serializable + + private static final long serialVersionUID = 1L; + + Object writeReplace() { + return new LiteralByteString(toByteArray()); + } + + private void readObject(ObjectInputStream in) throws IOException { + throw new InvalidObjectException( + "RopeByteStream instances are not to be serialized directly"); + } + // ================================================================= // ByteIterator 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 From a32a1a761bd921ab3f2bfd990052ffb6bb106171 Mon Sep 17 00:00:00 2001 From: Daniel Martin Date: Thu, 27 Nov 2014 07:10:02 -0500 Subject: Add tests for other ByteString subclasses --- .../com/google/protobuf/BoundedByteStringTest.java | 19 +++++++++++++++++++ .../java/com/google/protobuf/RopeByteStringTest.java | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+) (limited to 'java/src') diff --git a/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java b/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java index 2b71cfbc..6c9596ca 100644 --- a/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java @@ -30,8 +30,14 @@ package com.google.protobuf; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.io.UnsupportedEncodingException; + /** * This class tests {@link BoundedByteString}, which extends {@link LiteralByteString}, * by inheriting the tests from {@link LiteralByteStringTest}. The only method which @@ -65,4 +71,17 @@ public class BoundedByteStringTest extends LiteralByteStringTest { assertEquals(classUnderTest + " unicode bytes must match", testString.substring(2, testString.length() - 6), roundTripString); } + + 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); + } } diff --git a/java/src/test/java/com/google/protobuf/RopeByteStringTest.java b/java/src/test/java/com/google/protobuf/RopeByteStringTest.java index 9676f527..b3970196 100644 --- a/java/src/test/java/com/google/protobuf/RopeByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/RopeByteStringTest.java @@ -30,6 +30,11 @@ package com.google.protobuf; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.io.UnsupportedEncodingException; import java.util.Arrays; import java.util.Iterator; @@ -112,4 +117,17 @@ public class RopeByteStringTest extends LiteralByteStringTest { assertEquals(classUnderTest + " string must must have same hashCode as the flat string", flatString.hashCode(), unicode.hashCode()); } + + 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