简体   繁体   English

使用java.nio时抛出IllegalArgumentException

[英]Thrown IllegalArgumentException when using java.nio

Trying to download file by url. 试图通过网址下载文件。 For that using java.nio library. 对于那个使用java.nio库。 The code above work perfect for sdk 24 and above but getting IllegalArgumentException for android sdk 23 and below. 上面的代码适用于sdk 24及更高版本,但是对于android sdk 23及更低版本获取IllegalArgumentException。 I'm getting the error when trying transfer the data. 我在尝试传输数据时遇到错误。 Dear Friends can you please clarify which is the problem. 亲爱的朋友们,请您澄清哪个是问题所在。

    private void downloadFile(final String itemUrl) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                ReadableByteChannel readableByteChannel = null;
                FileOutputStream fOutStream = null;

                String root = 
                getApplicationContext().getApplicationInfo().dataDir;
                File myDir = new File(root + "/downloadedSongs");
                if (!myDir.exists()) {
                myDir.mkdirs();
                }

                File file = new File(myDir, itemTitle);
                if (file.exists()) {
                    file.delete();
                }

                try {
                    URL url = new URL(itemUrl);
                    readableByteChannel = 
                    Channels.newChannel(url.openStream());
                    fOutStream = new FileOutputStream(file);
      fOutStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);

                } catch (IOException e) {
                    orderAction = Enums.OrderAction.Delete;
                } catch (IllegalArgumentException e) {
                    orderAction = Enums.OrderAction.Delete;
                    Crashlytics.logException(e);
                } finally {
                    try {
                        if (fOutStream != null) {
                            fOutStream.close();
                        }
                        if (readableByteChannel != null) {
                           readableByteChannel.close();
                        }
                    } catch (IOException ioExObj) {
                        orderAction = Enums.OrderAction.Delete;
                    }
                    closeNotification(orderAction);
                }
            }
    }).start();
}

在此输入图像描述

Fatal Exception: java.lang.IllegalArgumentException: position=0 count=9223372036854775807 at java.nio.FileChannelImpl.transferFrom(FileChannelImpl.java:370) at am.itsoft.youtomp3.services.DnlService$1.run(DnlService.java:169) at java.lang.Thread.run(Thread.java:818) 致命异常:java.lang.IllegalArgumentException:position = 0 count = 9223372036854775807 at java.nio.FileChannelImpl.transferFrom(FileChannelImpl.java:370)at am.itsoft.youtomp3.services.DnlService $ 1.run(DnlService.java:169)在java.lang.Thread.run(Thread.java:818)

The reason is in this argument: Long.MAX_VALUE 原因在于这个论点:Long.MAX_VALUE

Right code: 正确代码:

FileChannel destChannel = fOutStream.getChannel();
long blockSize;
if (Build.VERSION.SDK_INT > 23) {
  long blockSize = Long.MAX_VALUE;
} else {
  long blockSize = 8*1024;
}
long position = 0;
long loaded;
while ((loaded = destChannel.transferFrom(readableByteChannel, position, blockSize)) > 0) {
   position += loaded;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM