In this paper, we describe the method of segmentation and assembly of the Android file by example, which is mainly for the segmentation and assembly of UDP packets. Share for everyone to use for reference. The specific methods are as follows:
In general, when you send a file by using a UDP packet, due to the size of UDP packets, a file to be placed in a few UDP packets sent, which requires a file to split into several parts, into a number of UDP packets, on the receiving side, after receiving these UDP packets, The file is then assembled to obtain a complete file. The steps are as follows:
The related variables defined:
The file to be split public
static Randomaccessfile Raf_split;
The files to be merged public
static Randomaccessfile Raf_merge;
File length public
static long Len;
byte array public
static int offset;
public static int os = 5;
public static int size = 1024-os;
public static byte file_data[] = new byte[1024];
Second, the Fileoperclass class implementation of file segmentation and assembly operations:
Constructor (0-Split file, 1-merged file) public
fileoperclass (String file, int x) {
//split file
if (x = = 0) {
try{
// Randomaccessfile opens the file in read-only mode
raf_split = new Randomaccessfile (file, "R");
Get file size
len = Raf_split.length ();
Several packets are required
pnum = (int) Math.ceil (raf_split.length () *1.0)/(Size * 1.0)) + 1;
The data in the last packet how many
pmod = (int) (Raf_split.length ()-(pnum-2) * size);
Split file
split ();
}
catch (Exception e) {
}
}//
merge file
else if (x = = 1) {
try{
//randomaccessfile Open File read-write
raf_merge = new Randomaccessfile (file, "RW");
Merging file merge
();
}
catch (Exception e) {
}
}
}
Third, the split file:
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;
Client ID file_data[1] = (byte) mainactivity.cli_id;
Session ID File_data[2] = (byte) mainactivity.ses_id;
The number of Session packets file_data[3] = (byte) pnum;
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;
The packet//packet type file_data[0] = (byte) 2 for the remaining bytes of the last packet;
Client ID file_data[1] = (byte) mainactivity.cli_id;
Session ID File_data[2] = (byte) mainactivity.ses_id;
The number of Session packets 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 Packet MainActivity.trd_send.set_action (2, File_data);
catch (Exception e) {} finally{//Close file try{raf_split.close ();
' Catch (Exception err) {}}}
Iv. Consolidation of documents:
Merge file 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 of the 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 byte array to file
raf_merge.write (tmp_byte[i], OS, size);
//The last byte array is written into
the file Raf_merge.write (tmp_byte[mainactivity.mer_pkt_num-1], OS, mainactivity.mer_pkt_mod);
catch (Exception e) {
}
finally{
//close file
try{
raf_merge.close ();
}
catch (Exception err) {
}
}
}
It is believed that this article has some reference value to everyone's Android program design.