001/*
002 * Copyright 2015-2024 the original author or authors
003 *
004 * This software is licensed under the Apache License, Version 2.0,
005 * the GNU Lesser General Public License version 2 or later ("LGPL")
006 * and the WTFPL.
007 * You may choose either license to govern your use of this software only
008 * upon the condition that you accept all of the terms of either
009 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL.
010 */
011package org.minidns.record;
012
013import java.io.DataInputStream;
014import java.io.IOException;
015import java.net.Inet6Address;
016
017import org.minidns.record.Record.TYPE;
018import org.minidns.util.InetAddressUtil;
019
020/**
021 * AAAA payload (an ipv6 pointer).
022 */
023public class AAAA extends InternetAddressRR<Inet6Address> {
024
025    @Override
026    public TYPE getType() {
027        return TYPE.AAAA;
028    }
029
030    public AAAA(Inet6Address inet6address) {
031        super(inet6address);
032        assert ip.length == 16;
033    }
034
035    public AAAA(byte[] ip) {
036        super(ip);
037        if (ip.length != 16) {
038            throw new IllegalArgumentException("IPv6 address in AAAA record is always 16 byte");
039        }
040    }
041
042    public AAAA(CharSequence ipv6CharSequence) {
043        this(InetAddressUtil.ipv6From(ipv6CharSequence));
044    }
045
046    public static AAAA parse(DataInputStream dis)
047            throws IOException {
048        byte[] ip = new byte[16];
049        dis.readFully(ip);
050        return new AAAA(ip);
051    }
052
053    @Override
054    public String toString() {
055        StringBuilder sb = new StringBuilder();
056        for (int i = 0; i < ip.length; i += 2) {
057            if (i != 0) {
058                sb.append(':');
059            }
060            sb.append(Integer.toHexString(
061                ((ip[i] & 0xff) << 8) + (ip[i + 1] & 0xff)
062            ));
063        }
064        return sb.toString();
065    }
066
067}