1 11 package org.eclipse.ui.internal.ide.dialogs; 12 13 14 18 class InternalBase64Encoder { 19 private static final String mappings = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; private static final String filler = "="; 28 public static String encode(byte[] contents) { 29 StringBuffer result = new StringBuffer (); 30 31 for (int i = 0; i < contents.length; i = i + 3) { 32 if (result.length() == 76) 33 result.append("\n\r"); 35 result.append(mappings.charAt((contents[i] & 0xFC) >> 2)); 37 38 int c2 = (contents[i] & 0x03) << 4; 40 if (i + 1 >= contents.length) { 41 result.append(mappings.charAt(c2)); 42 result.append(filler); 43 result.append(filler); 44 return result.toString(); 45 } 46 47 c2 |= ((contents[i + 1] & 0xF0) >> 4); 48 result.append(mappings.charAt(c2)); 49 50 int c3 = (contents[i + 1] & 0x0F) << 2; 52 if (i + 2 >= contents.length) { 53 result.append(mappings.charAt(c3)); 54 result.append(filler); 55 return result.toString(); 56 } 57 58 c3 |= ((contents[i + 2] & 0xC0) >> 6); 59 result.append(mappings.charAt(c3)); 60 61 result.append(mappings.charAt(contents[i + 2] & 0x3F)); 63 } 64 65 return result.toString(); 66 } 67 } 68 | Popular Tags |