è»è¼ªã®åçºæ - Base64ã®encoder/decoderãScalaã§æ¸ãã¦ã¿ãã
ãä½æ ãJDKã«ã¯Base64ã®encoder/decoderããªãã®ã ã?!ã
ã£ã¦ãã¨ã§ãæ¸ãã¾ããã
imutableãªLIstãreverseãããdropããããã¾ãã£ã¦ãå帰ã§Listãåã渡ãå½¢ãªã®ã§ããã©ã¼ãã³ã¹ãããªãã§ãã
ãã¨ãdecodeã®æã¯ãã©ã¼ãããã®ãã§ãã¯ãã¦ãªãé©å½ãwwwã
ã¾ãç·´ç¿ã§æ¸ãããã§
ãããããããããããã ï¼ï¼
ãããããããããããï¼ï¼ï¼ï¼
ããããããããã ï¼,.=ï¾''"ï¼ããã
ãããï¼ãããã i fã,.r='"-â'ã¤ï¼¿ï¼¿ï¼¿_ããããã¾ããããããããã ãï¼ï¼
ãã/ããããã /ããã_,.-â'~ï¼âããâï¼¼
ããããï¼ã ã,iããã,äºï¾âï¼ âï¼.ãï¼âï¼ï¼¼
ããã/ã ããï¾ããã ilï¾ã::::::âï¼__人__ï¼â::::: ï¼¼
ãããããã,イ「ï¾ï½¤ãã,!,!|ããããã|râ¬-|ããããã|
ããããã/ãiï¾ã¾ã½_/ィ"ï¼¼ ãã ã `ã¼'´ ã ã ï¼
object Base64 { val BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" def encode( s:String ) = encodeBuffer( s.getBytes ) def encode( s:String, charset:String ) = encodeBuffer( s.getBytes( charset ) ) private def encodeBuffer( buf:Array[Byte] ) = { val bits = fill( buf.map{ b => if( b < 0 ) b + 256 else b.toInt }.map{ i => ("00000000" + i.toBinaryString).reverse.take(8).reverse }.mkString, "0", 6 ).toList def encodeBits( xs:List[Char] ):String = xs match{ case Nil => "" case ss => val (h,t) = ss.splitAt(6) BASE64( (0 /: h.reverse.zipWithIndex){ case( n, (c, i)) => n + (Math.pow( 2, i) * c.asDigit ).toInt } ) + encodeBits( t ) } fill( encodeBits( bits), "=", 4) } private def fill( s:String, fill:String, n:Int ) = s + ( fill * ( n - ( s.length % n ))) def decode( s:String ) = new String( decodeBuffer( s )) def decode( s:String , charset:String ) = new String( decodeBuffer( s ), charset ) def decodeBuffer( s:String ) = { val bits = s.map{ BASE64.indexOf(_)}. filter( 0 < ). map{ b => ("000000" + b.toBinaryString).reverse.take(6).reverse }.mkString.toList def decodeBits( xs:List[Char] ):List[Byte]= xs match { case Nil => Nil case ss => val (h,t) = ss.splitAt(8) (( (0 /: h.reverse.zipWithIndex){ case( n, (c, i)) => n + (Math.pow( 2, i) * c.asDigit ).toInt } ) match{ case b if b > 256 => (b - 256).toByte case b => b.toByte }) :: decodeBits( t ) } decodeBits( bits ).toArray } }