Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package <edit_your_packet>;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.util.Random;
- /**
- * DatagramByte.
- *
- * @since 1.0
- * @author bearsoft
- */
- public class DatagramByte {
- // -------------------- METHODS -------------------- //
- /**
- * This method encode the byte array.
- * @param array - the array.
- * @return - Returns the byte encoded array, like (int) array.
- */
- public int[][] encode(byte[] array)
- {
- if (array == null) throw new IllegalArgumentException("The array is null.");
- int[][] iarray = new int[array.length + 2][array.length + 2];
- int[] randomize = randomize(array.length + 2, 4);
- for (int i = 0; i < array.length; i++)
- {
- iarray[i][iarray.length - 2] = array[i] << randomize[i];
- iarray[iarray.length - 1][i] = randomize[i];
- }
- return iarray;
- }
- /**
- * This method decode the (int) array.
- * @param array - the array.
- * @return - Returns the (int) array decoded, like (byte) array.
- */
- public byte[] decode(int[][] array)
- {
- if (array == null) throw new IllegalArgumentException("The array is null.");
- byte[] b = new byte[array.length - 2];
- for (int i = 0; i < b.length; i++)
- {
- b[i] = (byte) (array[i][array.length - 2] >> array[array.length - 1][i]);
- }
- return b;
- }
- /**
- * This method randomize the (int) array.
- * Each the index of array is a key.
- * @param length - the length.
- * @param bound - the bound.
- * @return - Returns the (int) array contains the keys of each byte encode.
- */
- public int[] randomize(int length, int bound)
- {
- if (length <= 0 || bound <= 0) throw new IllegalArgumentException("The length or bound can't be less than or equals 0.");
- Random random = new Random();
- int[] iarray = new int[length];
- for (int i = 0; i < iarray.length; i++)
- {
- iarray[i] = random.nextInt(bound);
- }
- return iarray;
- }
- /**
- * This method write the string that contains in the (byte) array.
- * @param array - the array.
- * @return - Returns the string.
- */
- public String write(byte[] array)
- {
- if (array == null) throw new IllegalArgumentException("The array is null.");
- ByteArrayOutputStream output = new ByteArrayOutputStream();
- try {
- output.write(array);
- return output.toString();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment