Java NegativeArraySizeException in byte

佐手、 提交于 2019-12-20 04:15:32

问题


I want to send file with OutputStream,

so I use byte[] = new byte[size of file ]

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

This is my code.

 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");
        }

Thanks in advance.


回答1:


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

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

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:

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();



回答2:


Well, this is a quite dangerous way to read files. int in Java is 32-bit int, so it resides in range -2147483648 - 2147483647, while long is 64 bit.

Generally you shouldn't read the whole file at once because your program may exhaust your RAM on big files. Use a BufferedReader on top of FileReader instead.



来源:https://stackoverflow.com/questions/22800628/java-negativearraysizeexception-in-byte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!