PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/jstun/core/attribute/Username.java

https://github.com/swapnapalaniswamy/batphone
Java | 64 lines | 38 code | 10 blank | 16 comment | 2 complexity | 050694661b8ae7998d16486f759f1616 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause
  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 com.jstun.core.attribute;
  12. import com.jstun.core.util.Utility;
  13. import com.jstun.core.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. }