@Test public void testGetChunkEmpty() throws IOException { File file = new File(parent, randomString()); FileUtils.write(file, ""); Chunk chunk = FileChunkUtil.getChunk(file, TestFileConfiguration.CHUNK_SIZE, 0, randomString()); assertEquals(0, chunk.getSize()); assertEquals(0, chunk.getOrder()); file.deleteOnExit(); }
public static Chunk getChunk(File file, int chunkSize, int chunkNumber, String chunkId) throws IOException { if (file == null || !file.exists()) { throw new IOException("File does not exist"); } else if (chunkSize <= 0) { throw new IOException("Chunk size cannot be smaller or equal to 0"); } else if (chunkNumber < 0) { throw new IOException("Chunk number cannot be smaller than 0"); } if (FileUtil.getFileSize(file) == 0 && chunkNumber == 0) { return new Chunk(chunkId, new byte[0], 0); } int read = 0; long offset = chunkSize * (long) chunkNumber; byte[] data = new byte[chunkSize]; RandomAccessFile rndAccessFile = new RandomAccessFile(file, "r"); rndAccessFile.seek(offset); read = rndAccessFile.read(data); rndAccessFile.close(); if (read > 0) { data = truncateData(data, read); return new Chunk(chunkId, data, chunkNumber); } else { return null; } }
FileChunkUtil { public static Chunk getChunk(File file, int chunkSize, int chunkNumber, String chunkId) throws IOException { if (file == null || !file.exists()) { throw new IOException("File does not exist"); } else if (chunkSize <= 0) { throw new IOException("Chunk size cannot be smaller or equal to 0"); } else if (chunkNumber < 0) { throw new IOException("Chunk number cannot be smaller than 0"); } if (FileUtil.getFileSize(file) == 0 && chunkNumber == 0) { return new Chunk(chunkId, new byte[0], 0); } int read = 0; long offset = chunkSize * (long) chunkNumber; byte[] data = new byte[chunkSize]; RandomAccessFile rndAccessFile = new RandomAccessFile(file, "r"); rndAccessFile.seek(offset); read = rndAccessFile.read(data); rndAccessFile.close(); if (read > 0) { data = truncateData(data, read); return new Chunk(chunkId, data, chunkNumber); } else { return null; } } }
FileChunkUtil { public static Chunk getChunk(File file, int chunkSize, int chunkNumber, String chunkId) throws IOException { if (file == null || !file.exists()) { throw new IOException("File does not exist"); } else if (chunkSize <= 0) { throw new IOException("Chunk size cannot be smaller or equal to 0"); } else if (chunkNumber < 0) { throw new IOException("Chunk number cannot be smaller than 0"); } if (FileUtil.getFileSize(file) == 0 && chunkNumber == 0) { return new Chunk(chunkId, new byte[0], 0); } int read = 0; long offset = chunkSize * (long) chunkNumber; byte[] data = new byte[chunkSize]; RandomAccessFile rndAccessFile = new RandomAccessFile(file, "r"); rndAccessFile.seek(offset); read = rndAccessFile.read(data); rndAccessFile.close(); if (read > 0) { data = truncateData(data, read); return new Chunk(chunkId, data, chunkNumber); } else { return null; } } private  FileChunkUtil(); }
FileChunkUtil { public static Chunk getChunk(File file, int chunkSize, int chunkNumber, String chunkId) throws IOException { if (file == null || !file.exists()) { throw new IOException("File does not exist"); } else if (chunkSize <= 0) { throw new IOException("Chunk size cannot be smaller or equal to 0"); } else if (chunkNumber < 0) { throw new IOException("Chunk number cannot be smaller than 0"); } if (FileUtil.getFileSize(file) == 0 && chunkNumber == 0) { return new Chunk(chunkId, new byte[0], 0); } int read = 0; long offset = chunkSize * (long) chunkNumber; byte[] data = new byte[chunkSize]; RandomAccessFile rndAccessFile = new RandomAccessFile(file, "r"); rndAccessFile.seek(offset); read = rndAccessFile.read(data); rndAccessFile.close(); if (read > 0) { data = truncateData(data, read); return new Chunk(chunkId, data, chunkNumber); } else { return null; } } private  FileChunkUtil(); static int getNumberOfChunks(File file, int chunkSize); static Chunk getChunk(File file, int chunkSize, int chunkNumber, String chunkId); static void reassembly(List<File> fileParts, File destination); }
FileChunkUtil { public static Chunk getChunk(File file, int chunkSize, int chunkNumber, String chunkId) throws IOException { if (file == null || !file.exists()) { throw new IOException("File does not exist"); } else if (chunkSize <= 0) { throw new IOException("Chunk size cannot be smaller or equal to 0"); } else if (chunkNumber < 0) { throw new IOException("Chunk number cannot be smaller than 0"); } if (FileUtil.getFileSize(file) == 0 && chunkNumber == 0) { return new Chunk(chunkId, new byte[0], 0); } int read = 0; long offset = chunkSize * (long) chunkNumber; byte[] data = new byte[chunkSize]; RandomAccessFile rndAccessFile = new RandomAccessFile(file, "r"); rndAccessFile.seek(offset); read = rndAccessFile.read(data); rndAccessFile.close(); if (read > 0) { data = truncateData(data, read); return new Chunk(chunkId, data, chunkNumber); } else { return null; } } private  FileChunkUtil(); static int getNumberOfChunks(File file, int chunkSize); static Chunk getChunk(File file, int chunkSize, int chunkNumber, String chunkId); static void reassembly(List<File> fileParts, File destination);  }