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

Last change on this file since 729 was 662, checked in by framm, 16 years ago
  • patch for usernames with international characters. please revert this immediately if it causes any trouble. submitted by danilo@….
  • Property svn:eol-style set to native
File size: 1.8 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.tools;
3
4import java.nio.ByteBuffer;
5
6public class Base64 {
7
8 private static String enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
9
10 public static String encode(String s) {
11 StringBuilder out = new StringBuilder();
12 for (int i = 0; i < (s.length()+2)/3; ++i) {
13 int l = Math.min(3, s.length()-i*3);
14 String buf = s.substring(i*3, i*3+l);
15 out.append(enc.charAt(buf.charAt(0)>>2));
16 out.append(enc.charAt(
17 (buf.charAt(0) & 0x03) << 4 |
18 (l==1?
19 0:
20 (buf.charAt(1) & 0xf0) >> 4)));
21 out.append(l>1?enc.charAt((buf.charAt(1) & 0x0f) << 2 | (l==2?0:(buf.charAt(2) & 0xc0) >> 6)):'=');
22 out.append(l>2?enc.charAt(buf.charAt(2) & 0x3f):'=');
23 }
24 return out.toString();
25 }
26
27 public static String encode(ByteBuffer s) {
28 StringBuilder out = new StringBuilder();
29 // Read 3 bytes at a time.
30 for (int i = 0; i < (s.limit()+2)/3; ++i) {
31 int l = Math.min(3, s.limit()-i*3);
32 int byte0 = s.get() & 0xff;
33 int byte1 = l>1? s.get() & 0xff : 0;
34 int byte2 = l>2? s.get() & 0xff : 0;
35
36 out.append(enc.charAt(byte0>>2));
37 out.append(enc.charAt(
38 (byte0 & 0x03) << 4 |
39 (l==1?
40 0:
41 (byte1 & 0xf0) >> 4)));
42 out.append(l>1?enc.charAt((byte1 & 0x0f) << 2 | (l==2?0:(byte2 & 0xc0) >> 6)):'=');
43 out.append(l>2?enc.charAt(byte2 & 0x3f):'=');
44 }
45 return out.toString();
46 }
47}
Note: See TracBrowser for help on using the repository browser.