@@ -222,18 +222,24 @@ public static void initialize() {
222222 Encode .class , "encoding_decode" , RuntimeCode .methodType );
223223 java .lang .invoke .MethodHandle nameHandle = RuntimeCode .lookup .findStatic (
224224 Encode .class , "encoding_name" , RuntimeCode .methodType );
225+ java .lang .invoke .MethodHandle mimeNameHandle = RuntimeCode .lookup .findStatic (
226+ Encode .class , "encoding_mime_name" , RuntimeCode .methodType );
225227 RuntimeCode encodeCode = new RuntimeCode (encodeHandle , null , null );
226228 encodeCode .isStatic = true ;
227229 RuntimeCode decodeCode = new RuntimeCode (decodeHandle , null , null );
228230 decodeCode .isStatic = true ;
229231 RuntimeCode nameCode = new RuntimeCode (nameHandle , null , null );
230232 nameCode .isStatic = true ;
233+ RuntimeCode mimeNameCode = new RuntimeCode (mimeNameHandle , null , null );
234+ mimeNameCode .isStatic = true ;
231235 GlobalVariable .getGlobalCodeRef ("Encode::Encoding::encode" ).set (
232236 new RuntimeScalar (encodeCode ));
233237 GlobalVariable .getGlobalCodeRef ("Encode::Encoding::decode" ).set (
234238 new RuntimeScalar (decodeCode ));
235239 GlobalVariable .getGlobalCodeRef ("Encode::Encoding::name" ).set (
236240 new RuntimeScalar (nameCode ));
241+ GlobalVariable .getGlobalCodeRef ("Encode::Encoding::mime_name" ).set (
242+ new RuntimeScalar (mimeNameCode ));
237243 } catch (NoSuchMethodException | IllegalAccessException e ) {
238244 System .err .println ("Warning: Missing Encode::Encoding method: " + e .getMessage ());
239245 }
@@ -863,9 +869,11 @@ public static RuntimeList find_encoding(RuntimeArray args, int ctx) {
863869
864870 try {
865871 Charset charset = getCharset (encodingName );
866- // Create a blessed hash with the charset name
872+ // Create a blessed hash with both the Perl-canonical Name
873+ // (used by ->name) and the IANA MimeName (used by ->mime_name).
867874 RuntimeHash encObj = new RuntimeHash ();
868- encObj .put ("Name" , new RuntimeScalar (charset .name ()));
875+ encObj .put ("Name" , new RuntimeScalar (perlCanonicalName (charset .name ())));
876+ encObj .put ("MimeName" , new RuntimeScalar (charset .name ()));
869877 RuntimeScalar ref = encObj .createReference ();
870878 ReferenceOperators .bless (ref , new RuntimeScalar ("Encode::Encoding" ));
871879 return ref .getList ();
@@ -875,6 +883,20 @@ public static RuntimeList find_encoding(RuntimeArray args, int ctx) {
875883 }
876884 }
877885
886+ /**
887+ * Maps a Java canonical charset name to Perl Encode's canonical
888+ * encoding name (as returned by C<< $enc->name >>). For encodings
889+ * we don't have a special mapping for, the Java name is returned
890+ * unchanged.
891+ */
892+ private static String perlCanonicalName (String javaName ) {
893+ switch (javaName ) {
894+ case "US-ASCII" : return "ascii" ;
895+ case "UTF-8" : return "utf-8-strict" ;
896+ default : return javaName ;
897+ }
898+ }
899+
878900 /**
879901 * find_mime_encoding($mime_name)
880902 * Looks up an encoding by its MIME name. Delegates to find_encoding
@@ -1004,6 +1026,23 @@ public static RuntimeList encoding_name(RuntimeArray args, int ctx) {
10041026 return hash .get ("Name" ).getList ();
10051027 }
10061028
1029+ /**
1030+ * Encode::Encoding->mime_name()
1031+ * Returns the IANA-registered MIME name of this encoding. Java's
1032+ * canonical Charset name matches the IANA preferred MIME name for
1033+ * the common encodings used here, so we reuse it.
1034+ */
1035+ public static RuntimeList encoding_mime_name (RuntimeArray args , int ctx ) {
1036+ if (args .isEmpty ()) {
1037+ throw new IllegalStateException ("Bad number of arguments for Encode::Encoding::mime_name" );
1038+ }
1039+
1040+ RuntimeScalar self = args .get (0 );
1041+ RuntimeHash hash = (RuntimeHash ) self .value ;
1042+ RuntimeScalar mime = hash .get ("MimeName" );
1043+ return (mime != null && mime .getDefinedBoolean () ? mime : hash .get ("Name" )).getList ();
1044+ }
1045+
10071046 /**
10081047 * from_to($octets, $from_enc, $to_enc [, $check])
10091048 * Converts in-place the octet sequence from one encoding to another.
0 commit comments