DefaultDnsRecordDecoder implements DnsRecordDecoder { @Override public final <T extends DnsRecord> T decodeRecord(ByteBuf in) throws Exception { final int startOffset = in.readerIndex(); final String name = decodeName(in); final int endOffset = in.writerIndex(); if (endOffset - startOffset < 10) { in.readerIndex(startOffset); return null; } final DnsRecordType type = DnsRecordType.valueOf(in.readUnsignedShort()); final int aClass = in.readUnsignedShort(); final long ttl = in.readUnsignedInt(); final int length = in.readUnsignedShort(); final int offset = in.readerIndex(); if (endOffset - offset < length) { in.readerIndex(startOffset); return null; } @SuppressWarnings("unchecked") T record = (T) decodeRecord(name, type, aClass, ttl, in, offset, length); in.readerIndex(offset + length); return record; } protected  DefaultDnsRecordDecoder(); @Override final DnsQuestion decodeQuestion(ByteBuf in); @Override final T decodeRecord(ByteBuf in); static String decodeName(ByteBuf in);  }
@Test public void testDecodePtrRecord() throws Exception { DefaultDnsRecordDecoder decoder = new DefaultDnsRecordDecoder(); ByteBuf buffer = Unpooled.buffer().writeByte(0); int readerIndex = buffer.readerIndex(); int writerIndex = buffer.writerIndex(); try { DnsPtrRecord record = (DnsPtrRecord) decoder.decodeRecord( "netty.io", DnsRecordType.PTR, DnsRecord.CLASS_IN, 60, buffer, 0, 1); assertEquals("netty.io.", record.name()); assertEquals(DnsRecord.CLASS_IN, record.dnsClass()); assertEquals(60, record.timeToLive()); assertEquals(DnsRecordType.PTR, record.type()); assertEquals(readerIndex, buffer.readerIndex()); assertEquals(writerIndex, buffer.writerIndex()); } finally { buffer.release(); } }