Make ByteStrings serializable with java serialization.

pull/102/head
Daniel Martin 10 years ago
parent 99aa0f9e8f
commit 5ff8dc8e00
  1. 17
      java/src/main/java/com/google/protobuf/BoundedByteString.java
  2. 3
      java/src/main/java/com/google/protobuf/ByteString.java
  3. 2
      java/src/main/java/com/google/protobuf/LiteralByteString.java
  4. 16
      java/src/main/java/com/google/protobuf/RopeByteString.java
  5. 16
      java/src/test/java/com/google/protobuf/LiteralByteStringTest.java

@ -30,6 +30,9 @@
package com.google.protobuf; package com.google.protobuf;
import java.io.InvalidObjectException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
/** /**
@ -122,6 +125,20 @@ class BoundedByteString extends LiteralByteString {
targetOffset, numberToCopy); 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 // ByteIterator

@ -34,6 +34,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
@ -57,7 +58,7 @@ import java.util.NoSuchElementException;
* @author carlanton@google.com Carl Haverl * @author carlanton@google.com Carl Haverl
* @author martinrb@google.com Martin Buchholz * @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 * When two strings to be concatenated have a combined length shorter than

@ -51,6 +51,8 @@ import java.util.NoSuchElementException;
*/ */
class LiteralByteString extends ByteString { class LiteralByteString extends ByteString {
private static final long serialVersionUID = 1L;
protected final byte[] bytes; protected final byte[] bytes;
/** /**

@ -30,8 +30,10 @@
package com.google.protobuf; package com.google.protobuf;
import java.io.InvalidObjectException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.io.ByteArrayInputStream; 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 // ByteIterator

@ -32,9 +32,12 @@ package com.google.protobuf;
import junit.framework.TestCase; import junit.framework.TestCase;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@ -393,4 +396,17 @@ public class LiteralByteStringTest extends TestCase {
assertSame("empty concatenated with " + classUnderTest + " must give " + classUnderTest, assertSame("empty concatenated with " + classUnderTest + " must give " + classUnderTest,
ByteString.EMPTY.concat(stringUnderTest), stringUnderTest); 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);
}
} }

Loading…
Cancel
Save