]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jstun-src-0.7.1/de/javawi/jstun/attribute/Username.java
1afaec7b129ffbc2e1addae17d77d24006b207ec
[linphone] / p2pproxy / dependencies-src / jstun-src-0.7.1 / de / javawi / jstun / attribute / Username.java
1 /*
2  * This file is part of JSTUN. 
3  * 
4  * Copyright (c) 2005 Thomas King <king@t-king.de> - All rights
5  * reserved.
6  * 
7  * This software is licensed under either the GNU Public License (GPL),
8  * or the Apache 2.0 license. Copies of both license agreements are
9  * included in this distribution.
10  */
11
12 package de.javawi.jstun.attribute;
13
14 import de.javawi.jstun.util.Utility;
15 import de.javawi.jstun.util.UtilityException;
16
17 public class Username extends MessageAttribute {
18         String username;
19         
20         public Username() {
21                 super(MessageAttribute.MessageAttributeType.Username);
22         }
23         
24         public Username(String username) {
25                 super(MessageAttribute.MessageAttributeType.Username);
26                 setUsername(username);
27         }
28         
29         public String getUsername() {
30                 return username;
31         }
32         
33         public void setUsername(String username) {
34                 this.username = username;
35         }
36         
37         public byte[] getBytes() throws UtilityException {
38                 int length = username.length();
39                 // username header
40                 if ((length % 4) != 0) {
41                         length += 4 - (length % 4);
42                 }
43                 // message attribute header
44                 length += 4;
45                 byte[] result = new byte[length];
46                 // message attribute header
47                 // type
48                 System.arraycopy(Utility.integerToTwoBytes(typeToInteger(type)), 0, result, 0, 2);
49                 // length
50                 System.arraycopy(Utility.integerToTwoBytes(length-4), 0, result, 2, 2);
51                 
52                 // username header
53                 byte[] temp = username.getBytes();
54                 System.arraycopy(temp, 0, result, 4, temp.length);
55                 return result;
56         }
57         
58         public static Username parse(byte[] data) {
59                 Username result = new Username();
60                 String username = new String(data);
61                 result.setUsername(username);
62                 return result;
63         }
64 }