source: josm/src/org/openstreetmap/josm/tools/Base64.java@ 217

Last change on this file since 217 was 217, checked in by framm, 17 years ago

Fixed BASE64 bug that would lead to exceptions on clear text lengths of 3n+2.
Patch provided by Sander Hoentjen.
Fixes http://lists.openstreetmap.org/pipermail/dev/2007-April/004090.html

File size: 761 bytes
Line 
1package org.openstreetmap.josm.tools;
2
3public class Base64 {
4
5 private static String enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
6
7 public static String encode(String s) {
8 StringBuilder out = new StringBuilder();
9 for (int i = 0; i < (s.length()+2)/3; ++i) {
10 int l = Math.min(3, s.length()-i*3);
11 String buf = s.substring(i*3, i*3+l);
12 out.append(enc.charAt(buf.charAt(0)>>2));
13 out.append(enc.charAt((buf.charAt(0) & 0x03) << 4 | (l==1?0:(buf.charAt(1) & 0xf0) >> 4)));
14 out.append(l>1?enc.charAt((buf.charAt(1) & 0x0f) << 2 | (l==2?0:(buf.charAt(2) & 0xc0) >> 6)):'=');
15 out.append(l>2?enc.charAt(buf.charAt(2) & 0x3f):'=');
16 }
17 return out.toString();
18 }
19}
Note: See TracBrowser for help on using the repository browser.