user3811952
user3811952

Reputation: 25

how do i count byteCount in read method of InputStream

i want to download a file through InputStream from some offSet, i know that i can do this by read method of InputStream but it requires a parameter byteCount but i don't know how to calculate byteCount for read Method

My Code is

         try {
                URL url;
                url = new URL(uri);
                HttpURLConnection c = (HttpURLConnection) url
                        .openConnection();
                c.setConnectTimeout(1000000);
                c.connect();
                int lenghtOfFile = c.getContentLength();
                Log.i("fileSize", lenghtOfFile + "");
                File file = new File(
                        Environment.getExternalStorageDirectory() + "/"
                                + Environment.DIRECTORY_DOWNLOADS);
                file.mkdirs();
                File outputFile = new File(file, "4k1.jpg");
                FileOutputStream fos = new FileOutputStream(outputFile);
                Log.v("Place", "trying...");
                InputStream is = c.getInputStream();
                Log.v("errorPart", c.getURL().toString());
                if (c.getURL().toString().contains("404"))
                    Log.v("Error", "404 Error");
                Log.v("Place", "1st attempt success");
                byte[] buffer = new byte[1024];
                int len1 = 0;
                Log.v("Place", "Download started!");
                int on = 0;
                    while ((len1 = is.read(buffer)) != -1) {
                        fos.write(buffer, 0, len1);
                        Log.v("is", "running "  + len1);
                    }
                Log.v("Place", "Download Finished!");
                fos.close();
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }    

Please try to explain your answer, i am newbiee

Upvotes: 1

Views: 2653

Answers (4)

千木郷
千木郷

Reputation: 1807

Why not directly use the class, CountingInputStream, provided by Apache. This class has been perfectly designed to match your requirment.

Upvotes: 1

waqaslam
waqaslam

Reputation: 68177

You don't really need to provide byteCount for read method. Using is.read(buffer)) will simply read out 1024 bytes (since defined by the length of your buffer array) at every round till -1 is returned.

Upvotes: 0

user3812027
user3812027

Reputation: 1

There is no direct access to the file size from HttpURLConnection You can use the nio cache under your file stream In a one-time generated file.

Upvotes: 0

Evil Dog Pie
Evil Dog Pie

Reputation: 2320

You need to tell it how many bytes it can read. This is to prevent the read() method copy data over memory that it shouldn't. You should therefore pass the size of the buffer. In the code above this will be 1024. You may want to use private static int BUFFER_SIZE = 1024 and use this constant in both the new and read() so that if you change the size of the buffer you don't forget to change the parameter to the read() function as well.

private static final int BUFFER_SIZE = 1024;

...

byte[] buffer = new byte[BUFFER_SIZE];
int len1 = 0;
Log.v("Place", "Download started!");
int on = 0;
while ((len1 = is.read(buffer,0,BUFFER_SIZE)) != -1) {
    fos.write(buffer, 0, len1);
    Log.v("is", "running "  + len1);
}

...

Upvotes: 2

Related Questions