Java InetAddress Create getBytes(InetAddress addr, int port)

Here you can find the source of getBytes(InetAddress addr, int port)

Description

Returns the IP:Port as byte array.

License

Open Source License

Declaration

public static byte[] getBytes(InetAddress addr, int port) 

Method Source Code


//package com.java2s;
/*// www  . ja  va  2  s  .  c om
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.net.*;

public class Main {
    /**
     * Returns the IP:Port as byte array.
     * 
     * This method is IPv6 compliant
     */
    public static byte[] getBytes(SocketAddress addr) throws UnknownHostException {
        InetSocketAddress iaddr = (InetSocketAddress) addr;
        if (iaddr.isUnresolved()) {
            throw new UnknownHostException(iaddr.toString());
        }

        return getBytes(iaddr.getAddress(), iaddr.getPort());
    }

    /**
     * Returns the IP:Port as byte array.
     * 
     * This method is IPv6 compliant
     */
    public static byte[] getBytes(InetAddress addr, int port) {
        if (!isValidPort(port)) {
            throw new IllegalArgumentException("Port out of range: " + port);
        }

        byte[] address = addr.getAddress();

        byte[] dst = new byte[address.length + 2];
        System.arraycopy(address, 0, dst, 0, address.length);
        dst[dst.length - 2] = (byte) ((port >> 8) & 0xFF);
        dst[dst.length - 1] = (byte) ((port) & 0xFF);
        return dst;
    }

    /**
     * Returns whether or not the specified port is within the valid range of
     * ports.
     * 
     * @param port
     *            the port number to check
     */
    public static boolean isValidPort(int port) {
        return (port > 0 && port <= 0xFFFF);
    }
}

Related

  1. getAddrInfo(InetAddress pAddr)
  2. getAllInetAddress(final String interfaceName)
  3. getAllIPv4InetAddresses()
  4. getAllLocalIPv4InetAddresses()
  5. getBroadcast(InetAddress address)
  6. getClassPart(InetAddress ip, int partNumber)
  7. getConnectionAddress(InetAddress addr)
  8. getDestinationInetAddress(URI uri)
  9. getExternalAddresses(InetAddress[] addresses)