Unsupported library that attempts to punch holes through NAT
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Username.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 Username extends MessageAttribute {
  15. String username;
  16. public Username() {
  17. super(MessageAttribute.MessageAttributeType.Username);
  18. }
  19. public Username(String username) {
  20. super(MessageAttribute.MessageAttributeType.Username);
  21. setUsername(username);
  22. }
  23. public String getUsername() {
  24. return username;
  25. }
  26. public void setUsername(String username) {
  27. this.username = username;
  28. }
  29. public byte[] getBytes() throws UtilityException {
  30. int length = username.length();
  31. // username 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. // username header
  44. byte[] temp = username.getBytes();
  45. System.arraycopy(temp, 0, result, 4, temp.length);
  46. return result;
  47. }
  48. public static Username parse(byte[] data) {
  49. Username result = new Username();
  50. String username = new String(data);
  51. result.setUsername(username);
  52. return result;
  53. }
  54. }