View Javadoc
1   /*
2    * Copyright 2015 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.dns;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.util.internal.StringUtil;
20  
21  import static io.netty.util.internal.ObjectUtil.checkNotNull;
22  
23  /**
24   * The default {@code DnsRawRecord} implementation.
25   */
26  public class DefaultDnsRawRecord extends AbstractDnsRecord implements DnsRawRecord {
27  
28      private final ByteBuf content;
29  
30      /**
31       * Creates a new {@link #CLASS_IN IN-class} record.
32       *
33       * @param name the domain name
34       * @param type the type of the record
35       * @param timeToLive the TTL value of the record
36       */
37      public DefaultDnsRawRecord(String name, DnsRecordType type, long timeToLive, ByteBuf content) {
38          this(name, type, DnsRecord.CLASS_IN, timeToLive, content);
39      }
40  
41      /**
42       * Creates a new record.
43       *
44       * @param name the domain name
45       * @param type the type of the record
46       * @param dnsClass the class of the record, usually one of the following:
47       *                 <ul>
48       *                     <li>{@link #CLASS_IN}</li>
49       *                     <li>{@link #CLASS_CSNET}</li>
50       *                     <li>{@link #CLASS_CHAOS}</li>
51       *                     <li>{@link #CLASS_HESIOD}</li>
52       *                     <li>{@link #CLASS_NONE}</li>
53       *                     <li>{@link #CLASS_ANY}</li>
54       *                 </ul>
55       * @param timeToLive the TTL value of the record
56       */
57      public DefaultDnsRawRecord(
58              String name, DnsRecordType type, int dnsClass, long timeToLive, ByteBuf content) {
59          super(name, type, dnsClass, timeToLive);
60          this.content = checkNotNull(content, "content");
61      }
62  
63      @Override
64      public ByteBuf content() {
65          return content;
66      }
67  
68      @Override
69      public DnsRawRecord copy() {
70          return replace(content().copy());
71      }
72  
73      @Override
74      public DnsRawRecord duplicate() {
75          return replace(content().duplicate());
76      }
77  
78      @Override
79      public DnsRawRecord retainedDuplicate() {
80          return replace(content().retainedDuplicate());
81      }
82  
83      @Override
84      public DnsRawRecord replace(ByteBuf content) {
85          return new DefaultDnsRawRecord(name(), type(), dnsClass(), timeToLive(), content);
86      }
87  
88      @Override
89      public int refCnt() {
90          return content().refCnt();
91      }
92  
93      @Override
94      public DnsRawRecord retain() {
95          content().retain();
96          return this;
97      }
98  
99      @Override
100     public DnsRawRecord retain(int increment) {
101         content().retain(increment);
102         return this;
103     }
104 
105     @Override
106     public boolean release() {
107         return content().release();
108     }
109 
110     @Override
111     public boolean release(int decrement) {
112         return content().release(decrement);
113     }
114 
115     @Override
116     public DnsRawRecord touch() {
117         content().touch();
118         return this;
119     }
120 
121     @Override
122     public DnsRawRecord touch(Object hint) {
123         content().touch(hint);
124         return this;
125     }
126 
127     @Override
128     public String toString() {
129         final StringBuilder buf = new StringBuilder(64).append(StringUtil.simpleClassName(this)).append('(');
130         final DnsRecordType type = type();
131         if (type != DnsRecordType.OPT) {
132             buf.append(name().isEmpty()? "<root>" : name())
133                .append(' ')
134                .append(timeToLive())
135                .append(' ');
136 
137             DnsMessageUtil.appendRecordClass(buf, dnsClass())
138                           .append(' ')
139                           .append(type.name());
140         } else {
141             buf.append("OPT flags:")
142                .append(timeToLive())
143                .append(" udp:")
144                .append(dnsClass());
145         }
146 
147         buf.append(' ')
148            .append(content().readableBytes())
149            .append("B)");
150 
151         return buf.toString();
152     }
153 }