diff options
author | kenton@google.com <kenton@google.com@630680e5-0e50-0410-840e-4b1c322b438d> | 2009-12-23 01:23:06 +0000 |
---|---|---|
committer | kenton@google.com <kenton@google.com@630680e5-0e50-0410-840e-4b1c322b438d> | 2009-12-23 01:23:06 +0000 |
commit | 35d2f017a7a685d700a8899d06d709cbffaaa885 (patch) | |
tree | 85112b072a34b483904b94d13720b2a003e69c4c /java/src/main/java/com/google/protobuf/ByteString.java | |
parent | 6e8b9e4a4a478c99a93925035b004a3829fd1e4f (diff) | |
download | protobuf-35d2f017a7a685d700a8899d06d709cbffaaa885.tar.gz protobuf-35d2f017a7a685d700a8899d06d709cbffaaa885.tar.bz2 protobuf-35d2f017a7a685d700a8899d06d709cbffaaa885.zip |
In Java's TextFormat, correcty concatenate adjacent string literals, as C++ does. Also fix a bug in handling of single-quoted strings.
Diffstat (limited to 'java/src/main/java/com/google/protobuf/ByteString.java')
-rw-r--r-- | java/src/main/java/com/google/protobuf/ByteString.java | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/java/src/main/java/com/google/protobuf/ByteString.java b/java/src/main/java/com/google/protobuf/ByteString.java index c043df8b..5fade03a 100644 --- a/java/src/main/java/com/google/protobuf/ByteString.java +++ b/java/src/main/java/com/google/protobuf/ByteString.java @@ -36,6 +36,7 @@ import java.io.ByteArrayOutputStream; import java.io.FilterOutputStream; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; +import java.util.List; /** * Immutable array of bytes. @@ -137,6 +138,34 @@ public final class ByteString { } } + /** + * Concatenates all byte strings in the list and returns the result. + * + * <p>The returned {@code ByteString} is not necessarily a unique object. + * If the list is empty, the returned object is the singleton empty + * {@code ByteString}. If the list has only one element, that + * {@code ByteString} will be returned without copying. + */ + public static ByteString copyFrom(List<ByteString> list) { + if (list.size() == 0) { + return EMPTY; + } else if (list.size() == 1) { + return list.get(0); + } + + int size = 0; + for (ByteString str : list) { + size += str.size(); + } + byte[] bytes = new byte[size]; + int pos = 0; + for (ByteString str : list) { + System.arraycopy(str.bytes, 0, bytes, pos, str.size()); + pos += str.size(); + } + return new ByteString(bytes); + } + // ================================================================= // ByteString -> byte[] |