Changeset 662 in josm for trunk


Ignore:
Timestamp:
2008-06-26T19:50:30+02:00 (16 years ago)
Author:
framm
Message:
  • patch for usernames with international characters. please revert this immediately if it causes any trouble. submitted by danilo@….
Location:
trunk/src/org/openstreetmap/josm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/OsmConnection.java

    r627 r662  
    99import java.net.HttpURLConnection;
    1010import java.net.PasswordAuthentication;
     11import java.nio.ByteBuffer;
     12import java.nio.CharBuffer;
     13import java.nio.charset.Charset;
     14import java.nio.charset.CharsetEncoder;
     15import java.nio.charset.CharacterCodingException;
    1116
    1217import javax.swing.JCheckBox;
     
    129134        }
    130135
    131         protected void addAuth(HttpURLConnection con) {
    132         con.addRequestProperty("Authorization", "Basic "+Base64.encode(Main.pref.get("osm-server.username")+":"+Main.pref.get("osm-server.password")));
     136        protected void addAuth(HttpURLConnection con) throws CharacterCodingException {
     137            CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
     138            String auth = Main.pref.get("osm-server.username") + ":" + Main.pref.get("osm-server.password");
     139            ByteBuffer bytes = encoder.encode(CharBuffer.wrap(auth));
     140            con.addRequestProperty("Authorization", "Basic "+Base64.encode(bytes));
    133141    }
    134142}
  • trunk/src/org/openstreetmap/josm/tools/Base64.java

    r627 r662  
    22package org.openstreetmap.josm.tools;
    33
     4import java.nio.ByteBuffer;
     5
    46public class Base64 {
    57
    6         private static String enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
     8    private static String enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    79
    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);
     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);
    1315            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)));
     16            out.append(enc.charAt(
     17                                  (buf.charAt(0) & 0x03) << 4 |
     18                                  (l==1?
     19                                   0:
     20                                   (buf.charAt(1) & 0xf0) >> 4)));
    1521            out.append(l>1?enc.charAt((buf.charAt(1) & 0x0f) << 2 | (l==2?0:(buf.charAt(2) & 0xc0) >> 6)):'=');
    1622            out.append(l>2?enc.charAt(buf.charAt(2) & 0x3f):'=');
    17                 }
    18                 return out.toString();
    19         }
     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    }
    2047}
Note: See TracChangeset for help on using the changeset viewer.