s_puria
s_puria

Reputation: 407

AsyncTask fatal exception #1

I have an asynctask which downloads a text file to the local file using channel.

class getUrlsClass extends AsyncTask<String, Integer, File>{

    @Override
    protected File doInBackground(String... params) {
        URLConnection uc;
        File urlFiles= new File(getApplicationContext().getFilesDir(), "urls");
        try {
            URL url = new URL(params[0]);
            uc = url.openConnection();
            uc.setUseCaches(false);
            ReadableByteChannel rbc = Channels.newChannel(uc.getInputStream());
            FileOutputStream fos = new FileOutputStream(urlFiles);
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            fos.close();


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}

and I get a fatal runtime exception which makes the program crash at start:

 07-22 01:31:01.769: E/AndroidRuntime(27610): FATAL EXCEPTION: AsyncTask #1
07-22 01:31:01.769: E/AndroidRuntime(27610): Process: com.example.griding, PID: 27610
07-22 01:31:01.769: E/AndroidRuntime(27610): java.lang.RuntimeException: An error occured while executing doInBackground()
07-22 01:31:01.769: E/AndroidRuntime(27610):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
07-22 01:31:01.769: E/AndroidRuntime(27610):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
07-22 01:31:01.769: E/AndroidRuntime(27610):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
07-22 01:31:01.769: E/AndroidRuntime(27610):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-22 01:31:01.769: E/AndroidRuntime(27610):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-22 01:31:01.769: E/AndroidRuntime(27610):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-22 01:31:01.769: E/AndroidRuntime(27610):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-22 01:31:01.769: E/AndroidRuntime(27610):    at java.lang.Thread.run(Thread.java:841)
07-22 01:31:01.769: E/AndroidRuntime(27610): Caused by: java.lang.IllegalArgumentException: position=0 count=9223372036854775807
07-22 01:31:01.769: E/AndroidRuntime(27610):    at java.nio.FileChannelImpl.transferFrom(FileChannelImpl.java:368)
07-22 01:31:01.769: E/AndroidRuntime(27610):    at com.example.myApp.DownloadManagerActivity$getUrlsClass.doInBackground(DownloadManagerActivity.java:284)
07-22 01:31:01.769: E/AndroidRuntime(27610):    at com.example.myApp.DownloadManagerActivity$getUrlsClass.doInBackground(DownloadManagerActivity.java:1)
07-22 01:31:01.769: E/AndroidRuntime(27610):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-22 01:31:01.769: E/AndroidRuntime(27610):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-22 01:31:01.769: E/AndroidRuntime(27610):    ... 4 more

What is the problem with the task?

Upvotes: 2

Views: 425

Answers (1)

Awanish Raj
Awanish Raj

Reputation: 2009

The exception is caused at:

fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

because the parameter count cannot be greater than Integer.MAX_VALUE. This is causing this particular exception!

Found this in the FileChannelImpl class file:

    if (position < 0 || count < 0 || count > Integer.MAX_VALUE) {
        throw new IllegalArgumentException("position=" + position + " count=" + count);
    }

Hope it helps! :)

Upvotes: 2

Related Questions