Java Socket Address Get toByte(InetSocketAddress socketAddress)

Here you can find the source of toByte(InetSocketAddress socketAddress)

Description

to Byte

License

Apache License

Declaration

public static byte[] toByte(InetSocketAddress socketAddress) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.net.*;

public class Main {

    public static byte[] toByte(InetSocketAddress socketAddress) {
        if (socketAddress == null) {
            throw new IllegalArgumentException("socketAddress is null");
        }//w  ww. j  av  a  2  s . c  o m
        InetAddress inetAddress = socketAddress.getAddress();
        if (inetAddress == null) {
            throw new IllegalArgumentException("socketAddress is invalid");
        }
        byte[] address = inetAddress.getAddress();
        byte[] result = new byte[address.length + 2];
        System.arraycopy(address, 0, result, 2, address.length);
        int port = socketAddress.getPort();
        result[1] = (byte) (port >> 8 & 0xFF);
        result[0] = (byte) (port & 0xFF);
        return result;
    }

    public static String getAddress(SocketAddress address) {
        if (address == null) {
            return null;
        }
        if (address instanceof InetSocketAddress) {
            InetSocketAddress isa = (InetSocketAddress) address;
            return isa.getAddress().getHostAddress() + ":" + isa.getPort();
        } else {
            return address.toString();
        }
    }
}

Related

  1. string2SocketAddress(final String addr)
  2. stringAddressToSocketAddress(String address)
  3. stringifyInetSocketAddressList(InetSocketAddress[] addresses)
  4. stringToInetSocketAddress( String address, int defaultPort)
  5. toAddressString(InetSocketAddress address)
  6. toBytes(SocketAddress address)
  7. toFileName(InetSocketAddress address)
  8. toHttpUrl(InetSocketAddress addr)
  9. toInetSocketAddress(String address)