Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Unified Diff: third_party/protobuf/java/core/src/main/java/com/google/protobuf/UnsafeByteOperations.java

Issue 1983203003: Update third_party/protobuf to protobuf-v3.0.0-beta-3 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: owners Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/protobuf/java/core/src/main/java/com/google/protobuf/UnsafeByteOperations.java
diff --git a/third_party/protobuf/java/src/main/java/com/google/protobuf/UnsafeByteStrings.java b/third_party/protobuf/java/core/src/main/java/com/google/protobuf/UnsafeByteOperations.java
similarity index 54%
copy from third_party/protobuf/java/src/main/java/com/google/protobuf/UnsafeByteStrings.java
copy to third_party/protobuf/java/core/src/main/java/com/google/protobuf/UnsafeByteOperations.java
index c1997515d0fc8ac03078aecb027c3d1246ff564f..0fbf4d401c404ebd8c4c759cb8c4fd88030a70aa 100644
--- a/third_party/protobuf/java/src/main/java/com/google/protobuf/UnsafeByteStrings.java
+++ b/third_party/protobuf/java/core/src/main/java/com/google/protobuf/UnsafeByteOperations.java
@@ -30,26 +30,55 @@
package com.google.protobuf;
+import java.io.IOException;
import java.nio.ByteBuffer;
/**
- * Provides unsafe factory methods for {@link ByteString} instances.
+ * Provides a number of unsafe byte operations to be used by advanced applications with high
+ * performance requirements. These methods are referred to as "unsafe" due to the fact that they
+ * potentially expose the backing buffer of a {@link ByteString} to the application.
*
* <p><strong>DISCLAIMER:</strong> The methods in this class should only be called if it is
- * guaranteed that the the buffer backing the {@link ByteString} will never change! Mutation of a
+ * guaranteed that the buffer backing the {@link ByteString} will never change! Mutation of a
* {@link ByteString} can lead to unexpected and undesirable consequences in your application,
* and will likely be difficult to debug. Proceed with caution!
*/
-public final class UnsafeByteStrings {
- private UnsafeByteStrings() {}
+@ExperimentalApi
+public final class UnsafeByteOperations {
+ private UnsafeByteOperations() {}
/**
* 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.
+ * @param buffer the Java NIO buffer to be wrapped
+ * @return a {@link ByteString} backed by the provided buffer
*/
public static ByteString unsafeWrap(ByteBuffer buffer) {
- return new NioByteString(buffer);
+ if (buffer.hasArray()) {
+ final int offset = buffer.arrayOffset();
+ return ByteString.wrap(buffer.array(), offset + buffer.position(), buffer.remaining());
+ } else {
+ return new NioByteString(buffer);
+ }
+ }
+
+ /**
+ * Writes the given {@link ByteString} to the provided {@link ByteOutput}. Calling this method may
+ * result in multiple operations on the target {@link ByteOutput}
+ * (i.e. for roped {@link ByteString}s).
+ *
+ * <p>This method exposes the internal backing buffer(s) of the {@link ByteString} to the {@link
+ * ByteOutput} in order to avoid additional copying overhead. It would be possible for a malicious
+ * {@link ByteOutput} to corrupt the {@link ByteString}. Use with caution!
+ *
+ * <p> NOTE: The {@link ByteOutput} <strong>MUST NOT</strong> modify the provided buffers. Doing
+ * so may result in corrupted data, which would be difficult to debug.
+ *
+ * @param bytes the {@link ByteString} to be written
+ * @param output the output to receive the bytes
+ * @throws IOException if an I/O error occurs
+ */
+ public static void unsafeWriteTo(ByteString bytes, ByteOutput output) throws IOException {
+ bytes.writeTo(output);
}
}

Powered by Google App Engine
This is Rietveld 408576698