Java InetAddress writeInetAddress(final InetAddress inetAddress, final DataOutput out, final boolean fixedLength)

Here you can find the source of writeInetAddress(final InetAddress inetAddress, final DataOutput out, final boolean fixedLength)

Description

write Inet Address

License

LGPL

Declaration

public static void writeInetAddress(final InetAddress inetAddress, final DataOutput out,
            final boolean fixedLength) throws IOException 

Method Source Code

//package com.java2s;
/*/*from   www . ja v a 2  s .c  om*/
 * 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.DataOutput;
import java.io.DataOutputStream;

import java.io.IOException;

import java.net.InetAddress;

public class Main {
    public static void writeInetAddress(final InetAddress inetAddress, final DataOutput out,
            final boolean fixedLength) throws IOException {

        if (inetAddress == null) {

            // Write null 16 byte address
            out.writeBoolean(true);

            if (fixedLength) {

                out.writeByte(0);
                out.writeLong(0);
                out.writeLong(0);
            }
        } else {

            // Write not null marker
            out.writeBoolean(false);

            final byte[] addr = inetAddress.getAddress();
            out.writeByte(addr.length);
            if (addr.length == 4) {

                int address = addr[3] & 0xFF;
                address |= addr[2] << 8 & 0xFF00;
                address |= addr[1] << 16 & 0xFF0000;
                address |= addr[0] << 24 & 0xFF000000;

                out.writeInt(address);

                if (fixedLength) {
                    out.writeInt(0);
                    out.writeLong(0);
                }
            } else if (addr.length == 16) {

                out.write(addr);
            } else {

                throw new IOException("Unknown address format: " + inetAddress);
            }
        }
    }

    public static void writeBoolean(final DataOutputStream out, final Boolean aBoolean) throws IOException {

        if (aBoolean == null) {

            out.writeBoolean(true);
        } else {

            out.writeBoolean(false);
            out.writeBoolean(aBoolean.booleanValue());
        }
    }

    public static void writeLong(final DataOutputStream out, final Long aLong) throws IOException {

        if (aLong == null) {
            out.writeBoolean(true);
        } else {
            out.writeBoolean(false);
            out.writeLong(aLong.longValue());
        }
    }
}

Related

  1. remove(InetAddress[] a_, InetAddress b)
  2. removeNull(InetAddress addrs[])
  3. serialize(InetAddress endpoint, DataOutput out)
  4. sortKey(InetAddress address, boolean prefer_v6)
  5. toStringWithNoHostname(InetAddress address)