Guest User

Untitled

a guest
Dec 31st, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. package <edit_your_packet>;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.util.Random;
  6.  
  7. /**
  8. * DatagramByte.
  9. *
  10. * @since 1.0
  11. * @author bearsoft
  12. */
  13. public class DatagramByte {
  14.  
  15. // -------------------- METHODS -------------------- //
  16.  
  17. /**
  18. * This method encode the byte array.
  19. * @param array - the array.
  20. * @return - Returns the byte encoded array, like (int) array.
  21. */
  22. public int[][] encode(byte[] array)
  23. {
  24. if (array == null) throw new IllegalArgumentException("The array is null.");
  25.  
  26. int[][] iarray = new int[array.length + 2][array.length + 2];
  27. int[] randomize = randomize(array.length + 2, 4);
  28.  
  29. for (int i = 0; i < array.length; i++)
  30. {
  31. iarray[i][iarray.length - 2] = array[i] << randomize[i];
  32. iarray[iarray.length - 1][i] = randomize[i];
  33. }
  34.  
  35. return iarray;
  36. }
  37.  
  38.  
  39. /**
  40. * This method decode the (int) array.
  41. * @param array - the array.
  42. * @return - Returns the (int) array decoded, like (byte) array.
  43. */
  44. public byte[] decode(int[][] array)
  45. {
  46. if (array == null) throw new IllegalArgumentException("The array is null.");
  47.  
  48. byte[] b = new byte[array.length - 2];
  49.  
  50. for (int i = 0; i < b.length; i++)
  51. {
  52. b[i] = (byte) (array[i][array.length - 2] >> array[array.length - 1][i]);
  53. }
  54.  
  55. return b;
  56. }
  57.  
  58. /**
  59. * This method randomize the (int) array.
  60. * Each the index of array is a key.
  61. * @param length - the length.
  62. * @param bound - the bound.
  63. * @return - Returns the (int) array contains the keys of each byte encode.
  64. */
  65. public int[] randomize(int length, int bound)
  66. {
  67. if (length <= 0 || bound <= 0) throw new IllegalArgumentException("The length or bound can't be less than or equals 0.");
  68.  
  69. Random random = new Random();
  70.  
  71. int[] iarray = new int[length];
  72.  
  73. for (int i = 0; i < iarray.length; i++)
  74. {
  75. iarray[i] = random.nextInt(bound);
  76. }
  77.  
  78. return iarray;
  79. }
  80.  
  81. /**
  82. * This method write the string that contains in the (byte) array.
  83. * @param array - the array.
  84. * @return - Returns the string.
  85. */
  86. public String write(byte[] array)
  87. {
  88. if (array == null) throw new IllegalArgumentException("The array is null.");
  89.  
  90. ByteArrayOutputStream output = new ByteArrayOutputStream();
  91.  
  92. try {
  93. output.write(array);
  94. return output.toString();
  95. } catch (IOException e) {
  96. e.printStackTrace();
  97. }
  98.  
  99. return null;
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment