filechannel

FileChannel#force and buffering

ⅰ亾dé卋堺 提交于 2019-12-10 09:28:22
问题 I would like to make it clear and draw some parallels between FileOutputStream and FileChannel right now. So first of all, it seems like the most efficient way to write file with standart Java io is to use FileOutputStream which is wrapped with BufferedOutputStream . Because it automatically flushes, when the internal buffer is overflowed. It is convenient to be able to do single writes (single bytes, floats, etc.) as well as array ones and to be not worried about speed. The only thing you

Should I close the FileChannel?

試著忘記壹切 提交于 2019-12-05 14:11:34
问题 I came across an issue with one of our utility classes today. It is a helper for files and contains some static file copy routines. Below are the relevant methods extracted along with a test method. The problem is that sometimes the setLastModified call fails, returning false. On my PC (Windows 7, latest Java) I sometimes get the "setLastModified failed" message (About 25 times out of 1000). I have worked around the problem right now by removing the FileChannel.close calls but I would much

Reading an ASCII file with FileChannel and ByteArrays

给你一囗甜甜゛ 提交于 2019-12-05 10:16:04
I have the following code: String inputFile = "somefile.txt"; FileInputStream in = new FileInputStream(inputFile); FileChannel ch = in.getChannel(); ByteBuffer buf = ByteBuffer.allocateDirect(BUFSIZE); // BUFSIZE = 256 /* read the file into a buffer, 256 bytes at a time */ int rd; while ( (rd = ch.read( buf )) != -1 ) { buf.rewind(); for ( int i = 0; i < rd/2; i++ ) { /* print each character */ System.out.print(buf.getChar()); } buf.clear(); } But the characters get displayed at ?'s. Does this have something to do with Java using Unicode characters? How do I correct this? You have to know what

java filechannel cpu usage growing with time

笑着哭i 提交于 2019-12-02 14:31:05
问题 I am using java nio filechannel transferFrom function along with Apache httpclient to download files from internet. Its starts normally but cpu usage suddenly grows after some time. And download speed decreases and eventually becomes zero. try (CloseableHttpResponse response = client.execute(get); ReadableByteChannel inputChannel = Channels.newChannel( response.getEntity().getContent())) { while (start < end && currentState.get() == 1) { delta = fileChannel.transferFrom(inputChannel, start,

Java NIO scan through ByteBuffer for certain bytes and word with sections

我只是一个虾纸丫 提交于 2019-12-02 11:19:32
问题 Okay, so I'm trying to do something that seemed like it should be fairly simple, but with these new NIO interfaces, things are confusing the hell out of me! Here's what I'm trying to do, I need to scan through a file as bytes until encountering certain bytes! When I encounter those certain bytes, need to grab that segment of the data and do something with it, and then move on and do this again. I would have thought that with all these markers and positions and limits in ByteBuffer, I'd be

Java NIO scan through ByteBuffer for certain bytes and word with sections

こ雲淡風輕ζ 提交于 2019-12-02 05:21:21
Okay, so I'm trying to do something that seemed like it should be fairly simple, but with these new NIO interfaces, things are confusing the hell out of me! Here's what I'm trying to do, I need to scan through a file as bytes until encountering certain bytes! When I encounter those certain bytes, need to grab that segment of the data and do something with it, and then move on and do this again. I would have thought that with all these markers and positions and limits in ByteBuffer, I'd be able to do this, but I can't seem make it work! Here's what I have so far.. test.text: this is a line of

Using mp4parser , how can I handle videos that are taken from Uri and ContentResolver?

荒凉一梦 提交于 2019-12-01 02:32:41
Background We want to let the user choose a video from any app, and then trim a video to be of max of 5 seconds. The problem For getting a Uri to be selected, we got it working fine (solution available here ) . As for the trimming itself, we couldn't find any good library that has permissive license, except for one called "k4l-video-trimmer" . The library "FFmpeg", for example, is considered not permission as it uses GPLv3, which requires the app that uses it to also be open sourced. Besides, as I've read, it takes quite a lot (about 9MB). Sadly, this library (k4l-video-trimmer) is very old

Using FileChannel to write any InputStream?

不问归期 提交于 2019-11-30 12:06:56
Can I write any InputStream into a FileChannel? I'm using java.nio.channels.FileChannel to open a file and lock it, then writing a InputStream to the output file. The InputStream may be opened by another file, URL, socket, or anything. I've write the following codes: FileOutputStream outputStream = new FileOutputStream(outputFile); FileChannel outputChannel = outputStream.getChannel(); FileLock lock = outputChannel.lock(); try { outputChannel.transferFrom(???); } finally { lock.release(); outputChannel.close(); outputStream.close(); } However, the first argument of outputChannel.transferFrom(.

Will FileChannel#write always write the whole buffer?

随声附和 提交于 2019-11-30 07:55:13
问题 (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

Reading a GZIP file from a FileChannel (Java NIO)

我的未来我决定 提交于 2019-11-30 07:22:22
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 . You could obtain a wrapping InputStream around the FileChannel: FileChannel fc = ... GZIPInputStream gis = new GZIPInputStream(Channels.newInputStream(fc)); Channels is in