001/*
002 * Copyright 2015-2020 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 org.minidns.util.Base64;
014
015import java.io.DataInputStream;
016import java.io.DataOutputStream;
017import java.io.IOException;
018
019public class OPENPGPKEY extends Data {
020
021    private final byte[] publicKeyPacket;
022
023    public static OPENPGPKEY parse(DataInputStream dis, int length) throws IOException {
024        byte[] publicKeyPacket = new byte[length];
025        dis.readFully(publicKeyPacket);
026        return new OPENPGPKEY(publicKeyPacket);
027    }
028
029    OPENPGPKEY(byte[] publicKeyPacket) {
030        this.publicKeyPacket = publicKeyPacket;
031    }
032
033    @Override
034    public Record.TYPE getType() {
035        return Record.TYPE.OPENPGPKEY;
036    }
037
038    @Override
039    public void serialize(DataOutputStream dos) throws IOException {
040        dos.write(publicKeyPacket);
041    }
042
043    @Override
044    public String toString() {
045        return getPublicKeyPacketBase64();
046    }
047
048    private transient String publicKeyPacketBase64Cache;
049
050    public String getPublicKeyPacketBase64() {
051        if (publicKeyPacketBase64Cache == null) {
052            publicKeyPacketBase64Cache = Base64.encodeToString(publicKeyPacket);
053        }
054        return publicKeyPacketBase64Cache;
055    }
056
057    public byte[] getPublicKeyPacket() {
058        return publicKeyPacket.clone();
059    }
060}