Java NegativeArraySizeException以字节为单位 [英] Java NegativeArraySizeException in byte

查看:154
本文介绍了Java NegativeArraySizeException以字节为单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用OutputStream发送文件,

I want to send file with OutputStream,

所以我使用 byte [] = new byte [file size]

但我不知道我可以使用的最大尺寸。

but I don't know what the max size is that i can use.

这是我的代码。

 File file = new File(sourceFilePath);
        if (file.isFile()) {
            try {
                DataInputStream diStream = new DataInputStream(new FileInputStream(file));
                long len = (int) file.length();
                if(len>Integer.MAX_VALUE){
                    file_info.setstatu("Error");
                }
                else{
                byte[] fileBytes = new byte[(int) len];
                int read = 0;
                int numRead = 0;
                while (read < fileBytes.length && (numRead = diStream.read(fileBytes, read,
                        fileBytes.length - read)) >= 0) {
                    read = read + numRead;
                }

                fileEvent =new File_object();
                fileEvent.setFileSize(len);
                fileEvent.setFileData(fileBytes);
                fileEvent.setStatus("Success");
                fileEvent.setSender_name(file_info.getSender_name());
                }
            } catch (Exception e) {
                e.printStackTrace();
                fileEvent.setStatus("Error");
                file_info.setstatu("Error");
            }
        } else {
            System.out.println("path specified is not pointing to a file");
            fileEvent.setStatus("Error");
             file_info.setstatu("Error");
        }

预先感谢。

推荐答案

出现异常的原因是由于以下原因:

The reason you're getting an exception is due to this line:

long len = (int) file.length();

long int 可能会导致符号更改,因为高阶位被丢弃了。请参见 JLS第5章,第5.1.3节 。相关引用:

A narrowing conversion from a long to an int may result in a change in sign, because higher order bits are discarded. See JLS Chapter 5, section 5.1.3. The relevant quote:


将带符号整数缩小为整数类型T会简单地丢弃除n个最低位以外的所有位,其中n是表示类型T的位数。除了可能丢失有关数值幅度的信息外,这还可能导致结果值的符号与输入值的符号不同。

A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.

您可以使用一个简单的程序对此进行测试:

You can test this with a simple program:

    long a = Integer.MAX_VALUE + 1;
    long b = (int) a;
    System.out.println(b); # prints -2147483648

无论如何,您都不应该使用字节数组来读取内存中的整个文件,但要解决此问题,请不要将文件长度转换为整数。然后您在下一行的检查将起作用:

You shouldn't be using a byte array to read the entire file in memory anyway, but to fix this, don't cast the file length to an int. Then your check on the next line will work:

long len = file.length();

这篇关于Java NegativeArraySizeException以字节为单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆