~maria-captains/mariadb-java-client/trunk

« back to all changes in this revision

Viewing changes to src/main/java/org/mariadb/jdbc/HostAddress.java

  • Committer: Vladislav Vaintroub
  • Date: 2013-01-06 00:22:56 UTC
  • Revision ID: wlad@montyprogram.com-20130106002256-nv0633mgkfl94i7e
CONJ-7 : support IPv6 addresses in JDBC URL (square-bracket syntax)

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
        HostAddress[] arr = new HostAddress[tokens.length];
17
17
 
18
18
        for (int i=0; i < tokens.length; i++) {
19
 
            arr[i] = new HostAddress();
20
 
            String t = tokens[i];
21
 
            if (t.contains(":")) {
22
 
                String[] hostPort = t.split(":");
23
 
                arr[i].host = hostPort[0];
24
 
                arr[i].port = Integer.parseInt(hostPort[1]);
25
 
            } else {
26
 
                arr[i].host = t;
27
 
            }
 
19
          arr[i] = parseSingleHostAddress(tokens[i]);
28
20
        }
29
21
 
30
22
        int  defaultPort = arr[arr.length-1].port;
40
32
        return arr;
41
33
    }
42
34
 
 
35
    static HostAddress parseSingleHostAddress(String s) {
 
36
         HostAddress result = new HostAddress(); 
 
37
         if (s.startsWith("[")) {
 
38
                /* IPv6 addresses in URLs are enclosed in square brackets */
 
39
                int ind = s.indexOf(']');
 
40
                result.host = s.substring(1,ind);
 
41
                if (ind != (s.length() -1) && s.charAt(ind + 1) == ':') { 
 
42
                        result.port = Integer.parseInt(s.substring(ind+2));
 
43
                }
 
44
          }
 
45
          else if (s.contains(":")) {
 
46
                  /* Parse host:port */
 
47
              String[] hostPort = s.split(":");
 
48
              result.host = hostPort[0];
 
49
              result.port = Integer.parseInt(hostPort[1]);
 
50
          } else {
 
51
                  /* Just host name is given */
 
52
              result.host = s;
 
53
          }
 
54
          return result;
 
55
    }
43
56
    public static String toString(HostAddress[] addrs) {
44
57
        String s="";
45
58
        for(int i=0; i < addrs.length; i++) {
46
 
            s += addrs[i].host + ":" + addrs[i].port;
 
59
                boolean isIPv6 = addrs[i].host != null && addrs[i].host.contains(":");
 
60
                String host = (isIPv6)?("[" + addrs[i].host + "]"):addrs[i].host;
 
61
            s += host + ":" + addrs[i].port;
47
62
            if (i < addrs.length -1)
48
63
                s += ",";
49
64
        }