SoFunction
Updated on 2025-03-11

Android implements file segmentation and assembly

This article describes the method of Android's implementation of file segmentation and assembly in an example form, mainly for UDP packet segmentation and assembly. Share it for your reference. The specific methods are as follows:

Generally speaking, when sending files using UDP packets, due to the limitation of UDP packet size, a file must be sent in several UDP packets. This requires dividing a file into several parts and putting it into several UDP packets. At the receiving end, after receiving these UDP packets, the file is assembled to obtain a complete file. The steps are as follows:

1. Defined related variables:

// File to be splitpublic static RandomAccessFile raf_split;
// Files to be mergedpublic static RandomAccessFile raf_merge;
// File lengthpublic static long len;
// Byte arraypublic 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 implements file segmentation and assembly operations:

// Constructor (0-segment file, 1-merge file)public FileOperClass(String file, int x){
   
  // Split the file  if (x == 0){    
    try{
      // RandomAccessFile opens the file in read-only      raf_split = new RandomAccessFile(file,"r");
      // Get file size      len = raf_split.length();
      // Several data packets are required      pnum = (int) ((raf_split.length()*1.0)/(size * 1.0)) + 1;
      // How much data is in the last packet      pmod = (int) (raf_split.length() - (pnum -2)* size);
      // Split the file      split();
    }
    catch (Exception e){  
 
    }
  }
  // Merge files  else if (x == 1){
    try{
      // RandomAccessFile opens the file by reading and writing      raf_merge = new RandomAccessFile(file,"rw");
      // Merge files      merge();
    }
    catch (Exception e){
 
    }
  }
}

3. Split files:

// Split the file and send itpublic static void split(){
   
  int m1,m2;
   
  p_id = 0;
  offset = 0;
   
  try{
    while (len>0){
      // Packet type      file_data[0] = (byte) 2;
      // Client ID      file_data[1] = (byte) MainActivity.cli_id;
      // Session ID      file_data[2] = (byte) MainActivity.ses_id;
      // The number of packets for this session      file_data[3] = (byte) pnum;
      // Packet ID      file_data[4] = (byte) p_id;
      // seek
      raf_split.seek(offset);
      // Read out the data to file_data      raf_split.read(file_data, os, size);
      // Send data packets      MainActivity.trd_send.set_action(2, file_data);
      len = len - size; 
      p_id = p_id + 1;
      offset = offset + size; 
    }
    // The packet that records the remaining bytes of the last packet    // Packet type    file_data[0] = (byte) 2;
    // Client ID    file_data[1] = (byte) MainActivity.cli_id;
    // Session ID    file_data[2] = (byte) MainActivity.ses_id;
    // The number of packets for this session    file_data[3] = (byte) pnum;
    // 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 data packets    MainActivity.trd_send.set_action(2, file_data);     
  }
  catch (Exception e){
 
  }
  finally{
    // Close the file    try{
      raf_split.close();
    }
    catch(Exception err){
 
    } 
  }
}

4. Merge files:

// Merge filespublic 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 whether the data packets are 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 out the data packet into the byte array        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++){
      // Write the byte array into the file      raf_merge.write(tmp_byte[i], os, size);
    }
    // Write the last byte array into 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 the description in this article has certain reference value for everyone's Android programming design.