Random volume randomaccessfile-multi-thread download

Source: Internet
Author: User

Random random class randomaccessfile

The input stream fileinputstream and output stream fileoutputstream implement sequential read and write of disk files, and create different objects for read and write respectively.

In contrast, the randomaccessfile class can perform random read/write operations on files.
The file position pointer of the randomaccessfile object follows the following rules:
· The file position pointer of the new randomaccessfile object is located at the beginning of the file;
· After each read/write operation, the pointer to the file location is corresponding to the number of bytes that are moved to read/write;
· You can use the getfilepointer method to obtain the position of the file pointer, and use the seek method to set the position of the file pointer.
If a file contains 30 bytes, you can use the SKIP () // skip method to read data from 20-30, the previous bytes are deleted,

If the user needs to read 10-20 bytes, then read the number between 1-10, and then between 20-30,

Multi-thread download

Public void threaddownload (string path, int threadsize) throws exception {
URL url = new URL (PATH );
Httpurlconnection connection = (httpurlconnection) URL. openconnection ();
Connection. setrequestmethod ("get ");
If (connection. getresponsecode ()! = 200 ){
Throw new runtimeexception ("responsecode! = 200 ");
}
// The total size to download
Int datalength = connection. getcontentlength ();
Connection. Disconnect ();
// Number of threads
Threadsize = 0? 5: threadsize;
Threadsize = datalength % threadsize = 0? Threadsize: threadsize + 1;
// Number of chunks to be downloaded per Thread
Int blocksize = datalength/threadsize;
String newfilename = Rename (PATH );
System. Out. println ("points" + threadsize + "Threads start downloading ");
For (INT I = 0; I <threadsize; I ++ ){
Randomaccessfile accessfile = new randomaccessfile (newfilename, "RW ");
Int startsize = blocksize * I;
Accessfile. Seek (startsize );
Thread thread = new thread (new download (startsize, path, blocksize, accessfile, I + 1 ));
Thread. Start ();
}
}

Defined thread download class

Private class download implements runnable {
Private string path;
Private randomaccessfile accessfile;
Private int startsize;
Private int blocksize;
Private int threadname;
Public download (INT startsize, string path, int blocksize, randomaccessfile accessfile, int threadname ){
This. Path = path;
This. accessfile = accessfile;
This. startsize = startsize;
This. blocksize = blocksize;
This. threadname = threadname;
}
@ Override
Public void run (){
Try {
System. Out. println ("Thread" + threadname + "start to download .....");
URL url = new URL (PATH );
Httpurlconnection connection = (httpurlconnection) URL. openconnection ();
Connection. setrequestmethod ("get ");
Connection. setreadtimeout (6*1000 );
Connection. setrequestproperty ("range", "bytes =" + startsize + "-");
If (connection. getresponsecode ()! = 206 ){
Throw new runtimeexception ("responsecode! = 206 ");
}
Inputstream = connection. getinputstream ();
Inputstream = new bufferedinputstream (inputstream );
Byte [] buffer = new byte [1024];
Int Len =-1;
Int length = 0;
While (length <blocksize & (LEN = inputstream. Read (buffer ))! =-1 )){
Accessfile. Write (buffer, 0, Len );
Length + = Len;
}
Accessfile. Close ();
Inputstream. Close ();
Connection. Disconnect ();
System. Out. println ("Thread" + threadname + "Download successful! ");
} Catch (exception e ){
E. printstacktrace ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.