How to get FileChannel from resource

Ulf Zibis Ulf.Zibis at gmx.de
Mon Mar 2 14:28:21 UTC 2009


Am 02.03.2009 14:18, Alan Bateman schrieb:
> Ulf Zibis wrote:
>> Sherman, yes, you are right. That's not what I'm looking for.
>>
>> This would be slower, than reading the stream directly, because the 
>> underlying byte array will be copied 2 times by 
>> BufferedInputStream#<init> + Channels.ReadableByteChannelImpl#read(),
>> ... but maybe not, because looping BufferedInputStream#readChar() 
>> could be slower than twice System.arraycopy().
>>
>> Anyway, I'm looking for a DirectBuffer directly from a resource file.
>> So I think there is something missing:
>> URL.openChannel() or Class#getResourceAsChannel()
>>
>> Maybe there is a way to get a RamdomAccessFile from URL to get the 
>> Channel from. Anybody knows ?
> A URL just identifies an abstract resource so it may not support 
> random access or other file-like operations you require. Have you 
> thought about special handling for when the scheme is "file"? That way 
> you can get the FileChannel you want for the typical case.
>
> -Alan

I thought about something like:
FileChannel fc = new 
RandomAccessFile(getClass().getResource(name).toURI().getPath(), 
"r").getChannel();

... but this doesn't work, if resource is packed in jar file.

-Ulf





More information about the core-libs-dev mailing list