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.
The reason you're getting an exception is due to this line:
A narrowing conversion from a
long
to anint
may result in a change in sign, because higher order bits are discarded. See JLS Chapter 5, section 5.1.3. The relevant quote:You can test this with a simple program:
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:
Well, this is a quite dangerous way to read files.
int
in Java is 32-bit int, so it resides in range -2147483648 - 2147483647, whilelong
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.