Java NegativeArraySizeException in byte

后端 未结 2 1782
谎友^
谎友^ 2021-01-24 23:11

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.

Th

相关标签:
2条回答
  • 2021-01-24 23:56

    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.

    0 讨论(0)
  • 2021-01-25 00:00

    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();
    
    0 讨论(0)
提交回复
热议问题