Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.stream.IntStream;
  5.  
  6. public final class Chunker implements IChunker{
  7.     private static final int CHUNK_SIZE = 512;
  8.     private static final byte[] KEY_WORD =
  9.             new byte[]{34, 36, 83, 84, 79, 80, 95, 84, 82, 65, 78, 83, 70, 69, 82, 82, 73, 78, 71, 36, 34};
  10.  
  11.     /**
  12.      * This method is used to convert inputArray to a list of chunks, and added
  13.      * key word to the end.
  14.      *
  15.      * @param inputArray
  16.      * @return List of chunks
  17.      */
  18.     @Override
  19.     public List<byte[]> split(byte[] inputArray) {
  20.         List<byte[]> chunks = new ArrayList<>();
  21.  
  22.         for (int index = 0; index < inputArray.length; index += CHUNK_SIZE) {
  23.             if (inputArray.length - index < CHUNK_SIZE) {
  24.                 chunks.add(Arrays.copyOfRange(inputArray, index, inputArray.length - (CHUNK_SIZE * index + 1)));
  25.                 break;
  26.             }
  27.  
  28.             chunks.add(Arrays.copyOfRange(inputArray, index, index + CHUNK_SIZE - 1));
  29.         }
  30.  
  31.         chunks.add(getKeyWord());
  32.  
  33.         return chunks;
  34.     }
  35.  
  36.     /**
  37.      * This method is used to get "stop chunk", which shows us
  38.      * end of transferring query to server.
  39.      *
  40.      * @return KEY_WORD
  41.      */
  42.     @Override
  43.     public byte[] getKeyWord() {
  44.         byte[] stopChunk = new byte[CHUNK_SIZE];
  45.         for (int index = 0; index < KEY_WORD.length; index++) {
  46.             stopChunk[index] = KEY_WORD[index];
  47.         }
  48.         return stopChunk;
  49.     }
  50.  
  51.     /**
  52.      * This method joins all chunks to one array.
  53.      *
  54.      * @param chunks
  55.      * @return outputArray
  56.      */
  57.     @Override
  58.     public byte[] join(List<byte[]> chunks) {
  59.         chunks.remove(chunks.size() - 1);
  60.  
  61.         byte[] outputArray = new byte[CHUNK_SIZE * chunks.size()];
  62.  
  63.         for (int iteration = 0; iteration < chunks.size(); iteration++) {
  64.             for (int index = 0; index < CHUNK_SIZE; index++) {
  65.                 outputArray[index + iteration * CHUNK_SIZE] = chunks.get(iteration)[index];
  66.             }
  67.         }
  68.  
  69.         return outputArray;
  70.     }
  71.  
  72.     public static void main(String[] args) {
  73.        
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement