filechannel

Reading a GZIP file from a FileChannel (Java NIO)

。_饼干妹妹 提交于 2019-11-29 09:34:25
问题 I need to read/unpack a .gz file given a FileChannel. I've played around with extracting GZIP archives using GZIPInputStream, but this won't take a FileChannel. I don't have access to the original FileInputStream that the FileChannel was taken from. If someone could tell me a good way (or at least any way) of reading GZIP from a FileChannel, I would greatly appreciate it. Adapted from a question on the Sun Oracle forums. 回答1: You could obtain a wrapping InputStream around the FileChannel:

Will FileChannel#write always write the whole buffer?

心不动则不痛 提交于 2019-11-29 05:40:09
(This is related to (or rather the "opposite" of) Would FileChannel.read read less bytes than specified if there's enough data? ) TL;DR : Will this always write the whole buffer... ByteBuffer bytes = ...; fileOutputStream.getChannel().write(bytes); ...or is it necessary to use a loop like this: ByteBuffer bytes = ...; while (bytes.remaining() > 0) { fileOutputStream.getChannel().write(bytes); } ? Due to a comment in another answer , I'd like to ask whether there are any guarantees regarding the behavior of writing a Buffer to a FileChannel by calling FileChannel#write(ByteBuffer) . Just for

Java NIO MappedByteBuffer OutOfMemoryException

血红的双手。 提交于 2019-11-28 22:05:51
I am really in trouble: I want to read HUGE files over several GB using FileChannel s and MappedByteBuffer s - all the documentation I found implies it's rather simple to map a file using the FileChannel.map() method. Of course there is a limit at 2GB as all the Buffer methods use int for position, limit and capacity - but what about the system implied limits below that? In reality, I get lots of problems regarding OutOfMemoryException s! And no documentation at all that really defines the limits! So - how can I map a file that fits into the int-limit safely into one or several

How to get random access to a file on SD-Card by means of API presented for Lollipop?

◇◆丶佛笑我妖孽 提交于 2019-11-28 21:41:50
My application uses Java class RandomAccessFile to read/write bytes to a file on SD card randomly by means of realization of SeekableByteChannel interface. Now I need rewrite it for Android 5.0 with new Lollipop API. I have found the only way to read: InputStream inputStream = getContentResolver().openInputStream(uri); and write: ParcelFileDescriptor pfd = getActivity().getContentResolver().openFileDescriptor(uri, "w"); FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor()); from/to a file in new API. I would like to have an ability to set channel in some random

Can multiple threads see writes on a direct mapped ByteBuffer in Java?

爱⌒轻易说出口 提交于 2019-11-28 05:22:57
I'm working on something that uses ByteBuffers built from memory-mapped files (via FileChannel.map() ) as well as in-memory direct ByteBuffers. I am trying to understand the concurrency and memory model constraints. I have read all of the relevant Javadoc (and source) for things like FileChannel, ByteBuffer, MappedByteBuffer, etc. It seems clear that a particular ByteBuffer (and relevant subclasses) has a bunch of fields and the state is not protected from a memory model point of view. So, you must synchronize when modifying state of a particular ByteBuffer if that buffer is used across

Java NIO MappedByteBuffer OutOfMemoryException

为君一笑 提交于 2019-11-27 14:12:49
问题 I am really in trouble: I want to read HUGE files over several GB using FileChannel s and MappedByteBuffer s - all the documentation I found implies it's rather simple to map a file using the FileChannel.map() method. Of course there is a limit at 2GB as all the Buffer methods use int for position, limit and capacity - but what about the system implied limits below that? In reality, I get lots of problems regarding OutOfMemoryException s! And no documentation at all that really defines the

How to get random access to a file on SD-Card by means of API presented for Lollipop?

折月煮酒 提交于 2019-11-27 14:01:56
问题 My application uses Java class RandomAccessFile to read/write bytes to a file on SD card randomly by means of realization of SeekableByteChannel interface. Now I need rewrite it for Android 5.0 with new Lollipop API. I have found the only way to read: InputStream inputStream = getContentResolver().openInputStream(uri); and write: ParcelFileDescriptor pfd = getActivity().getContentResolver().openFileDescriptor(uri, "w"); FileOutputStream fileOutputStream = new FileOutputStream(pfd

Can multiple threads see writes on a direct mapped ByteBuffer in Java?

醉酒当歌 提交于 2019-11-27 00:55:22
问题 I'm working on something that uses ByteBuffers built from memory-mapped files (via FileChannel.map()) as well as in-memory direct ByteBuffers. I am trying to understand the concurrency and memory model constraints. I have read all of the relevant Javadoc (and source) for things like FileChannel, ByteBuffer, MappedByteBuffer, etc. It seems clear that a particular ByteBuffer (and relevant subclasses) has a bunch of fields and the state is not protected from a memory model point of view. So, you