am d7f9473d: am 1817ed30: Merge "Update CodedOutputByteBufferNano to properly handle a malformed surrogate pair with a buffer too small to output a potentially well formed surrogate pair. This behavior mimics that of the ByteBuffer based methods."

* commit 'd7f9473d9e2684e9dfe54db5b32eea6b43bbcc82':
  Update CodedOutputByteBufferNano to properly handle a malformed surrogate pair with a buffer too small to output a potentially well formed surrogate pair. This behavior mimics that of the ByteBuffer based methods.
diff --git a/java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java b/java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java
index 304c042..662f0fa 100644
--- a/java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java
+++ b/java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java
@@ -500,6 +500,13 @@
         bytes[j++] = (byte) (0x80 | (0x3F & (codePoint >>> 6)));
         bytes[j++] = (byte) (0x80 | (0x3F & codePoint));
       } else {
+        // If we are surrogates and we're not a surrogate pair, always throw an
+        // IllegalArgumentException instead of an ArrayOutOfBoundsException.
+        if ((Character.MIN_SURROGATE <= c && c <= Character.MAX_SURROGATE)
+            && (i + 1 == sequence.length()
+                || !Character.isSurrogatePair(c, sequence.charAt(i + 1)))) {
+          throw new IllegalArgumentException("Unpaired surrogate at index " + i);
+        }
         throw new ArrayIndexOutOfBoundsException("Failed writing " + c + " at index " + j);
       }
     }