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    *   http://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  import io.netty.util.internal.UnstableApi;
21  
22  import static io.netty.util.internal.ObjectUtil.checkNotNull;
23  
24  /**
25   * The default {@code DnsRawRecord} implementation.
26   */
27  @UnstableApi
28  public class DefaultDnsRawRecord extends AbstractDnsRecord implements DnsRawRecord {
29  
30      private final ByteBuf content;
31  
32      /**
33       * Creates a new {@link #CLASS_IN IN-class} record.
34       *
35       * @param name the domain name
36       * @param type the type of the record
37       * @param timeToLive the TTL value of the record
38       */
39      public DefaultDnsRawRecord(String name, DnsRecordType type, long timeToLive, ByteBuf content) {
40          this(name, type, DnsRecord.CLASS_IN, timeToLive, content);
41      }
42  
43      /**
44       * Creates a new record.
45       *
46       * @param name the domain name
47       * @param type the type of the record
48       * @param dnsClass the class of the record, usually one of the following:
49       *                 <ul>
50       *                     <li>{@link #CLASS_IN}</li>
51       *                     <li>{@link #CLASS_CSNET}</li>
52       *                     <li>{@link #CLASS_CHAOS}</li>
53       *                     <li>{@link #CLASS_HESIOD}</li>
54       *                     <li>{@link #CLASS_NONE}</li>
55       *                     <li>{@link #CLASS_ANY}</li>
56       *                 </ul>
57       * @param timeToLive the TTL value of the record
58       */
59      public DefaultDnsRawRecord(
60              String name, DnsRecordType type, int dnsClass, long timeToLive, ByteBuf content) {
61          super(name, type, dnsClass, timeToLive);
62          this.content = checkNotNull(content, "content");
63      }
64  
65      @Override
66      public ByteBuf content() {
67          return content;
68      }
69  
70      @Override
71      public DnsRawRecord copy() {
72          return replace(content().copy());
73      }
74  
75      @Override
76      public DnsRawRecord duplicate() {
77          return replace(content().duplicate());
78      }
79  
80      @Override
81      public DnsRawRecord retainedDuplicate() {
82          return replace(content().retainedDuplicate());
83      }
84  
85      @Override
86      public DnsRawRecord replace(ByteBuf content) {
87          return new DefaultDnsRawRecord(name(), type(), dnsClass(), timeToLive(), content);
88      }
89  
90      @Override
91      public int refCnt() {
92          return content().refCnt();
93      }
94  
95      @Override
96      public DnsRawRecord retain() {
97          content().retain();
98          return this;
99      }
100 
101     @Override
102     public DnsRawRecord retain(int increment) {
103         content().retain(increment);
104         return this;
105     }
106 
107     @Override
108     public boolean release() {
109         return content().release();
110     }
111 
112     @Override
113     public boolean release(int decrement) {
114         return content().release(decrement);
115     }
116 
117     @Override
118     public DnsRawRecord touch() {
119         content().touch();
120         return this;
121     }
122 
123     @Override
124     public DnsRawRecord touch(Object hint) {
125         content().touch(hint);
126         return this;
127     }
128 
129     @Override
130     public String toString() {
131         final StringBuilder buf = new StringBuilder(64).append(StringUtil.simpleClassName(this)).append('(');
132         final DnsRecordType type = type();
133         if (type != DnsRecordType.OPT) {
134             buf.append(name().isEmpty()? "<root>" : name())
135                .append(' ')
136                .append(timeToLive())
137                .append(' ');
138 
139             DnsMessageUtil.appendRecordClass(buf, dnsClass())
140                           .append(' ')
141                           .append(type.name());
142         } else {
143             buf.append("OPT flags:")
144                .append(timeToLive())
145                .append(" udp:")
146                .append(dnsClass());
147         }
148 
149         buf.append(' ')
150            .append(content().readableBytes())
151            .append("B)");
152 
153         return buf.toString();
154     }
155 }