Index: third_party/protobuf/java/core/src/test/java/com/google/protobuf/ByteBufferWriterTest.java |
diff --git a/third_party/protobuf/java/src/main/java/com/google/protobuf/UnsafeByteStrings.java b/third_party/protobuf/java/core/src/test/java/com/google/protobuf/ByteBufferWriterTest.java |
similarity index 56% |
rename from third_party/protobuf/java/src/main/java/com/google/protobuf/UnsafeByteStrings.java |
rename to third_party/protobuf/java/core/src/test/java/com/google/protobuf/ByteBufferWriterTest.java |
index c1997515d0fc8ac03078aecb027c3d1246ff564f..cbe742e5b91f3b5822de7d189d7953d768af4a22 100644 |
--- a/third_party/protobuf/java/src/main/java/com/google/protobuf/UnsafeByteStrings.java |
+++ b/third_party/protobuf/java/core/src/test/java/com/google/protobuf/ByteBufferWriterTest.java |
@@ -30,26 +30,52 @@ |
package com.google.protobuf; |
+import junit.framework.TestCase; |
+ |
+import java.io.ByteArrayOutputStream; |
+import java.io.IOException; |
import java.nio.ByteBuffer; |
+import java.util.Arrays; |
+import java.util.Random; |
/** |
- * Provides unsafe factory methods for {@link ByteString} instances. |
- * |
- * <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 |
- * {@link ByteString} can lead to unexpected and undesirable consequences in your application, |
- * and will likely be difficult to debug. Proceed with caution! |
+ * Tests for {@link ByteBufferWriter}. |
*/ |
-public final class UnsafeByteStrings { |
- private UnsafeByteStrings() {} |
- |
- /** |
- * 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) { |
- return new NioByteString(buffer); |
+public class ByteBufferWriterTest extends TestCase { |
+ |
+ public void testHeapBuffer() throws IOException { |
+ // Test a small and large buffer. |
+ testWrite(ByteBuffer.allocate(100)); |
+ testWrite(ByteBuffer.allocate(1024 * 100)); |
+ } |
+ |
+ public void testDirectBuffer() throws IOException { |
+ // Test a small and large buffer. |
+ testWrite(ByteBuffer.allocateDirect(100)); |
+ testWrite(ByteBuffer.allocateDirect(1024 * 100)); |
+ } |
+ |
+ private void testWrite(ByteBuffer buffer) throws IOException { |
+ fillRandom(buffer); |
+ ByteArrayOutputStream os = new ByteArrayOutputStream(buffer.remaining()); |
+ ByteBufferWriter.write(buffer, os); |
+ assertEquals(0, buffer.position()); |
+ assertTrue(Arrays.equals(toArray(buffer), os.toByteArray())); |
+ } |
+ |
+ private void fillRandom(ByteBuffer buf) { |
+ byte[] bytes = new byte[buf.remaining()]; |
+ new Random().nextBytes(bytes); |
+ buf.put(bytes); |
+ buf.flip(); |
+ return; |
+ } |
+ |
+ private byte[] toArray(ByteBuffer buf) { |
+ int originalPosition = buf.position(); |
+ byte[] bytes = new byte[buf.remaining()]; |
+ buf.get(bytes); |
+ buf.position(originalPosition); |
+ return bytes; |
} |
} |