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 Password extends MessageAttribute {
21 super(MessageAttribute.MessageAttributeType.Password);
24 public Password(String password) {
25 super(MessageAttribute.MessageAttributeType.Password);
26 setPassword(password);
29 public String getPassword() {
33 public void setPassword(String password) {
34 this.password = password;
37 public byte[] getBytes() throws UtilityException {
38 int length = password.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 = password.getBytes();
54 System.arraycopy(temp, 0, result, 4, temp.length);
58 public static Password parse(byte[] data) {
59 Password result = new Password();
60 String password = new String(data);
61 result.setPassword(password);