Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
7f2a9fb1
Commit
7f2a9fb1
authored
Nov 28, 2014
by
Feng Xiao
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #102 from fizbin/bytestring_serializable
Make ByteStrings serializable with java serialization.
parents
bdcdee0a
a32a1a76
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
90 additions
and
1 deletion
+90
-1
BoundedByteString.java
.../src/main/java/com/google/protobuf/BoundedByteString.java
+17
-0
ByteString.java
java/src/main/java/com/google/protobuf/ByteString.java
+2
-1
LiteralByteString.java
.../src/main/java/com/google/protobuf/LiteralByteString.java
+2
-0
RopeByteString.java
java/src/main/java/com/google/protobuf/RopeByteString.java
+16
-0
BoundedByteStringTest.java
.../test/java/com/google/protobuf/BoundedByteStringTest.java
+19
-0
LiteralByteStringTest.java
.../test/java/com/google/protobuf/LiteralByteStringTest.java
+16
-0
RopeByteStringTest.java
...src/test/java/com/google/protobuf/RopeByteStringTest.java
+18
-0
No files found.
java/src/main/java/com/google/protobuf/BoundedByteString.java
View file @
7f2a9fb1
...
...
@@ -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
...
...
java/src/main/java/com/google/protobuf/ByteString.java
View file @
7f2a9fb1
...
...
@@ -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
...
...
java/src/main/java/com/google/protobuf/LiteralByteString.java
View file @
7f2a9fb1
...
...
@@ -51,6 +51,8 @@ import java.util.NoSuchElementException;
*/
class
LiteralByteString
extends
ByteString
{
private
static
final
long
serialVersionUID
=
1L
;
protected
final
byte
[]
bytes
;
/**
...
...
java/src/main/java/com/google/protobuf/RopeByteString.java
View file @
7f2a9fb1
...
...
@@ -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
...
...
java/src/test/java/com/google/protobuf/BoundedByteStringTest.java
View file @
7f2a9fb1
...
...
@@ -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
);
}
}
java/src/test/java/com/google/protobuf/LiteralByteStringTest.java
View file @
7f2a9fb1
...
...
@@ -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
);
}
}
java/src/test/java/com/google/protobuf/RopeByteStringTest.java
View file @
7f2a9fb1
...
...
@@ -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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment