Android implementation file segmentation and assembly

  • 2020-06-07 05:15:23
  • OfStack

This paper describes the method of Android implementation file segmentation and assembly in the form of an example, mainly for UDP packet segmentation and assembly. To share with you for your reference. The specific methods are as follows:

1 a, when using UDP packet sending files, because of the limitation of UDP packet size 1 file should be placed several UDP inside the packet to send, it is need to split a file into several parts, respectively in several UDP packets inside, at the receiving end, received after these UDP packets, again to assembly file, to get a complete file. The steps are as follows:

1. Relevant variables defined:


//  Files to split 
public static RandomAccessFile raf_split;
//  Files to merge 
public static RandomAccessFile raf_merge;
//  The length of the file 
public static long len;
// Byte An array of 
public static int offset;
public static int os = 5;
public static int size = 1024-os;
public static byte file_data[] = new byte[1024];

2. FileOperClass class implementation file segmentation and assembly operations:


//  The constructor (0- Split the file ,1- Merge files )
public FileOperClass(String file, int x){
   
  //  Split the file 
  if (x == 0){    
    try{
      // RandomAccessFile Open the file read-only 
      raf_split = new RandomAccessFile(file,"r");
      //  Get file size 
      len = raf_split.length();
      //  A few packets are needed 
      pnum = (int) Math.ceil((raf_split.length()*1.0)/(size * 1.0)) + 1;
      //  The last 1 How much data is in a packet 
      pmod = (int) (raf_split.length() - (pnum -2)* size);
      //  Split the file 
      split();
    }
    catch (Exception e){  
 
    }
  }
  //  Merge files 
  else if (x == 1){
    try{
      // RandomAccessFile Open the file as read and write 
      raf_merge = new RandomAccessFile(file,"rw");
      //  Merge files 
      merge();
    }
    catch (Exception e){
 
    }
  }
}

3. Split files:


//  Split the file and send 
public static void split(){
   
  int m1,m2;
   
  p_id = 0;
  offset = 0;
   
  try{
    while (len>0){
      //  Packet type 
      file_data[0] = (byte) 2;
      //  The client ID
      file_data[1] = (byte) MainActivity.cli_id;
      //  The session ID
      file_data[2] = (byte) MainActivity.ses_id;
      //  The number of session packets 
      file_data[3] = (byte) pnum;
      //  The packet ID
      file_data[4] = (byte) p_id;
      // seek
      raf_split.seek(offset);
      //  Read data to file_data
      raf_split.read(file_data, os, size);
      //  Send packet 
      MainActivity.trd_send.set_action(2, file_data);
      len = len - size; 
      p_id = p_id + 1;
      offset = offset + size; 
    }
    //  Record the last 1 The remaining bytes of a packet 
    //  Packet type 
    file_data[0] = (byte) 2;
    //  The client ID
    file_data[1] = (byte) MainActivity.cli_id;
    //  The session ID
    file_data[2] = (byte) MainActivity.ses_id;
    //  The number of session packets 
    file_data[3] = (byte) pnum;
    //  The packet ID
    file_data[4] = (byte) p_id;
    m1 = pmod / 128;
    m2 = pmod % 128;
    file_data[5] = (byte) m1;
    file_data[6] = (byte) m2;
    //  Send packet 
    MainActivity.trd_send.set_action(2, file_data);     
  }
  catch (Exception e){
 
  }
  finally{
    //  Close the file 
    try{
      raf_split.close();
    }
    catch(Exception err){
 
    } 
  }
}

4. Merged documents:


//  Merge files 
public static void merge(){
   
  byte[][] tmp_byte = new byte[MainActivity.mer_pkt_num][1024];
  int i,j;
   
  try{
    for(i=0; i<MainActivity.r_datapacket.size(); i++){        
      //  Determine if the packet is complete 
      if ((MainActivity.r_datapacket.get(i).c_id == MainActivity.mer_cli_id) && (MainActivity.r_datapacket.get(i).ses_id == MainActivity.mer_ses_id))
      {
        //  Read the data feed of the packet byte An array of 
        tmp_byte[MainActivity.r_datapacket.get(i).p_id] = MainActivity.r_datapacket.get(i).b;
      }        
    }
    for (i=0; i<MainActivity.mer_pkt_num-2; i++){
      //  the byte Write the array to the file 
      raf_merge.write(tmp_byte[i], os, size);
    }
    //  The last 1 a byte Write the array to the file 
    raf_merge.write(tmp_byte[MainActivity.mer_pkt_num-1], os, MainActivity.mer_pkt_mod );
  }
  catch(Exception e){
 
  }
  finally{
    //  Close the file 
    try{
      raf_merge.close();
    }
    catch(Exception err){
    } 
  }
}

I believe that this article has a certain reference value for your Android programming.


Related articles: