package name.tuliopa.files.examples;


/**
 *
 * @author tuliopa
 */

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileSplitterv1 {


    private static byte[] convertFileToBytes(String location) throws IOException {
        RandomAccessFile f = new RandomAccessFile(location, "r");
        byte[] b = new byte[(int)f.length()];
        f.readFully(b);
        f.close();
        return b;
    }
    
    private static void writeBytes(String name, byte[] buffer) throws IOException {
        BufferedOutputStream bw = new BufferedOutputStream(new FileOutputStream(name));
        bw.write(buffer);
        bw.close();
    }
    
    /**
     * 
     * @param fileName name of file to be splited.
     * @param mBperSplit number of MB per file.
     * @return Return a list of files.
     * @throws IOException
     */
    public static List splitFile(String fileName, int mBperSplit) throws IOException {
        
    	if(mBperSplit <= 0) {
    		throw new IllegalArgumentException("mBperSplit must be more than zero");
    	}
    	
        String dir = "/tmp/";
        String suffix = ".splitPart";
        List tempFiles = new ArrayList();
        
        final long sourceSize = new File(fileName).length();

        int bytesPerSplit = 1024 * 1024 * mBperSplit;
        long numSplits = sourceSize / bytesPerSplit;
        long remainingBytes = sourceSize % bytesPerSplit;

      /// Copy arrays
        byte[] originalbytes = convertFileToBytes(fileName);
        int i=0;
        for( ; i < numSplits; i++){
            //write stream to a file.
            String tempName = dir + "part" + i + suffix;
            byte[] b = new byte[bytesPerSplit];
            System.arraycopy(originalbytes, (i * bytesPerSplit), b, 0, bytesPerSplit);
            writeBytes(tempName, b);
            tempFiles.add(tempName);
        }

        if ( remainingBytes > 0 ){
            String tempName = dir + "part" + i + suffix;
            byte[] b = new byte[(int)remainingBytes];
            System.arraycopy(originalbytes, (i * bytesPerSplit), b, 0, (int)remainingBytes);
            writeBytes(tempName, b);
            tempFiles.add(tempName);
        }

        return tempFiles;
    }
}

@@ -298,16 +298,115 @@
 erv1 %7B%0A%0A
+%09private static final String dir = %22/tmp/%22;%0A    private static final String suffix = %22.splitPart%22;%0A
 %0A    pri
@@ -699,39 +699,51 @@
 iteB
-ytes(String name, byte%5B%5D buffer
+ufferToFiles(byte%5B%5D buffer, String fileName
 ) th
@@ -841,17 +841,21 @@
 tStream(
-n
+fileN
 ame));%0A 
@@ -906,24 +906,453 @@
 %0A    %7D%0A    %0A
+    private static void copyBytesToPartFile(byte%5B%5D originalBytes, List partFiles, int partNum, int bytesPerSplit, int bufferSize) throws IOException%7B%0A        String partFileName = dir + %22part%22 + partNum + suffix;%0A        byte%5B%5D b = new byte%5BbufferSize%5D;%0A        System.arraycopy(originalBytes, (partNum * bytesPerSplit), b, 0, bufferSize);%0A        writeBufferToFiles(b, partFileName);%0A        partFiles.add(partFileName);%0A    %7D%0A%0A
     /**%0A    
@@ -1737,21 +1737,16 @@
 %0A    %09%7D%0A
-    %09
 %0A       
@@ -1750,85 +1750,17 @@
     
-String dir = %22/tmp/%22;%0A        String suffix = %22.splitPart%22;%0A        List temp
+List part
 File
@@ -1780,25 +1780,16 @@
 List();%0A
-        %0A
         
@@ -1841,17 +1841,16 @@
 ngth();%0A
-%0A
         
@@ -1956,20 +1956,19 @@
         
-long
+int
  remaini
@@ -1976,16 +1976,22 @@
 gBytes =
+ (int)
  sourceS
@@ -2053,25 +2053,25 @@
 e%5B%5D original
-b
+B
 ytes = conve
@@ -2107,17 +2107,23 @@
     int 
-i
+partNum
 =0;%0A    
@@ -2130,16 +2130,21 @@
     
-for( ; i
+while(partNum
  %3C n
@@ -2155,13 +2155,8 @@
 lits
-; i++
 )%7B%0A 
@@ -2178,159 +2178,61 @@
 ite 
-stream to a file.%0A            String tempName = dir + %22part%22 + i + suffix;%0A            byte%5B%5D b = new byte%5BbytesPerSplit%5D;%0A            System.arraycopy
+bytes to a part file.%0A            copyBytesToPartFile
 (ori
@@ -2232,35 +2232,50 @@
 ile(original
-b
+B
 ytes, 
-(i *
+partFiles, partNum,
  bytesPerSpl
@@ -2276,23 +2276,16 @@
 PerSplit
-), b, 0
 , bytesP
@@ -2310,391 +2310,161 @@
     
-writeBytes(tempName, b);%0A            tempFiles.add(tempName);%0A        %7D%0A%0A        if ( remainingBytes %3E 0 )%7B%0A            String tempName = dir + %22part%22 + i + suffix;%0A            byte%5B%5D b = new byte%5B(int)remainingBytes%5D;%0A            System.arraycopy(originalbytes, (i * bytesPerSplit), b, 0, (int)remainingBytes);%0A            writeBytes(tempName, b);%0A            tempFiles.add(tempName
+++partNum;%0A        %7D%0A%0A        if ( remainingBytes %3E 0 )%7B%0A            copyBytesToPartFile(originalBytes, partFiles, partNum, bytesPerSplit, remainingBytes
 );%0A 
@@ -2492,12 +2492,12 @@
 urn 
-temp
+part
 File

Refactor version 1 to reuse code.