added org.ibex.net.IP
[org.ibex.net.git] / src / org / ibex / net / IP.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.net;
6
7 import java.net.*;
8 import java.io.*;
9 import java.util.*;
10 import org.ibex.util.*;
11
12 public class IP {
13
14     /** construct an IP address from four bytes; doesn't throw an exception */
15     public static InetAddress getIP(int a, int b, int c, int d) {
16         try {
17             return InetAddress.getByAddress(new byte[] { (byte)a, (byte)b, (byte)c, (byte)d });
18         } catch (Exception e) {
19             Log.error(IP.class, e);
20             return null;
21         }
22     }
23
24     public static String toString(InetAddress pi) {
25         return
26             (pi.getAddress()[0]&0xff) + "." +
27             (pi.getAddress()[1]&0xff) + "." +
28             (pi.getAddress()[2]&0xff) + "." +
29             (pi.getAddress()[3]&0xff);
30     }
31
32 }