Clover Coverage Report - JmDNS 3.4.1
Coverage timestamp: Thu Aug 25 2011 13:06:33 CEST
../../../img/srcFileCovDistChart4.png 59% of files have more coverage
83   307   36   3.95
28   159   0.43   21
21     1.71  
1    
 
  DNSMessage       Line # 19 83 0% 36 81 38.6% 0.38636363
 
  (23)
 
1    /**
2    *
3    */
4    package javax.jmdns.impl;
5   
6    import java.util.ArrayList;
7    import java.util.Collection;
8    import java.util.Collections;
9    import java.util.LinkedList;
10    import java.util.List;
11   
12    import javax.jmdns.impl.constants.DNSConstants;
13   
14    /**
15    * DNSMessage define a DNS message either incoming or outgoing.
16    *
17    * @author Werner Randelshofer, Rick Blair, Pierre Frisch
18    */
 
19    public abstract class DNSMessage {
20   
21    /**
22    *
23    */
24    public static final boolean MULTICAST = true;
25   
26    /**
27    *
28    */
29    public static final boolean UNICAST = false;
30   
31    // protected DatagramPacket _packet;
32    // protected int _off;
33    // protected int _len;
34    // protected byte[] _data;
35   
36    private int _id;
37   
38    boolean _multicast;
39   
40    private int _flags;
41   
42    protected final List<DNSQuestion> _questions;
43   
44    protected final List<DNSRecord> _answers;
45   
46    protected final List<DNSRecord> _authoritativeAnswers;
47   
48    protected final List<DNSRecord> _additionals;
49   
50    /**
51    * @param flags
52    * @param id
53    * @param multicast
54    */
 
55  1483 toggle protected DNSMessage(int flags, int id, boolean multicast) {
56  1455 super();
57  1488 _flags = flags;
58  1472 _id = id;
59  1487 _multicast = multicast;
60  1478 _questions = Collections.synchronizedList(new LinkedList<DNSQuestion>());
61  1467 _answers = Collections.synchronizedList(new LinkedList<DNSRecord>());
62  1483 _authoritativeAnswers = Collections.synchronizedList(new LinkedList<DNSRecord>());
63  1491 _additionals = Collections.synchronizedList(new LinkedList<DNSRecord>());
64    }
65   
66    // public DatagramPacket getPacket() {
67    // return _packet;
68    // }
69    //
70    // public int getOffset() {
71    // return _off;
72    // }
73    //
74    // public int getLength() {
75    // return _len;
76    // }
77    //
78    // public byte[] getData() {
79    // if ( _data == null ) _data = new byte[DNSConstants.MAX_MSG_TYPICAL];
80    // return _data;
81    // }
82   
83    /**
84    * @return message id
85    */
 
86  368 toggle public int getId() {
87  376 return (_multicast ? 0 : _id);
88    }
89   
90    /**
91    * @param id
92    * the id to set
93    */
 
94  673 toggle public void setId(int id) {
95  688 this._id = id;
96    }
97   
98    /**
99    * @return message flags
100    */
 
101  757 toggle public int getFlags() {
102  760 return _flags;
103    }
104   
105    /**
106    * @param flags
107    * the flags to set
108    */
 
109  645 toggle public void setFlags(int flags) {
110  652 this._flags = flags;
111    }
112   
113    /**
114    * @return true if multicast
115    */
 
116  1068 toggle public boolean isMulticast() {
117  1070 return _multicast;
118    }
119   
120    /**
121    * @return list of questions
122    */
 
123  1782 toggle public Collection<? extends DNSQuestion> getQuestions() {
124  1801 return _questions;
125    }
126   
127    /**
128    * @return number of questions in the message
129    */
 
130  1316 toggle public int getNumberOfQuestions() {
131  1318 return this.getQuestions().size();
132    }
133   
 
134  753 toggle public Collection<? extends DNSRecord> getAllAnswers() {
135  778 List<DNSRecord> aList = new ArrayList<DNSRecord>(_answers.size() + _authoritativeAnswers.size() + _additionals.size());
136  770 aList.addAll(_answers);
137  753 aList.addAll(_authoritativeAnswers);
138  769 aList.addAll(_additionals);
139  778 return aList;
140    }
141   
142    /**
143    * @return list of answers
144    */
 
145  1800 toggle public Collection<? extends DNSRecord> getAnswers() {
146  1803 return _answers;
147    }
148   
149    /**
150    * @return number of answers in the message
151    */
 
152  1316 toggle public int getNumberOfAnswers() {
153  1316 return this.getAnswers().size();
154    }
155   
156    /**
157    * @return list of authorities
158    */
 
159  1316 toggle public Collection<? extends DNSRecord> getAuthorities() {
160  1317 return _authoritativeAnswers;
161    }
162   
163    /**
164    * @return number of authorities in the message
165    */
 
166  1316 toggle public int getNumberOfAuthorities() {
167  1316 return this.getAuthorities().size();
168    }
169   
170    /**
171    * @return list of additional answers
172    */
 
173  1315 toggle public Collection<? extends DNSRecord> getAdditionals() {
174  1315 return _additionals;
175    }
176   
177    /**
178    * @return number of additional in the message
179    */
 
180  1313 toggle public int getNumberOfAdditionals() {
181  1316 return this.getAdditionals().size();
182    }
183   
184    /**
185    * Check if the message is truncated.
186    *
187    * @return true if the message was truncated
188    */
 
189  513 toggle public boolean isTruncated() {
190  524 return (_flags & DNSConstants.FLAGS_TC) != 0;
191    }
192   
193    /**
194    * Check if the message is a query.
195    *
196    * @return true is the message is a query
197    */
 
198  631 toggle public boolean isQuery() {
199  651 return (_flags & DNSConstants.FLAGS_QR_MASK) == DNSConstants.FLAGS_QR_QUERY;
200    }
201   
202    /**
203    * Check if the message is a response.
204    *
205    * @return true is the message is a response
206    */
 
207  1 toggle public boolean isResponse() {
208  1 return (_flags & DNSConstants.FLAGS_QR_MASK) == DNSConstants.FLAGS_QR_RESPONSE;
209    }
210   
211    /**
212    * Check if the message is empty
213    *
214    * @return true is the message is empty
215    */
 
216  900 toggle public boolean isEmpty() {
217  900 return (this.getNumberOfQuestions() + this.getNumberOfAnswers() + this.getNumberOfAuthorities() + this.getNumberOfAdditionals()) == 0;
218    }
219   
220    /**
221    * Debugging.
222    */
 
223  0 toggle String print() {
224  0 StringBuffer buf = new StringBuffer(200);
225  0 buf.append(this.toString());
226  0 buf.append("\n");
227  0 for (DNSQuestion question : _questions) {
228  0 buf.append("\tquestion: ");
229  0 buf.append(question);
230  0 buf.append("\n");
231    }
232  0 for (DNSRecord answer : _answers) {
233  0 buf.append("\tanswer: ");
234  0 buf.append(answer);
235  0 buf.append("\n");
236    }
237  0 for (DNSRecord answer : _authoritativeAnswers) {
238  0 buf.append("\tauthoritative: ");
239  0 buf.append(answer);
240  0 buf.append("\n");
241    }
242  0 for (DNSRecord answer : _additionals) {
243  0 buf.append("\tadditional: ");
244  0 buf.append(answer);
245  0 buf.append("\n");
246    }
247  0 return buf.toString();
248    }
249   
250    /**
251    * Debugging.
252    *
253    * @param data
254    * @return data dump
255    */
 
256  0 toggle protected String print(byte[] data) {
257  0 StringBuilder buf = new StringBuilder(4000);
258  0 for (int off = 0, len = data.length; off < len; off += 32) {
259  0 int n = Math.min(32, len - off);
260  0 if (off < 0x10) {
261  0 buf.append(' ');
262    }
263  0 if (off < 0x100) {
264  0 buf.append(' ');
265    }
266  0 if (off < 0x1000) {
267  0 buf.append(' ');
268    }
269  0 buf.append(Integer.toHexString(off));
270  0 buf.append(':');
271  0 int index = 0;
272  0 for (index = 0; index < n; index++) {
273  0 if ((index % 8) == 0) {
274  0 buf.append(' ');
275    }
276  0 buf.append(Integer.toHexString((data[off + index] & 0xF0) >> 4));
277  0 buf.append(Integer.toHexString((data[off + index] & 0x0F) >> 0));
278    }
279    // for incomplete lines
280  0 if (index < 32) {
281  0 for (int i = index; i < 32; i++) {
282  0 if ((i % 8) == 0) {
283  0 buf.append(' ');
284    }
285  0 buf.append(" ");
286    }
287    }
288  0 buf.append(" ");
289  0 for (index = 0; index < n; index++) {
290  0 if ((index % 8) == 0) {
291  0 buf.append(' ');
292    }
293  0 int ch = data[off + index] & 0xFF;
294  0 buf.append(((ch > ' ') && (ch < 127)) ? (char) ch : '.');
295    }
296  0 buf.append("\n");
297   
298    // limit message size
299  0 if (off + 32 >= 2048) {
300  0 buf.append("....\n");
301  0 break;
302    }
303    }
304  0 return buf.toString();
305    }
306   
307    }