2 * Copyright (C) The Arvados Authors. All rights reserved.
4 * SPDX-License-Identifier: AGPL-3.0 OR Apache-2.0
8 package org.arvados.client.utils;
10 import org.apache.commons.io.FileUtils;
13 import java.util.ArrayList;
14 import java.util.List;
18 * {@link} https://stackoverflow.com/questions/10864317/how-to-break-a-file-into-pieces-using-java
20 public class FileSplit {
22 public static List<File> split(File f, File dir, int splitSize) throws IOException {
25 long sizeOfFiles = splitSize * FileUtils.ONE_MB;
26 byte[] buffer = new byte[(int) sizeOfFiles];
28 List<File> files = new ArrayList<>();
29 String fileName = f.getName();
31 try (FileInputStream fis = new FileInputStream(f); BufferedInputStream bis = new BufferedInputStream(fis)) {
33 while ((bytesAmount = bis.read(buffer)) > 0) {
34 String filePartName = String.format("%s.%03d", fileName, partCounter++);
35 File newFile = new File(dir, filePartName);
36 try (FileOutputStream out = new FileOutputStream(newFile)) {
37 out.write(buffer, 0, bytesAmount);