Last active
          August 29, 2015 14:10 
        
      - 
      
- 
        Save silverprize/7100a1dd6090f3812b27 to your computer and use it in GitHub Desktop. 
    base64 encoding
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | intToBase64 = [ | |
| 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', | |
| 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', | |
| 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', | |
| 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', | |
| '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' | |
| ] | |
| def encode(InputStream is) { | |
| int countByte = 2 | |
| int twentyFourBit = 0 | |
| is.each { | |
| twentyFourBit |= it << (countByte * 8) | |
| countByte-- | |
| if (countByte == -1) { | |
| for (int shift = 18; shift >= 0; shift -= 6) { | |
| print intToBase64[(twentyFourBit >> shift) & 63] | |
| } | |
| countByte = 2 | |
| twentyFourBit = 0 | |
| } | |
| } | |
| if (countByte != 2) { | |
| int shift = 18 | |
| for (int i = 0; i < 3 - countByte; i++) { | |
| print intToBase64[(twentyFourBit >> shift) & 63] | |
| shift -= 6 | |
| } | |
| for (int i = countByte; i >= 0; i--) { | |
| print '=' | |
| } | |
| } | |
| } | |
| def sc = new Scanner(System.in) | |
| while (true) { | |
| encode(new ByteArrayInputStream(sc.nextLine().bytes)) | |
| println() | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
3바이트씩 읽어주는 InputStream을 만들면 좋을 것 같다는 피드백을 받음