/*
 * @(#)Address.java
 *
 * Copyright (c) 2001-2002 JScape
 * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * JScape. ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with
 * the terms of the license agreement you entered into with JScape.
 */

package com.jscape.inet.dns.record;


import com.jscape.inet.dns.DnsInputStream;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;


/**
 * Address record represents a host address.
 */
public class Address
        extends DnsRecord {
    private int[] ipAddress = new int[4];

    protected void decode(DnsInputStream inputStream)
            throws IOException {
        for (int i = 0; i < 4; ++i) {
            ipAddress[i] = inputStream.readByte();
        }
    }

    /**
     * Gets <code>byte[]</code> representing 32 bit ip address.
     *
     * @return byte[]
     */
    public byte[] getAddress() {
        byte[] ip = new byte[4];

        for (int j = 0; j < 4; ++j) {
            ip[j] = (byte) ipAddress[j];
        }
        return ip;
    }

    /**
     * Gets InetAddress for this Address.
     *
     * @return an InetAddress
     *
     * @throws UnknownHostException the IP address of a host could not be determined
     */
    public InetAddress getInetAddress()
            throws UnknownHostException {
        return InetAddress.getByName(toByteString());
    }

    private String toByteString() {
        return ipAddress[0] + "." + ipAddress[1] + "." + ipAddress[2] + "." + ipAddress[3];
    }

    /**
     * Gets String representation of this record.
     *
     * @return String
     */
    public String toString() {
        return getRecordName() + "\tinternet address = " + toByteString();
    }
}

