eclipse-archived / ceylon-sdk

Standard platform modules belonging to the Ceylon SDK
http://www.ceylon-lang.org
Apache License 2.0
72 stars 60 forks source link

Add Base64 Charset #71

Closed FroMage closed 7 years ago

FroMage commented 11 years ago

That will be very useful :)

quintesse commented 11 years ago

Wait, how should I interpret this? A charset that does encoding to/from Base64? Wow, never thought of it that way :)

FroMage commented 11 years ago

One of Java 8's big new features ;)

quintesse commented 11 years ago

Oh I thought lambda's would be its big feature ;)

FroMage commented 11 years ago

http://openjdk.java.net/jeps/135 and http://download.java.net/jdk8/docs/api/java/util/Base64.html for what Java does.

I think we rather need a Base64 encoder/decoder of ByteArray<->String like they have. It's not really a Charset, especially since converting text to Base64 in binary means text -> charset convertion (UTF or else) to binary -> Base64 conversion to String -> charset conversion (probably ASCII) to binary.

We just need utility methods that do this chaining for us.

luolong commented 11 years ago

There is another implementation that is public domain and as far as I know has been copied countless times due to the sun.util.Base64 being in the "proprietary" namespace: http://iharder.sourceforge.net/current/java/base64/

Bhathiya90 commented 10 years ago

@FroMage - Can you elaborate what need to be done? And from where should I start if I am to work on this issue ? As I understand(by the comment),what is needed is implementation similar to http://download.java.net/jdk8/docs/api/java/util/Base64.html.

DiegoCoronel commented 10 years ago

Hi @Bhathiya90 this is partially implemented: https://github.com/ceylon/ceylon-sdk/blob/master/source/ceylon/io/base64.ceylon , i think maybe it would be good to complete with same RFC implementations as your link.

Bhathiya90 commented 10 years ago

@FroMage @DiegoCoronel

Started with some helper methods without changing the implemeted base64 encode/decode.
/*Encode a givenString to Base64.*/
shared String encodeStringToString(String string ,String enc = "ASCII"){
            //In the java implemetation i am reffering   string.getBytes() is used  
            value cs = getCharset(enc);
            if(exists cs){
                return  encodeByteToString(cs.encode(string), enc);
            }           
            throw Exception("Charset not supported ");          
    }

shared String encodeByteToString(ByteBuffer inputBuffer,String enc ="ASCII"){
        // in java  new String(byte[] b ) is used 
ByteBuffer encodedBuffer = base64.getEncoder().encode(inputBuffer);//uses already implemeted base64 encoding
        value cs = getCharset(enc);
        if(exists cs){
              return cs.decode(encodedBuffer);               
        }       
        throw Exception("Charset not supported");   
    }

is this what you are looking for?

FroMage commented 10 years ago

I think we should add the following methods on the base64 object:

"Encodes the given string in Base64 using the given character encoding"
shared String encode(String input, Charset charset);

"Decodes the given string from Base64 using the given character encoding"
shared String decode(String input, Charset charset);
Bhathiya90 commented 10 years ago

Here , https://github.com/Bhathiya90/ceylon-sdk/blob/master/source/ceylon/io/base64.ceylon implemented

shared String encode(String input, String charset);
shared String decode(String input, String charset); methods with a class Base64Helper().

In most of the java implementations Charset is passed as a String (with platform's default character encoding as default) ? What should we choose? Charset charset or String charset?

FroMage commented 10 years ago

Definitly keep it type-safe with Charset.

Bhathiya90 commented 10 years ago

@FroMage Changed the methods to take a Charset.What should I do next? as for me if I 1- Change/ Reimplement shared actual ByteBuffer decode(ByteBuffer encoded) to discard padding 2 -Update testing classes to test new methods added this issue can be closed.Is it?

Bhathiya90 commented 10 years ago

Changed the base64 test classas well. https://github.com/Bhathiya90/ceylon-sdk/blob/master/test-source/test/ceylon/io/testBase64.ceylon

FroMage commented 10 years ago

Sounds great. Can you create a pull-request?

Bhathiya90 commented 10 years ago

Created https://github.com/ceylon/ceylon-sdk/pull/186

ASzc commented 8 years ago

457 removes the distinction between Charset and Base64, as part of the unified Codec types.

CPColin commented 7 years ago

I think this is done and in ceylon.buffer now.

luolong commented 7 years ago

Yeah. the implementation is in base64.ceylon