You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2016/05/09 08:49:58 UTC

svn commit: r1742895 - in /commons/proper/csv/trunk/src: changes/changes.xml main/java/org/apache/commons/csv/CSVPrinter.java

Author: ggregory
Date: Mon May  9 08:49:57 2016
New Revision: 1742895

URL: http://svn.apache.org/viewvc?rev=1742895&view=rev
Log:
[CSV-181] Make CSVPrinter.print(Object) GC-free.

Modified:
    commons/proper/csv/trunk/src/changes/changes.xml
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java

Modified: commons/proper/csv/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/changes/changes.xml?rev=1742895&r1=1742894&r2=1742895&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/changes/changes.xml (original)
+++ commons/proper/csv/trunk/src/changes/changes.xml Mon May  9 08:49:57 2016
@@ -38,6 +38,9 @@
     <title>Release Notes</title>
   </properties>
   <body>
+    <release version="1.3.1" date="2016-MM-DD" description="Feature and bug fix release">
+      <action issue="CSV-181" type="update" dev="ggregory" due-to="Gary Gregory">Make CSVPrinter.print(Object) GC-free.</action>
+    </release>
     <release version="1.3" date="2016-MM-DD" description="Feature and bug fix release">
       <action issue="CSV-179" type="add" dev="britter">Add shortcut method for using first record as header to CSVFormat</action>
       <action issue="CSV-180" type="add" dev="britter">Add withHeader(Class&lt;? extends Enum&gt;) to CSVFormat</action>

Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java?rev=1742895&r1=1742894&r2=1742895&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java (original)
+++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java Mon May  9 08:49:57 2016
@@ -121,15 +121,33 @@ public final class CSVPrinter implements
      */
     public void print(final Object value) throws IOException {
         // null values are considered empty
-        String strValue;
+        // Only call CharSequence.toString() if you have to, helps GC-free use cases. 
+        CharSequence charSequence;
         if (value == null) {
             final String nullString = format.getNullString();
-            strValue = nullString == null ? Constants.EMPTY : nullString;
+            charSequence = nullString == null ? Constants.EMPTY : nullString;
         } else {
-            strValue = value.toString();
+            charSequence = value instanceof CharSequence ? (CharSequence) value : value.toString();
         }
-        strValue = format.getTrim() ? strValue.trim() : strValue;
-        this.print(value, strValue, 0, strValue.length());
+        charSequence = format.getTrim() ? trim(charSequence) : charSequence;
+        this.print(value, charSequence, 0, charSequence.length());
+    }
+
+    private CharSequence trim(final CharSequence charSequence) {
+        if (charSequence instanceof String) {
+            return ((String) charSequence).trim();
+        }
+        final int count = charSequence.length();
+        int len = count;
+        int pos = 0;
+
+        while ((pos < len) && (charSequence.charAt(pos) <= ' ')) {
+            pos++;
+        }
+        while ((pos < len) && (charSequence.charAt(len - 1) <= ' ')) {
+            len--;
+        }
+        return (pos > 0) || (len < count) ? charSequence.subSequence(pos, len) : charSequence;
     }
 
     private void print(final Object object, final CharSequence value, final int offset, final int len)