1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.directory.server.dns.io.encoder;
22
23
24 import java.io.IOException;
25
26 import org.apache.directory.server.dns.messages.RecordClass;
27 import org.apache.directory.server.dns.messages.RecordType;
28 import org.apache.directory.server.dns.messages.ResourceRecord;
29 import org.apache.mina.common.ByteBuffer;
30
31
32
33
34
35
36 public abstract class ResourceRecordEncoder implements RecordEncoder
37 {
38 public void put( ByteBuffer byteBuffer, ResourceRecord record ) throws IOException
39 {
40 putDomainName( byteBuffer, record.getDomainName() );
41 putRecordType( byteBuffer, record.getRecordType() );
42 putRecordClass( byteBuffer, record.getRecordClass() );
43
44 byteBuffer.putInt( record.getTimeToLive() );
45
46 putResourceRecord( byteBuffer, record );
47 }
48
49
50 protected abstract void putResourceRecordData( ByteBuffer byteBuffer, ResourceRecord record );
51
52
53 protected void putResourceRecord( ByteBuffer byteBuffer, ResourceRecord record )
54 {
55 int startPosition = byteBuffer.position();
56 byteBuffer.position( startPosition + 2 );
57
58 putResourceRecordData( byteBuffer, record );
59
60 putDataSize( byteBuffer, startPosition );
61 }
62
63
64 protected void putDataSize( ByteBuffer byteBuffer, int startPosition )
65 {
66 int endPosition = byteBuffer.position();
67 short length = ( short ) ( endPosition - startPosition - 2 );
68
69 byteBuffer.position( startPosition );
70 byteBuffer.putShort( length );
71 byteBuffer.position( endPosition );
72 }
73
74
75
76
77
78
79
80
81
82 protected void putDomainName( ByteBuffer byteBuffer, String domainName )
83 {
84 String[] labels = domainName.split( "\\." );
85
86 for ( int ii = 0; ii < labels.length; ii++ )
87 {
88 byteBuffer.put( ( byte ) labels[ii].length() );
89
90 char[] characters = labels[ii].toCharArray();
91 for ( int jj = 0; jj < characters.length; jj++ )
92 {
93 byteBuffer.put( ( byte ) characters[jj] );
94 }
95 }
96
97 byteBuffer.put( ( byte ) 0x00 );
98 }
99
100
101 protected void putRecordType( ByteBuffer byteBuffer, RecordType recordType )
102 {
103 byteBuffer.putShort( recordType.convert() );
104 }
105
106
107 protected void putRecordClass( ByteBuffer byteBuffer, RecordClass recordClass )
108 {
109 byteBuffer.putShort( recordClass.convert() );
110 }
111
112
113
114
115
116
117
118
119
120
121 protected void putCharacterString( ByteBuffer byteBuffer, String characterString )
122 {
123 byteBuffer.put( ( byte ) characterString.length() );
124
125 char[] characters = characterString.toCharArray();
126
127 for ( int ii = 0; ii < characters.length; ii++ )
128 {
129 byteBuffer.put( ( byte ) characters[ii] );
130 }
131 }
132 }