james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1089427 - in /james/mime4j/trunk: core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java dom/src/test/java/org/apache/james/mime4j/dom/HeaderTest.java
Date Wed, 06 Apr 2011 12:38:15 GMT
Author: olegk
Date: Wed Apr  6 12:38:15 2011
New Revision: 1089427

URL: http://svn.apache.org/viewvc?rev=1089427&view=rev
Log:
Use simple byte to char and char to byte casts when decoding / encoding ASCII elements

Modified:
    james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
    james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java
    james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/HeaderTest.java

Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java?rev=1089427&r1=1089426&r2=1089427&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
(original)
+++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
Wed Apr  6 12:38:15 2011
@@ -24,7 +24,6 @@ import java.util.BitSet;
 import java.util.List;
 
 import org.apache.james.mime4j.MimeException;
-import org.apache.james.mime4j.util.ByteArrayBuffer;
 import org.apache.james.mime4j.util.ByteSequence;
 import org.apache.james.mime4j.util.ContentUtil;
 
@@ -252,7 +251,11 @@ public class RawFieldParser {
     }
     
     private static String copy(final ByteSequence buf, int beginIndex, int endIndex) {
-        return ContentUtil.decode(buf, beginIndex, endIndex - beginIndex);
+        StringBuilder strbuf = new StringBuilder(endIndex - beginIndex);
+        for (int i = beginIndex; i < endIndex; i++) {
+            strbuf.append((char) (buf.byteAt(i) & 0xff));
+        }
+        return strbuf.toString();
     }
 
     private static String copyTrimmed(final ByteSequence buf, int beginIndex, int endIndex)
{
@@ -262,29 +265,29 @@ public class RawFieldParser {
         while (endIndex > beginIndex && isWhitespace(buf.byteAt(endIndex - 1)))
{
             endIndex--;
         }
-        return ContentUtil.decode(buf, beginIndex, endIndex - beginIndex);
+        return copy(buf, beginIndex, endIndex);
     }
 
     private static String copyEscaped(final ByteSequence buf, int beginIndex, int endIndex)
{
-        ByteArrayBuffer copy = new ByteArrayBuffer(buf.length());
+        StringBuilder strbuf  = new StringBuilder(endIndex - beginIndex);
         boolean escaped = false;
         for (int i = beginIndex; i < endIndex; i++) {
-            int b = buf.byteAt(i);
+            char ch = (char) (buf.byteAt(i) & 0xff);
             if (escaped) {
-                if (b != '\"' && b != '\\') {
-                    copy.append('\\');
+                if (ch != '\"' && ch != '\\') {
+                    strbuf.append('\\');
                 }
-                copy.append(b);
+                strbuf.append(ch);
                 escaped = false;
             } else {
-                if (b == '\\') {
+                if (ch == '\\') {
                     escaped = true;
                 } else {
-                    copy.append(b);
+                    strbuf.append(ch);
                 }
             }
         }
-        return ContentUtil.decode(copy, 0, copy.length());
+        return strbuf.toString();
     }
 
 }

Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java?rev=1089427&r1=1089426&r2=1089427&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java (original)
+++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java Wed
Apr  6 12:38:15 2011
@@ -40,7 +40,14 @@ public class ContentUtil {
      * @return encoded string as an immutable sequence of bytes.
      */
     public static ByteSequence encode(String string) {
-        return encode(CharsetUtil.US_ASCII, string);
+        if (string == null) {
+            return null;
+        }
+        ByteArrayBuffer buf = new ByteArrayBuffer(string.length());
+        for (int i = 0; i < string.length(); i++) {
+            buf.append((byte) string.charAt(i));
+        }
+        return buf;
     }
 
     /**
@@ -54,10 +61,16 @@ public class ContentUtil {
      * @return encoded string as an immutable sequence of bytes.
      */
     public static ByteSequence encode(Charset charset, String string) {
+        if (string == null) {
+            return null;
+        }
+        if (charset == null) {
+            charset = Charset.defaultCharset();
+        }
         ByteBuffer encoded = charset.encode(CharBuffer.wrap(string));
-        ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining());
-        bab.append(encoded.array(), encoded.position(), encoded.remaining());
-        return bab;
+        ByteArrayBuffer buf = new ByteArrayBuffer(encoded.remaining());
+        buf.append(encoded.array(), encoded.position(), encoded.remaining());
+        return buf;
     }
 
     /**
@@ -69,8 +82,10 @@ public class ContentUtil {
      * @return decoded string.
      */
     public static String decode(ByteSequence byteSequence) {
-        return decode(CharsetUtil.US_ASCII, byteSequence, 0, byteSequence
-                .length());
+        if (byteSequence == null) {
+            return null;
+        }
+        return decode(byteSequence, 0, byteSequence.length());
     }
 
     /**
@@ -99,9 +114,15 @@ public class ContentUtil {
      *            number of bytes.
      * @return decoded string.
      */
-    public static String decode(ByteSequence byteSequence, int offset,
-            int length) {
-        return decode(CharsetUtil.US_ASCII, byteSequence, offset, length);
+    public static String decode(ByteSequence byteSequence, int offset, int length) {
+        if (byteSequence == null) {
+            return null;
+        }
+        StringBuilder buf = new StringBuilder(length);
+        for (int i = offset; i < offset + length; i++) {
+            buf.append((char) (byteSequence.byteAt(i) & 0xff));
+        }
+        return buf.toString();
     }
 
     /**
@@ -120,6 +141,12 @@ public class ContentUtil {
      */
     public static String decode(Charset charset, ByteSequence byteSequence,
             int offset, int length) {
+        if (byteSequence == null) {
+            return null;
+        }
+        if (charset == null) {
+            charset = Charset.defaultCharset();
+        }
         if (byteSequence instanceof ByteArrayBuffer) {
             ByteArrayBuffer bab = (ByteArrayBuffer) byteSequence;
             return decode(charset, bab.buffer(), offset, length);

Modified: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/HeaderTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/HeaderTest.java?rev=1089427&r1=1089426&r2=1089427&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/HeaderTest.java (original)
+++ james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/HeaderTest.java Wed Apr
 6 12:38:15 2011
@@ -27,7 +27,8 @@ import org.apache.james.mime4j.dom.field
 import org.apache.james.mime4j.field.DefaultFieldParser;
 import org.apache.james.mime4j.message.HeaderImpl;
 import org.apache.james.mime4j.message.MimeWriter;
-import org.apache.james.mime4j.util.CharsetUtil;
+import org.apache.james.mime4j.util.ByteArrayBuffer;
+import org.apache.james.mime4j.util.ContentUtil;
 
 public class HeaderTest extends TestCase {
 
@@ -58,14 +59,17 @@ public class HeaderTest extends TestCase
         assertNotNull(field);
         // field.getBody is already a 7 bit ASCII string, after MIME4J-151
         // assertEquals(hello, field.getBody());
-        assertEquals("Gr?ezi_z?m?", field.getBody());
+        assertEquals(SWISS_GERMAN_HELLO, field.getBody());
         
-        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
         
-        MimeWriter.DEFAULT.writeHeader(header, buffer);
-        String s = buffer.toString(CharsetUtil.US_ASCII.name());
+        MimeWriter.DEFAULT.writeHeader(header, outstream);
+        byte[] b = outstream.toByteArray();
+        ByteArrayBuffer buf = new ByteArrayBuffer(b.length);
+        buf.append(b, 0, b.length);
+        String s = ContentUtil.decode(buf);
         
-        assertEquals("Hello: Gr?ezi_z?m?\r\n\r\n", s);
+        assertEquals("Hello: " + SWISS_GERMAN_HELLO + "\r\n\r\n", s);
     }
 
     public void testRemoveFields() throws Exception {



Mime
View raw message