Two issues in Files

Alan Bateman Alan.Bateman at oracle.com
Fri Sep 13 14:15:46 UTC 2013


On 13/09/2013 00:37, Colin Decker wrote:
> :
>
> As for OutputStream.write failing after successfully writing some 
> bytes: that's going to be true regardless of whether you're passing an 
> array to write in slices or all at once, right? It could still fail at 
> any point in that process.
>
> And if some OutputStream implementations do work better (creating less 
> memory overhead) when written in slices of array rather than when 
> given a big array up front, that definitely seems like something that 
> might be good to fix in those implementations, given that they can 
> always implement write(byte[]) to do the looping and writing slices 
> itself. And while Files.write doesn't pass the whole byte array to the 
> OutputStream, users can do that directly. Anyway, it's not that big a 
> deal as the performance difference is probably pretty minimal, it just 
> seemed odd.
>
I've created JDK-8024788 on the issue of Files.readAllBytes using 
FileChannel.

On the OutputStream.write discussion then we would need to move the 
slicing to Channels.newOutputStream, otherwise it would have the poor 
effects that I was mentioned. You are right that someone using the 
OutputStream directly and passing a huge byte[] would do the same thing 
(there are a number of OOME bugs on that). For OutputStream.write itself 
then I think we should look to clarify the javadoc on this point, maybe 
to make it clear that it may throw I/O exception when some (but not all) 
bytes have been written. It could have a warning to say that it could 
leave the stream in an inconsistent state.

-Alan.


More information about the nio-dev mailing list