001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    
021    package org.apache.directory.server.dns.io.encoder;
022    
023    
024    import org.apache.directory.server.dns.messages.QuestionRecord;
025    import org.apache.directory.server.dns.messages.RecordClass;
026    import org.apache.directory.server.dns.messages.RecordType;
027    import org.apache.mina.core.buffer.IoBuffer;
028    
029    
030    /**
031     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032     * @version $Rev: 725712 $, $Date: 2008-12-11 16:32:04 +0100 (Thu, 11 Dec 2008) $
033     */
034    public class QuestionRecordEncoder
035    {
036        /**
037         * Encodes the {@link QuestionRecord} into the {@link ByteBuffer}.
038         *
039         * @param out
040         * @param question
041         */
042        public void put( IoBuffer out, QuestionRecord question )
043        {
044            encodeDomainName( out, question.getDomainName() );
045            encodeRecordType( out, question.getRecordType() );
046            encodeRecordClass( out, question.getRecordClass() );
047        }
048    
049    
050        private void encodeDomainName( IoBuffer byteBuffer, String domainName )
051        {
052            String[] labels = domainName.split( "\\." );
053    
054            for ( int ii = 0; ii < labels.length; ii++ )
055            {
056                byteBuffer.put( ( byte ) labels[ii].length() );
057    
058                char[] characters = labels[ii].toCharArray();
059                for ( int jj = 0; jj < characters.length; jj++ )
060                {
061                    byteBuffer.put( ( byte ) characters[jj] );
062                }
063            }
064    
065            byteBuffer.put( ( byte ) 0x00 );
066        }
067    
068    
069        private void encodeRecordType( IoBuffer byteBuffer, RecordType recordType )
070        {
071            byteBuffer.putShort( recordType.convert() );
072        }
073    
074    
075        private void encodeRecordClass( IoBuffer byteBuffer, RecordClass recordClass )
076        {
077            byteBuffer.putShort( recordClass.convert() );
078        }
079    }