commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Julius Davies (JIRA)" <j...@apache.org>
Subject [jira] Issue Comment Edited: (CODEC-59) Add methods to Base64 which work with String instead of byte[]
Date Fri, 17 Jul 2009 23:40:15 GMT

    [ https://issues.apache.org/jira/browse/CODEC-59?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12732788#action_12732788
] 

Julius Davies edited comment on CODEC-59 at 7/17/09 4:38 PM:
-------------------------------------------------------------

codec59.patch attached.  It introduces two new methods:

public String Base64.encodeToString( byte[] b )

public byte[] Base64.decodeFromString( String s )


The String the first method produces chunks the output into 76-character lines.  If consumers
want very-long single lines instead they can run .replaceAll("\r\n", "") against the output.
 (I chose chunking as default behaviour because the breaking a very-long-line into chunks
is quite a bit trickier).

Even if this patch is not accepted, I strongly recommend taking in these 5 lines, since they
fix a very subtle and rare bug (newly introduced in CODEC-69 I suspect):

{code}
@@ -698,7 +715,11 @@
             len += 4 - mod;
         }
         if (isChunked) {
-            len += (1 + (len / CHUNK_SIZE)) * CHUNK_SEPARATOR.length;
+            boolean lenChunksPerfectly = len % CHUNK_SIZE == 0;
+            len += (len / CHUNK_SIZE) * CHUNK_SEPARATOR.length;
+            if (!lenChunksPerfectly) {
+                len += CHUNK_SEPARATOR.length;
+            }
         }
{code}


      was (Author: juliusdavies):
    codec59.patch attached.  It introduces two new methods:

public String Base64.encodeToString( byte[] b )

public byte[] Base64.decodeFromString( String s )


The String the first method produces chunks the output into 76-character lines.  If consumers
want very-long single lines instead they can run .replaceAll("\r\n", "") against the output.
 (I chose chunking as default behaviour because the breaking a very-long-line into chunks
is quite a bit trickier).

Even if this patch is not accepted, I strongly recommend taking in these 5 lines, since they
fix a very subtle and rare bug (newly introduced in CODEC-69 I suspect):

@@ -698,7 +715,11 @@
             len += 4 - mod;
         }
         if (isChunked) {
-            len += (1 + (len / CHUNK_SIZE)) * CHUNK_SEPARATOR.length;
+            boolean lenChunksPerfectly = len % CHUNK_SIZE == 0;
+            len += (len / CHUNK_SIZE) * CHUNK_SEPARATOR.length;
+            if (!lenChunksPerfectly) {
+                len += CHUNK_SEPARATOR.length;
+            }
         }


  
> Add methods to Base64 which work with String instead of byte[]
> --------------------------------------------------------------
>
>                 Key: CODEC-59
>                 URL: https://issues.apache.org/jira/browse/CODEC-59
>             Project: Commons Codec
>          Issue Type: Improvement
>            Reporter: Pepijn Schmitz
>            Priority: Minor
>             Fix For: 1.x
>
>         Attachments: codec59.patch
>
>
> It would be great if the Base64 class had String encode(byte[]) and byte[] decode(String)
methods. The RFC is stated in terms of "characters" for the encoding, so there should be no
problem with unwarranted assumptions with this. Currently everyone is having to convert to
and from Strings themselves.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message