- Timestamp:
- 2008-06-26T19:50:30+02:00 (16 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/OsmConnection.java
r627 r662 9 9 import java.net.HttpURLConnection; 10 10 import java.net.PasswordAuthentication; 11 import java.nio.ByteBuffer; 12 import java.nio.CharBuffer; 13 import java.nio.charset.Charset; 14 import java.nio.charset.CharsetEncoder; 15 import java.nio.charset.CharacterCodingException; 11 16 12 17 import javax.swing.JCheckBox; … … 129 134 } 130 135 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)); 133 141 } 134 142 } -
trunk/src/org/openstreetmap/josm/tools/Base64.java
r627 r662 2 2 package org.openstreetmap.josm.tools; 3 3 4 import java.nio.ByteBuffer; 5 4 6 public class Base64 { 5 7 6 8 private static String enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 7 9 8 9 10 11 12 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); 13 15 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))); 15 21 out.append(l>1?enc.charAt((buf.charAt(1) & 0x0f) << 2 | (l==2?0:(buf.charAt(2) & 0xc0) >> 6)):'='); 16 22 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 } 20 47 }
Note:
See TracChangeset
for help on using the changeset viewer.