LiangliangSui commented on code in PR #1667:
URL: https://github.com/apache/incubator-fury/pull/1667#discussion_r1623412375


##########
java/fury-core/src/main/java/org/apache/fury/resolver/ClassInfo.java:
##########
@@ -86,14 +85,19 @@ public class ClassInfo {
     } else {
       this.fullClassNameBytes = null;
     }
+    // When `classId == ClassResolver.REPLACE_STUB_ID` was established,
+    // means only classes are serialized, not the instance. If we
+    // serialize such class only, we need to write classname bytes.
     if (cls != null
-        && (classId == ClassResolver.NO_CLASS_ID || classId == 
ClassResolver.REPLACE_STUB_ID)) {
+        && ((classId == ClassResolver.NO_CLASS_ID
+                && !classResolver.getFury().getConfig().isMetaShareEnabled())
+            || classId == ClassResolver.REPLACE_STUB_ID)) {
       // REPLACE_STUB_ID for write replace class in `ClassSerializer`.
       Tuple2<String, String> tuple2 = Encoders.encodePkgAndClass(cls);
       this.packageNameBytes =
-          
metaStringResolver.getOrCreateMetaStringBytes(PACKAGE_ENCODER.encode(tuple2.f0));
+          
metaStringResolver.getOrCreateMetaStringBytes(Encoders.encodePackage(tuple2.f0));
       this.classNameBytes =
-          
metaStringResolver.getOrCreateMetaStringBytes(TYPE_NAME_ENCODER.encode(tuple2.f1));
+          
metaStringResolver.getOrCreateMetaStringBytes(Encoders.encodeTypeName(tuple2.f1));

Review Comment:
   I think we should not update `MetaStringBytes#decode` for the following 
reasons:
   
   `Encoders.encodePackage` supports UTF-8 encoding by default, so even if the 
classname contains unicode characters, it can be encoded successfully.
   
   ```java
   static final Encoding[] pkgEncodings =
         new Encoding[] {UTF_8, ALL_TO_LOWER_SPECIAL, 
LOWER_UPPER_DIGIT_SPECIAL};
   
   static final Encoding[] typeNameEncodings =
       new Encoding[] {
         UTF_8, LOWER_UPPER_DIGIT_SPECIAL, FIRST_TO_LOWER_SPECIAL, 
ALL_TO_LOWER_SPECIAL
       };
   ```
   
   I coded a unit test, as follows, which pass
   ```java
   @Test
   public void testEncoding() {
       String str = "你好,世界";
       MetaString metaString = Encoders.encodePackage(str);
       MetaStringBytes metaStringBytes = new MetaStringBytes(metaString);
       String decode = metaStringBytes.decode(Encoders.PACKAGE_DECODER);
       Assert.assertEquals(str, decode);
   }
   ```
   
   > And the key here is that MetaStringBytes can't distinguish whether the 
data is package or classname, so the decode need use the whole encoding set
   
   `MetaStringBytes#decode` uses the encoding stored in 
`MetaStringBytes#hashCode & 0x111`, which means that if the encoding is 
successful, the decoding will be successful. It should have nothing to do with 
the encoding set.
   



-- 
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