Advertisement
Guest User

Untitled

a guest
Aug 27th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1.  
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.RandomAccessFile;
  7.  
  8. /*
  9.  * To change this license header, choose License Headers in Project Properties.
  10.  * To change this template file, choose Tools | Templates
  11.  * and open the template in the editor.
  12.  */
  13.  
  14. /**
  15.  *
  16.  * @author ram
  17.  */
  18. public class split
  19. {
  20.    
  21.     public static void main(String[] args) throws Exception
  22.     {      
  23.         String fileName = "tifName.tif";
  24.         String fil = "name";
  25.         String type = ".tif";
  26.        
  27.         File tifFile = new File("C:\\Users\\s\\folder\\car_folder\\" + fileName);
  28.         RandomAccessFile raf = new RandomAccessFile(tifFile, "r");
  29.         long numSplits = 2; //from user input, extract it from args
  30.         long sourceSize = raf.length();
  31.         long bytesPerSplit = sourceSize/numSplits ;
  32.         long remainingBytes = sourceSize % numSplits;
  33.  
  34.         long maxReadBufferSize = 1 * 1610612736; //1500mb
  35.         for(int destIx = 1; destIx <= numSplits; destIx++) {
  36.             BufferedOutputStream bw = new BufferedOutputStream(new FileOutputStream(fil + "_" + destIx + type));
  37.             if(bytesPerSplit > maxReadBufferSize) {
  38.                 long numReads = bytesPerSplit/maxReadBufferSize;
  39.                 long numRemainingRead = bytesPerSplit % maxReadBufferSize;
  40.                 for(int i=0; i<numReads; i++) {
  41.                     readWrite(raf, bw, maxReadBufferSize);
  42.                 }
  43.                 if(numRemainingRead > 0) {
  44.                     readWrite(raf, bw, numRemainingRead);
  45.                 }
  46.             }
  47.             else {
  48.                 readWrite(raf, bw, bytesPerSplit);
  49.             }
  50.             bw.close();
  51.         }
  52.         if(remainingBytes > 0) {
  53.             BufferedOutputStream bw = new BufferedOutputStream(new FileOutputStream("split." + numSplits+1));
  54.             readWrite(raf, bw, remainingBytes);
  55.             bw.close();
  56.         }
  57.             raf.close();
  58.     }
  59.  
  60.     static void readWrite(RandomAccessFile raf, BufferedOutputStream bw, long numBytes) throws IOException {
  61.         byte[] buf = new byte[(int) numBytes];
  62.         int val = raf.read(buf);
  63.         if(val != -1) {
  64.             bw.write(buf);
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement