diff options
author | Feng Xiao <xfxyjwf@gmail.com> | 2014-11-27 18:49:11 -0800 |
---|---|---|
committer | Feng Xiao <xfxyjwf@gmail.com> | 2014-11-27 18:49:11 -0800 |
commit | 7f2a9fb1af432a9831b3e6769905601d72c29796 (patch) | |
tree | 78ee10550ec627ad5bb429b2515669b021efab0c /java/src/main | |
parent | bdcdee0a1d58c65854ad7e95245f2301ea4c6707 (diff) | |
parent | a32a1a761bd921ab3f2bfd990052ffb6bb106171 (diff) | |
download | protobuf-7f2a9fb1af432a9831b3e6769905601d72c29796.tar.gz protobuf-7f2a9fb1af432a9831b3e6769905601d72c29796.tar.bz2 protobuf-7f2a9fb1af432a9831b3e6769905601d72c29796.zip |
Merge pull request #102 from fizbin/bytestring_serializable
Make ByteStrings serializable with java serialization.
Diffstat (limited to 'java/src/main')
4 files changed, 37 insertions, 1 deletions
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; /** @@ -123,6 +126,20 @@ class BoundedByteString extends LiteralByteString { } // ================================================================= + // 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 @Override diff --git a/java/src/main/java/com/google/protobuf/ByteString.java b/java/src/main/java/com/google/protobuf/ByteString.java index 996789c7..cff1ee51 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<Byte> { +public abstract class ByteString implements Iterable<Byte>, 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; @@ -772,6 +774,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 @Override |