avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1552418 - in /avro/trunk: CHANGES.txt lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java
Date Thu, 19 Dec 2013 20:33:05 GMT
Author: cutting
Date: Thu Dec 19 20:33:05 2013
New Revision: 1552418

URL: http://svn.apache.org/r1552418
Log:
AVRO-1348. Java: Improve UTF-8 to String conversion performance in Java 6.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1552418&r1=1552417&r2=1552418&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Thu Dec 19 20:33:05 2013
@@ -27,6 +27,11 @@ Trunk (not yet released)
     AVRO-1379. C: avro_file_writer_append_encoded() function.
     (Mark Teodoro via dcreager)
 
+  OPTIMIZATIONS
+
+    AVRO-1348. Java: Improve UTF-8 to String conversion performance in
+    Java 6. (cutting)
+
   IMPROVEMENTS
 
     AVRO-1355. Java: Reject schemas with duplicate field

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java?rev=1552418&r1=1552417&r2=1552418&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java Thu Dec 19 20:33:05
2013
@@ -18,6 +18,7 @@
 package org.apache.avro.util;
 
 import java.nio.charset.Charset;
+import java.io.UnsupportedEncodingException;
 
 import org.apache.avro.io.BinaryData;
 
@@ -91,11 +92,43 @@ public class Utf8 implements Comparable<
     return this;
   }
 
+  private abstract static class Utf8Converter {
+    public abstract String fromUtf8(byte[] bytes, int length);
+    public abstract byte[] toUtf8(String str);
+  }
+
+  private static final Utf8Converter UTF8_CONVERTER =
+    System.getProperty("java.version").startsWith("1.6.")
+    ? new Utf8Converter() {                       // optimized for Java 6
+        public String fromUtf8(byte[] bytes, int length) {
+          try {
+            return new String(bytes, 0, length, "UTF-8");
+          } catch (UnsupportedEncodingException e) {
+            throw new RuntimeException(e);
+          }
+        }
+        public byte[] toUtf8(String str) {
+          try {
+            return str.getBytes("UTF-8");
+          } catch (UnsupportedEncodingException e) {
+            throw new RuntimeException(e);
+          }
+        }
+      }
+    : new Utf8Converter() {                       // faster in Java 7 & 8
+        public String fromUtf8(byte[] bytes, int length) {
+          return new String(bytes, 0, length, UTF8);
+        }
+        public byte[] toUtf8(String str) {
+          return str.getBytes(UTF8);
+        }
+      };
+
   @Override
   public String toString() {
     if (this.length == 0) return "";
     if (this.string == null) {
-      this.string = new String(bytes, 0, length, UTF8);
+      this.string = UTF8_CONVERTER.fromUtf8(bytes, length);
     }
     return this.string;
   }
@@ -136,7 +169,7 @@ public class Utf8 implements Comparable<
 
   /** Gets the UTF-8 bytes for a String */
   public static final byte[] getBytesFor(String str) {
-    return str.getBytes(UTF8);
+    return UTF8_CONVERTER.toUtf8(str);
   }
 
 }



Mime
View raw message