Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.stream.IntStream;
- public final class Chunker implements IChunker{
- private static final int CHUNK_SIZE = 512;
- private static final byte[] KEY_WORD =
- new byte[]{34, 36, 83, 84, 79, 80, 95, 84, 82, 65, 78, 83, 70, 69, 82, 82, 73, 78, 71, 36, 34};
- /**
- * This method is used to convert inputArray to a list of chunks, and added
- * key word to the end.
- *
- * @param inputArray
- * @return List of chunks
- */
- @Override
- public List<byte[]> split(byte[] inputArray) {
- List<byte[]> chunks = new ArrayList<>();
- for (int index = 0; index < inputArray.length; index += CHUNK_SIZE) {
- if (inputArray.length - index < CHUNK_SIZE) {
- chunks.add(Arrays.copyOfRange(inputArray, index, inputArray.length - (CHUNK_SIZE * index + 1)));
- break;
- }
- chunks.add(Arrays.copyOfRange(inputArray, index, index + CHUNK_SIZE - 1));
- }
- chunks.add(getKeyWord());
- return chunks;
- }
- /**
- * This method is used to get "stop chunk", which shows us
- * end of transferring query to server.
- *
- * @return KEY_WORD
- */
- @Override
- public byte[] getKeyWord() {
- byte[] stopChunk = new byte[CHUNK_SIZE];
- for (int index = 0; index < KEY_WORD.length; index++) {
- stopChunk[index] = KEY_WORD[index];
- }
- return stopChunk;
- }
- /**
- * This method joins all chunks to one array.
- *
- * @param chunks
- * @return outputArray
- */
- @Override
- public byte[] join(List<byte[]> chunks) {
- chunks.remove(chunks.size() - 1);
- byte[] outputArray = new byte[CHUNK_SIZE * chunks.size()];
- for (int iteration = 0; iteration < chunks.size(); iteration++) {
- for (int index = 0; index < CHUNK_SIZE; index++) {
- outputArray[index + iteration * CHUNK_SIZE] = chunks.get(iteration)[index];
- }
- }
- return outputArray;
- }
- public static void main(String[] args) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement