Unsupported library that attempts to punch holes through NAT
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Password.java 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. package de.javawi.jstun.attribute;
  12. import de.javawi.jstun.util.Utility;
  13. import de.javawi.jstun.util.UtilityException;
  14. public class Password extends MessageAttribute {
  15. String password;
  16. public Password() {
  17. super(MessageAttribute.MessageAttributeType.Password);
  18. }
  19. public Password(String password) {
  20. super(MessageAttribute.MessageAttributeType.Password);
  21. setPassword(password);
  22. }
  23. public String getPassword() {
  24. return password;
  25. }
  26. public void setPassword(String password) {
  27. this.password = password;
  28. }
  29. public byte[] getBytes() throws UtilityException {
  30. int length = password.length();
  31. // password header
  32. if ((length % 4) != 0) {
  33. length += 4 - (length % 4);
  34. }
  35. // message attribute header
  36. length += 4;
  37. byte[] result = new byte[length];
  38. // message attribute header
  39. // type
  40. System.arraycopy(Utility.integerToTwoBytes(typeToInteger(type)), 0, result, 0, 2);
  41. // length
  42. System.arraycopy(Utility.integerToTwoBytes(length - 4), 0, result, 2, 2);
  43. // password header
  44. byte[] temp = password.getBytes();
  45. System.arraycopy(temp, 0, result, 4, temp.length);
  46. return result;
  47. }
  48. public static Password parse(byte[] data) {
  49. Password result = new Password();
  50. String password = new String(data);
  51. result.setPassword(password);
  52. return result;
  53. }
  54. }