dsmiley commented on code in PR #4653:
URL: https://github.com/apache/solr/pull/4653#discussion_r4732317859


##########
solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java:
##########
@@ -1068,28 +1074,62 @@ public void writeStr(CharSequence s) throws IOException 
{
       return;
     }
     int end = s.length();
-    int maxSize = end * ByteUtils.MAX_UTF8_BYTES_PER_CHAR;
+    int maxSize = end * MAX_UTF8_BYTES_PER_CHAR;
 
     if (maxSize <= MAX_UTF8_SIZE_FOR_ARRAY_GROW_STRATEGY) {
-      if (bytes == null || bytes.length < maxSize) {
-        int bufferSize = getBufferSize(maxSize);
-        bytes = new byte[bufferSize];
-      }
-
-      int sz = ByteUtils.UTF16toUTF8(s, 0, end, bytes, 0);
-      writeTag(STR, sz);
-      daos.write(bytes, 0, sz);
+      byte[] utf8 = s.toString().getBytes(StandardCharsets.UTF_8);
+      writeTag(STR, utf8.length);
+      daos.write(utf8);
     } else {
       // double pass logic for large strings, see SOLR-7971
-      int sz = ByteUtils.calcUTF16toUTF8Length(s, 0, end);
-      writeTag(STR, sz);
+      CharBuffer cb = CharBuffer.wrap(s);
+      // Replace malformed input (unpaired surrogates) as String.getBytes() 
does. Otherwise, the
+      // encoder reports an error without consuming the input.
+      CharsetEncoder encoder =
+          StandardCharsets.UTF_8
+              .newEncoder()
+              .onMalformedInput(CodingErrorAction.REPLACE)
+              .onUnmappableCharacter(CodingErrorAction.REPLACE);

Review Comment:
   This can be extracted to an instance field so we don't create one of these 
each time.  It's not thread-safe but nor is JavaBinCodec.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to