Java Socket Address Get writeAddress(InetSocketAddress address, DataOutput out)

Here you can find the source of writeAddress(InetSocketAddress address, DataOutput out)

Description

Writes InetSocketAddress to the specified data output.

License

Apache License

Parameter

Parameter Description
address Address to write.
out Data output.

Exception

Parameter Description
IOException If write operation failed.

Declaration

public static void writeAddress(InetSocketAddress address, DataOutput out) throws IOException 

Method Source Code

//package com.java2s;
/*//from ww w.  j a  v a2s  .c o m
 * Copyright 2019 The Hekate Project
 *
 * The Hekate Project licenses this file to you under the Apache License,
 * version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * 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.IOException;

import java.net.InetSocketAddress;

public class Main {
    private static final int INT_BITS = 31;

    /**
     * Writes {@link InetSocketAddress} to the specified data output.
     *
     * @param address Address to write.
     * @param out Data output.
     *
     * @throws IOException If write operation failed.
     * @see #readAddress(DataInput)
     */
    public static void writeAddress(InetSocketAddress address, DataOutput out) throws IOException {
        // Address host.
        byte[] hostBytes = address.getAddress().getAddress();

        out.writeByte(hostBytes.length);
        out.write(hostBytes);

        // Address port.
        writeVarInt(address.getPort(), out);
    }

    /**
     * Encodes a value using the variable-length encoding from <a href="http://code.google.com/apis/protocolbuffers/docs/encoding.html">
     * Google Protocol Buffers</a>. It uses zig-zag encoding to efficiently encode signed values. If values are known to be non-negative,
     * {@link #writeVarIntUnsigned(int, DataOutput)}  should be used.
     *
     * @param value Value to encode
     * @param out Data output.
     *
     * @throws IOException if failed to write value.
     */
    // Code borrowed from 'stream-lib' (Apache 2.0 license) - see https://github.com/addthis/stream-lib
    public static void writeVarInt(int value, DataOutput out) throws IOException {
        // Great trick from http://code.google.com/apis/protocolbuffers/docs/encoding.html#types
        writeVarIntUnsigned(value << 1 ^ value >> INT_BITS, out);
    }

    /**
     * Encodes a value using the variable-length encoding from <a href="http://code.google.com/apis/protocolbuffers/docs/encoding.html">
     * Google Protocol Buffers</a>. Zig-zag is not used, so input must not be negative. If values can be negative, use {@link
     * #writeVarInt(int, DataOutput)} instead. This method treats negative input as like a large unsigned value.
     *
     * @param value Value to encode (must be non-negative).
     * @param out Data output.
     *
     * @throws IOException if failed to write value.
     */
    // Code borrowed from 'stream-lib' (Apache 2.0 license) - see https://github.com/addthis/stream-lib
    public static void writeVarIntUnsigned(int value, DataOutput out) throws IOException {
        while ((value & 0xFFFFFF80) != 0L) {
            out.writeByte((value & 0x7F) | 0x80);

            value >>>= 7;
        }

        out.writeByte(value & 0x7F);
    }
}

Related

  1. toSocketAddress(URL url)
  2. toString(InetSocketAddress addr)
  3. toString(InetSocketAddress entryServer)
  4. toString(InetSocketAddress socketAddress)
  5. writeAddress(DataOutputStream outputStream, InetSocketAddress address, int version)