2 * This file is part of JSTUN.
4 * Copyright (c) 2005 Thomas King <king@t-king.de> - All rights
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.
12 package de.javawi.jstun.attribute;
14 import de.javawi.jstun.util.Utility;
15 import de.javawi.jstun.util.UtilityException;
17 public class Username extends MessageAttribute {
21 super(MessageAttribute.MessageAttributeType.Username);
24 public Username(String username) {
25 super(MessageAttribute.MessageAttributeType.Username);
26 setUsername(username);
29 public String getUsername() {
33 public void setUsername(String username) {
34 this.username = username;
37 public byte[] getBytes() throws UtilityException {
38 int length = username.length();
40 if ((length % 4) != 0) {
41 length += 4 - (length % 4);
43 // message attribute header
45 byte[] result = new byte[length];
46 // message attribute header
48 System.arraycopy(Utility.integerToTwoBytes(typeToInteger(type)), 0, result, 0, 2);
50 System.arraycopy(Utility.integerToTwoBytes(length-4), 0, result, 2, 2);
53 byte[] temp = username.getBytes();
54 System.arraycopy(temp, 0, result, 4, temp.length);
58 public static Username parse(byte[] data) {
59 Username result = new Username();
60 String username = new String(data);
61 result.setUsername(username);