Read file segment in Java / Android

I'm sure this may be a simple question, but unfortunately this is my first experience using Java and working with the Android SDK.

I upload files to Android using Apache HTTP libraries, in particular using MultipartEntity.

I upload to a service that allows me to send them chunks of a file, and as soon as they finish, they collect chunks. I would like to take this opportunity.

Here is the script.

The FOO.BAR file is 20 MB. I would divide it into several arbitrary sizes, say 1 MB, which means 20 pieces. Failure No. 3 and No. 14 (possibly cellular / WiFi was bad). Now I can reload only these two pieces, and everything will be fine.

I would like to know how can I read only part of the file (for example, data between 3 MB and 4 MB)?

The file must be an InputStream or File object.

Thanks Makoto

+3
source share
3 answers

You can use the skip (long) method to skip the number of bytes in an InputStream or create a RandomAccessFile on a File object and call seek (long) to set a pointer to this position so you can start reading from there.

A quick test is given in the 4mb + file (between 3m and 4mb) and writes read data to the ".out" .

 import java.io.*; import java.util.*; public class Test { public static void main(String[] args) throws Throwable { long threeMb = 1024 * 1024 * 3; File assembled = new File(args[0]); // your downloaded and assembled file RandomAccessFile raf = new RandomAccessFile(assembled, "r"); // read raf.seek(threeMb); // set the file pointer to 3mb int bytesRead = 0; int totalRead = 0; int bytesToRead = 1024 * 1024; // 1MB (between 3M and 4M File f = new File(args[0] + ".out"); FileOutputStream out = new FileOutputStream(f); byte[] buffer = new byte[1024 * 128]; // 128k buffer while(totalRead < bytesToRead) { // go on reading while total bytes read is // less than 1mb bytesRead = raf.read(buffer); totalRead += bytesRead; out.write(buffer, 0, bytesRead); System.out.println((totalRead / 1024)); } } } 
+2
source

I was able to figure this out ... I just needed to find out that there is a ByteArrayInputStream that will allow me to convert the byte [] buffer to an InputStream. Now I can track which pieces failed and handle this. Thanks to Konstantin for my implementation:

  final int chunkSize = 512 * 1024; // 512 kB final long pieces = file.length() / chunkSize; int chunkId = 0; HttpPost request = new HttpPost(endpoint); BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file)); for (chunkId = 0; chunkId < pieces; chunkId++) { byte[] buffer = new byte[chunkSize]; stream.skip(chunkId * chunkSize); stream.read(buffer); MultipartEntity entity = new MultipartEntity(); entity.addPart("chunk_id", new StringBody(String.valueOf(chunkId))); request.setEntity(entity); ByteArrayInputStream arrayStream = new ByteArrayInputStream(buffer); entity.addPart("file_data", new InputStreamBody(arrayStream, filename)); HttpClient client = app.getHttpClient(); client.execute(request); } 
+1
source

Use the skip () method of the FileInputStream stream to find the desired fragment.

0
source

All Articles