Last change
on this file since 298 was 298, checked in by imi, 17 years ago |
- added license description to head of each source file
|
File size:
823 bytes
|
Rev | Line | |
---|
[298] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[195] | 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
| 4 | public class Base64 {
|
---|
| 5 |
|
---|
| 6 | private static String enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
---|
| 7 |
|
---|
| 8 | public static String encode(String s) {
|
---|
| 9 | StringBuilder out = new StringBuilder();
|
---|
| 10 | for (int i = 0; i < (s.length()+2)/3; ++i) {
|
---|
| 11 | int l = Math.min(3, s.length()-i*3);
|
---|
| 12 | String buf = s.substring(i*3, i*3+l);
|
---|
| 13 | out.append(enc.charAt(buf.charAt(0)>>2));
|
---|
| 14 | out.append(enc.charAt((buf.charAt(0) & 0x03) << 4 | (l==1?0:(buf.charAt(1) & 0xf0) >> 4)));
|
---|
[217] | 15 | out.append(l>1?enc.charAt((buf.charAt(1) & 0x0f) << 2 | (l==2?0:(buf.charAt(2) & 0xc0) >> 6)):'=');
|
---|
[195] | 16 | out.append(l>2?enc.charAt(buf.charAt(2) & 0x3f):'=');
|
---|
| 17 | }
|
---|
| 18 | return out.toString();
|
---|
| 19 | }
|
---|
| 20 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.