001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Alexei Y. Zakharov
021: * @version $Revision: 1.1.2.4 $
022: */package org.apache.harmony.jndi.provider.dns;
023:
024: import org.apache.harmony.jndi.internal.nls.Messages;
025:
026: /**
027: * Represents domain protocol Question Record
028: *
029: * @see RFC 1035
030: */
031: public class QuestionRecord {
032:
033: /** domain name */
034: private String qName;
035:
036: /** type of query */
037: private int qType;
038:
039: /** class of query */
040: private int qClass;
041:
042: /** Empty constructor */
043: public QuestionRecord() {
044: }
045:
046: /**
047: * Constructs new question record with given parameters
048: *
049: * @param qName
050: * domain name
051: * @param qType
052: * question record type
053: * @param qClass
054: * question record class
055: */
056: public QuestionRecord(String qName, int qType, int qClass) {
057: this .qName = qName;
058: this .qType = qType;
059: this .qClass = qClass;
060: }
061:
062: /**
063: * Creates a sequence of bytes that represents the current question record.
064: *
065: * @param buffer
066: * the buffer in which the result byte sequence will be written
067: * @param startIdx
068: * the index in the buffer to start at
069: * @return updated index of the buffer array
070: */
071: public int writeBytes(byte[] buffer, int startIdx)
072: throws DomainProtocolException {
073: int idx = startIdx;
074:
075: // basic checkings
076: if (buffer == null) {
077: // jndi.32=buffer is null
078: throw new DomainProtocolException(Messages
079: .getString("jndi.32")); //$NON-NLS-1$
080: }
081: if (startIdx >= buffer.length || startIdx < 0) {
082: throw new ArrayIndexOutOfBoundsException();
083: }
084: // write the name
085: idx = ProviderMgr.writeName(qName, buffer, idx);
086: // QTYPE
087: idx = ProviderMgr.write16Int(qType, buffer, idx);
088: // QCLASS
089: idx = ProviderMgr.write16Int(qClass, buffer, idx);
090: return idx;
091: }
092:
093: // getters and setters methods
094:
095: /**
096: * Parses given sequence of bytes and constructs a question record from it.
097: *
098: * @param mesBytes
099: * the byte array that should be parsed
100: * @param startIdx
101: * an index of <code>mesBytes</code> array to start the parsing
102: * at
103: * @param resultQR
104: * an object the result of the operation will be stored into
105: * @return updated index of <code>mesBytes</code> array
106: * @throws DomainProtocolException
107: * if something went wrong
108: */
109: public static int parseRecord(byte[] mesBytes, int startIdx,
110: QuestionRecord resultQR) throws DomainProtocolException {
111: int idx = startIdx;
112: StringBuffer nameSB = new StringBuffer();
113:
114: if (resultQR == null) {
115: // jndi.33=Given resultQR is null
116: throw new NullPointerException(Messages
117: .getString("jndi.33")); //$NON-NLS-1$
118: }
119: // name
120: idx = ProviderMgr.parseName(mesBytes, idx, nameSB);
121: resultQR.setQName(nameSB.toString());
122: // QTYPE
123: resultQR.setQType(ProviderMgr.parse16Int(mesBytes, idx));
124: idx += 2;
125: // QCLASS
126: resultQR.setQClass(ProviderMgr.parse16Int(mesBytes, idx));
127: idx += 2;
128: return idx;
129: }
130:
131: @Override
132: public String toString() {
133: StringBuffer sb = new StringBuffer();
134: String qClassStr, qTypeStr;
135:
136: if (qType > 0 && qType < 256) {
137: qTypeStr = ProviderConstants.rrTypeNames[qType];
138: } else {
139: qTypeStr = String.valueOf(qType);
140: }
141: if (qClass > 0 && qClass < 256) {
142: qClassStr = ProviderConstants.rrClassNames[qClass];
143: } else {
144: qClassStr = String.valueOf(qClass);
145: }
146: sb.append(qClassStr);
147: sb.append(" "); //$NON-NLS-1$
148: sb.append(qTypeStr);
149: sb.append(" "); //$NON-NLS-1$
150: sb.append(qName);
151: return sb.toString();
152: }
153:
154: /**
155: * @return Returns the qClass.
156: */
157: public int getQClass() {
158: return qClass;
159: }
160:
161: /**
162: * @param class1
163: * The qClass to set.
164: */
165: public void setQClass(int class1) {
166: qClass = class1;
167: }
168:
169: /**
170: * @return Returns the qName.
171: */
172: public String getQName() {
173: return qName;
174: }
175:
176: /**
177: * @param name
178: * The qName to set.
179: */
180: public void setQName(String name) {
181: qName = name;
182: }
183:
184: /**
185: * @return Returns the qType.
186: */
187: public int getQType() {
188: return qType;
189: }
190:
191: /**
192: * @param type
193: * The qType to set.
194: */
195: public void setQType(int type) {
196: qType = type;
197: }
198:
199: }
|