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.DataOutputStream;
015import java.io.IOException;
016
017import org.minidns.record.Record.TYPE;
018
019public final class UNKNOWN extends Data {
020
021    private final int typeValue;
022    private final byte[] data;
023
024    private UNKNOWN(DataInputStream dis, int payloadLength, int typeValue) throws IOException {
025        this.typeValue = typeValue;
026        this.data = new byte[payloadLength];
027        dis.readFully(data);
028    }
029
030    @Override
031    public TYPE getType() {
032        return TYPE.getType(typeValue);
033    }
034
035    /**
036     * Get the binary type value of this payload. In contrast to {@link #getType()}, this also returns the actual value
037     * if the type is unknown to MiniDNS.
038     *
039     * @return the binary type value.
040     */
041    public int getTypeValue() {
042        return typeValue;
043    }
044
045    @Override
046    public void serialize(DataOutputStream dos) throws IOException {
047        dos.write(data);
048    }
049
050    public static UNKNOWN parse(DataInputStream dis, int payloadLength, int typeValue)
051            throws IOException {
052        return new UNKNOWN(dis, payloadLength, typeValue);
053    }
054
055    /**
056     * Parse an UNKNOWN payload.
057     *
058     * @param dis the input stream.
059     * @param payloadLength the length of the payload.
060     * @param type the type of the payload.
061     * @return the parsed payload.
062     * @throws IOException in case of an I/O error.
063     * @deprecated use {@link #parse(DataInputStream, int, int)} instead, as this loses the actual binary type value if
064     *             the type is unknown to MiniDNS.
065     */
066    @Deprecated
067    public static UNKNOWN parse(DataInputStream dis, int payloadLength, TYPE type)
068            throws IOException {
069        return parse(dis, payloadLength, type.getValue());
070    }
071
072}