aboutsummaryrefslogtreecommitdiff
path: root/java/core/src/main/java/com/google/protobuf/UnsafeByteOperations.java
diff options
context:
space:
mode:
authorBo Yang <teboring@google.com>2016-09-19 13:45:07 -0700
committerBo Yang <teboring@google.com>2016-10-10 11:23:36 -0700
commitcc8ca5b6a5478b40546d4206392eb1471454460d (patch)
treec0b45abfa16d7d373a6ea8f7fe50f1de00ab938e /java/core/src/main/java/com/google/protobuf/UnsafeByteOperations.java
parent337a028bb65ccca4dda768695950b5aba53ae2c9 (diff)
downloadprotobuf-cc8ca5b6a5478b40546d4206392eb1471454460d.tar.gz
protobuf-cc8ca5b6a5478b40546d4206392eb1471454460d.tar.bz2
protobuf-cc8ca5b6a5478b40546d4206392eb1471454460d.zip
Integrate internal changes
Diffstat (limited to 'java/core/src/main/java/com/google/protobuf/UnsafeByteOperations.java')
-rw-r--r--java/core/src/main/java/com/google/protobuf/UnsafeByteOperations.java31
1 files changed, 25 insertions, 6 deletions
diff --git a/java/core/src/main/java/com/google/protobuf/UnsafeByteOperations.java b/java/core/src/main/java/com/google/protobuf/UnsafeByteOperations.java
index e72a6b4d..878c7758 100644
--- a/java/core/src/main/java/com/google/protobuf/UnsafeByteOperations.java
+++ b/java/core/src/main/java/com/google/protobuf/UnsafeByteOperations.java
@@ -67,16 +67,34 @@ public final class UnsafeByteOperations {
/**
* An unsafe operation that returns a {@link ByteString} that is backed by the provided buffer.
*
+ * @param buffer the buffer to be wrapped
+ * @return a {@link ByteString} backed by the provided buffer
+ */
+ public static ByteString unsafeWrap(byte[] buffer) {
+ return ByteString.wrap(buffer);
+ }
+
+ /**
+ * An unsafe operation that returns a {@link ByteString} that is backed by a subregion of the
+ * provided buffer.
+ *
+ * @param buffer the buffer to be wrapped
+ * @param offset the offset of the wrapped region
+ * @param length the number of bytes of the wrapped region
+ * @return a {@link ByteString} backed by the provided buffer
+ */
+ public static ByteString unsafeWrap(byte[] buffer, int offset, int length) {
+ return ByteString.wrap(buffer, offset, length);
+ }
+
+ /**
+ * An unsafe operation that returns a {@link ByteString} that is backed by the provided buffer.
+ *
* @param buffer the Java NIO buffer to be wrapped
* @return a {@link ByteString} backed by the provided buffer
*/
public static ByteString unsafeWrap(ByteBuffer buffer) {
- if (buffer.hasArray()) {
- final int offset = buffer.arrayOffset();
- return ByteString.wrap(buffer.array(), offset + buffer.position(), buffer.remaining());
- } else {
- return new NioByteString(buffer);
- }
+ return ByteString.wrap(buffer);
}
/**
@@ -98,4 +116,5 @@ public final class UnsafeByteOperations {
public static void unsafeWriteTo(ByteString bytes, ByteOutput output) throws IOException {
bytes.writeTo(output);
}
+
}