Advertisement
Guest User

Untitled

a guest
May 24th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. /**
  2. * Created by daniel.peczkowski on 2017-05-24.
  3. */
  4. public class ByteDataString implements Cloneable{
  5. public static final char[] HEX_CHARS = "0123456789ABCDEF".toCharArray();
  6. public final static char PAGE = 0xE100;//Custom symbols
  7. public final static String PAGE_STRING = "E1";
  8.  
  9. private final StringBuilder data = new StringBuilder();
  10.  
  11. public ByteDataString() {}
  12.  
  13. public ByteDataString(ByteDataString bds){
  14. append(bds);
  15. }
  16.  
  17. public ByteDataString(byte... bytes) {
  18. append(bytes);
  19. }
  20.  
  21. public ByteDataString(char... chars) {
  22. append(chars);
  23. }
  24.  
  25. public ByteDataString(String string, boolean hex) {
  26. append(string, hex);
  27. }
  28.  
  29. @Override
  30. protected ByteDataString clone() {
  31. return new ByteDataString(this);
  32. }
  33.  
  34. public StringBuilder getDataCopy() {
  35. return new StringBuilder(data);
  36. }
  37.  
  38. public StringBuilder getDataObject() {
  39. return data;
  40. }
  41.  
  42. public ByteDataString append(ByteDataString bds){
  43. data.append(bds.data);
  44. return this;
  45. }
  46.  
  47. public ByteDataString append(byte... bytes) {
  48. char[] chars = new char[bytes.length];
  49. for (int i = 0; i < chars.length; i++) {
  50. chars[i] = (char) (0xFF & bytes[i]);
  51. if (chars[i] < 0x20 || chars[i] >= 0x7f) {
  52. chars[i] |= PAGE;
  53. }
  54. }
  55. data.append(chars);
  56. return this;
  57. }
  58.  
  59. public ByteDataString append(char... chars) {
  60. for (int i = 0; i < chars.length; i++) {
  61. chars[i] &= 0xFF;
  62. if (chars[i] < 0x20 || chars[i] >= 0x7f) {
  63. chars[i] |= PAGE;
  64. }
  65. }
  66. data.append(chars);
  67. return this;
  68. }
  69.  
  70. public ByteDataString append(String string, boolean hex) {
  71. if (hex) {
  72. if ((string.length() & 1) == 1) throw new Error("Invalid length of String: " + string);
  73. char[] chars = new char[string.length() >>> 1];
  74. for (int i = 0; i < chars.length; i++) {
  75. chars[i] = (char) ((Character.digit(string.charAt(i << 1), 16) << 4)
  76. + Character.digit(string.charAt((i << 1)+1), 16));
  77. if (chars[i] < 0x20 || chars[i] >= 0x7f) {
  78. chars[i] |= PAGE;
  79. }
  80. }
  81. data.append(chars);
  82. return this;
  83. }
  84. return append(string.toCharArray());
  85. }
  86.  
  87. @Override
  88. public String toString() {
  89. return data.toString();
  90. }
  91.  
  92. public String toMultilineString() {
  93. return data.toString().replaceAll("\\u"+PAGE_STRING+"0A", "\n");
  94. }
  95.  
  96. public String toHexString(boolean whiteSpaces, boolean lines) {
  97. StringBuilder result = new StringBuilder();
  98. for (int i = 0; i < data.length(); ++i) {
  99. result.append(HEX_CHARS[(data.charAt(i) & 0xF0) >>> 4]);
  100. result.append(HEX_CHARS[data.charAt(i) & 0x0F]);
  101. if (whiteSpaces) {
  102. if (lines && (i & 0xF) == 0xF)
  103. result.append("\n");
  104. else
  105. result.append(" ");
  106. }
  107. }
  108. return result.toString();
  109. }
  110.  
  111.  
  112. public byte[] toBytes() {
  113. byte[] bytes = new byte[data.length()];
  114. for (int i = 0; i < bytes.length; i++) {
  115. bytes[i] = (byte) (data.charAt(i) & 0xff);
  116. }
  117. return bytes;
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement