InetAddress toString - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for InetAddress toString method.

This code example shows how to use the following methods:isLoopbackAddress, toString
	ia = InetAddress.getByName("");
	System.out.println("\"\" = " + check(ia));
 
	ia = InetAddress.getByName("localhost");
	System.out.println("localhost = " + check(ia));
	if( !ia.isLoopbackAddress() )
	{ 
	    System.out.println("Not a loopback?");
	} 
    } 
 
    private static String check(InetAddress ia)
         throws Throwable 
    { 
	if (ia instanceof Inet6Address) {
	    if (ia.toString().equals("localhost/::::::::1") ||
		ia.toString().equals("localhost/0:0:0:0:0:0:0:1")) {
		return "localhost/127.0.0.1"; 
	    } 
	} 

6
CodeRank
This code example shows how to use the following methods:toString
		return publicAddress.hashCode() ^ natType;
	} 
	 
	@Override 
	public String toString() {
		return publicAddress.toString()+":"+natType+":"+mtu;
	} 
} 
6
CodeRank
This code example shows how to use the following methods:toString
 
    @Override 
    public void serialize(InetAddress value, JsonGenerator jgen, SerializerProvider provider) throws IOException
    { 
        // Ok: get textual description; choose "more specific" part 
        String str = value.toString().trim();
        int ix = str.indexOf('/');
        if (ix >= 0) {
            if (ix == 0) { // missing host name; use address
                str = str.substring(1);
            } else { // otherwise use name 
                str = str.substring(0, ix);
            } 
        } 
        jgen.writeString(str);
    } 
 
    @Override 
    public void serializeWithType(InetAddress value, JsonGenerator jgen, SerializerProvider provider, TypeSerializer typeSer) throws IOException, JsonGenerationException
    { 

5
CodeRank
This code example shows how to use the following methods:getHostAddress, toString
     * TODO: We may introduce a System property or some other method of configuration 
     * TODO: which will specify whether to allow reverse DNS lookup or not 
     */ 
    public static String getHostName(InetAddress address) {
        String result;
        String hostAddress = address.getHostAddress();
        String inetAddr = address.toString();
        int index1 = inetAddr.lastIndexOf('/');
        int index2 = inetAddr.indexOf(hostAddress);
        if (index2 == index1 + 1) {
            if (index1 == 0) {
                result = hostAddress;
            } else { 
                result = inetAddr.substring(0, index1);
            } 
        } else { 
            result = hostAddress;
        } 
        return result;
    } 
5
CodeRank
This code example shows how to use the following methods:getHostAddress, toString
     * @param address The InetAddress whose hostname has to be determined 
     * @return hostsname, if it can be determined. hostaddress, if not.           
     */ 
    public static String getHostName(InetAddress address) {
        String result;
        String hostAddress = address.getHostAddress();
        String inetAddr = address.toString();
        int index1 = inetAddr.lastIndexOf('/');
        int index2 = inetAddr.indexOf(hostAddress);
        if (index2 == index1 + 1) {
            if (index1 == 0) {
                result = hostAddress;
            } else { 
                result = inetAddr.substring(0, index1);
            } 
        } else { 
            result = hostAddress;
        } 
        return result;
    } 
5
CodeRank
This code example shows how to use the following methods:getHostAddress, toString
     * @param address The InetAddress whose hostname has to be determined 
     * @return hostsname, if it can be determined. hostaddress, if not.           
     */ 
    public static String getHostName(InetAddress address) {
        String result;
        String hostAddress = address.getHostAddress();
        String inetAddr = address.toString();
        int index1 = inetAddr.lastIndexOf('/');
        int index2 = inetAddr.indexOf(hostAddress);
        if (index2 == index1 + 1) {
            if (index1 == 0) {
                result = hostAddress;
            } else { 
                result = inetAddr.substring(0, index1);
            } 
        } else { 
            result = hostAddress;
        } 
        return result;
    } 
5
CodeRank
This code example shows how to use the following methods:equals, toString
	@Override 
	public String toString() {
		StringBuilder b = new StringBuilder();
		String t = start.toString();
		b.append(t.substring(t.indexOf('/') + 1));
		if (!end.equals(start)) {
			b.append('-');
			t = end.toString();
			b.append(t.substring(t.indexOf('/') + 1));
		} 
		return b.toString();
	} 
} 
5
CodeRank
This code example shows how to use the following methods:toString
 
    @Override 
    public void serialize(InetSocketAddress value, JsonGenerator jgen, SerializerProvider provider) throws IOExceptionInetAddress addr = value.getAddress();
        String str = addr == null ? value.getHostName() : addr.toString().trim();
        int ix = str.indexOf('/');
        if (ix >= 0) {
            if (ix == 0) { // missing host name; use address
                str = addr instanceof Inet6Address
                        ? "[" + str.substring(1) + "]" // bracket IPv6 addresses with
                        : str.substring(1);
 
            } else { // otherwise use name 
                str = str.substring(0, ix);
            } 
        } 
 
        jgen.writeString(str + ":" + value.getPort());
    } 
5
CodeRank
This code example shows how to use the following methods:toString
    @Override 
    public void serialize(InetAddress value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonGenerationException 
    { 
        // Ok: get textual description; choose "more specific" part 
        String str = value.toString().trim();
        int ix = str.indexOf('/');
        if (ix >= 0) {
            if (ix == 0) { // missing host name; use address
                str = str.substring(1);
            } else { // otherwise use name 
                str = str.substring(0, ix);
            } 
        } 
        jgen.writeString(str);
    } 
 
    @Override 
    public void serializeWithType(InetAddress value, JsonGenerator jgen, SerializerProvider provider,
            TypeSerializer typeSer)
4
CodeRank
This code example shows how to use the following methods:toString
 
	public String getBindAddress()
	{ 
			if (bindAddr == null)
				return ""; 
			return bindAddr.toString();
	} 
 
	public int getBindPort() 
	{ 
		return bindPort;
	} 
	 
	 
	 
	//////////////////////////////////////////////// 
	//	open/close 
	//////////////////////////////////////////////// 
	 
	/** 
4
CodeRank

Related InetAddress toString Questions & Answers:

toString Method Overview
Returns a string containing the host name (if available) and host address.
See Code Examples for other Java 8 InetAddress Methods: