Java InetAddress readInetAddress(final DataInput in, final boolean fixedLength)

Here you can find the source of readInetAddress(final DataInput in, final boolean fixedLength)

Description

read Inet Address

License

LGPL

Declaration

public static InetAddress readInetAddress(final DataInput in, final boolean fixedLength) throws IOException 

Method Source Code


//package com.java2s;
/*/*from  www. j  a v a 2s. co  m*/
 * Cacheonix Systems licenses this file to You under the LGPL 2.1
 * (the "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *      http://www.cacheonix.org/products/cacheonix/license-lgpl-2.1.htm
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.DataInput;
import java.io.DataInputStream;

import java.io.IOException;

import java.net.InetAddress;

public class Main {
    public static InetAddress readInetAddress(final DataInput in, final boolean fixedLength) throws IOException {

        // Read boolean null value marker
        if (in.readBoolean()) {

            // The address is null
            if (fixedLength) {

                // Read padded bytes
                in.readByte();
                in.readLong();
                in.readLong();
            }

            return null;
        }

        // Read the address
        final byte addrLength = in.readByte();
        switch (addrLength) {
        case 4:

            final int address = in.readInt();
            if (fixedLength) {

                // Pad 12 bytes
                in.readInt();
                in.readLong();
            }
            final byte[] addr = new byte[4];
            addr[0] = (byte) (address >>> 24 & 0xFF);
            addr[1] = (byte) (address >>> 16 & 0xFF);
            addr[2] = (byte) (address >>> 8 & 0xFF);
            addr[3] = (byte) (address & 0xFF);
            return InetAddress.getByAddress(addr);
        case 16:

            final byte[] result = new byte[16];
            in.readFully(result);
            return InetAddress.getByAddress(result);
        default:

            throw new IOException("Unknown address format, length: " + addrLength);
        }
    }

    public static Long readLong(final DataInputStream in) throws IOException {

        if (in.readBoolean()) {
            return null;
        }
        return in.readLong();
    }
}

Related

  1. inetAddressGt(InetAddress a, InetAddress b)
  2. inetAddrToByteArray(InetAddress a)
  3. newInterfaceAddress(InetAddress inetAddress, short maskLength)
  4. normalizeHostAddress(final InetAddress localHost)
  5. parseInetAddressFromString(String input)
  6. readInetAddress(final String stringInetAddress)
  7. remove(InetAddress[] a_, InetAddress b)
  8. removeNull(InetAddress addrs[])
  9. serialize(InetAddress endpoint, DataOutput out)