filechannel

Getting a wrong FileChannel size

假装没事ソ 提交于 2019-12-20 02:14:06
问题 I'm trying to get the size of a file contained in assets . I'm using a FileChannel because I need a FileChannel later. The file myfile.txt contains 7 bytes. Here is my code: AssetManager amgr; AssetFileDescriptor afd; FileChannel fchIn; FileInputStream fis; amgr=context.getAssets(); afd=amgr.openFd("myfile.txt"); fis=afd.createInputStream(); fchIn=fis.getChannel(); Log.d("mytag", fchIn.size()); Log.d("mytag", fis.available()); And the output is: 7237492 7 Why is the size returned by the

File Channel reads/adds wrong data

匆匆过客 提交于 2019-12-20 01:39:24
问题 I am using a filechannel with a byte buffer to send packets over the network. My problem is that when the filechannel reads the last few bytes it appends the last bit of data from previous bytes read even though I am clearing the byte buffer after I write. For example, Byte Buffer size = 512 For the last iteration, the remaining bytes to send is 372. It reads the last 372 but it also appends another 140 bytes (512-372) to the end of it, and appears that last 140 bytes is from the previous 512

Prevent OutOfMemory when using java.nio.MappedByteBuffer

自古美人都是妖i 提交于 2019-12-18 06:39:10
问题 Consider application, which create 5-6 threads, each thread in cycle allocate MappedByteBuffer for 5mb page size. MappedByteBuffer b = ch.map(FileChannel.MapMode.READ_ONLY, r, 1024*1024*5); Sooner or later, when application works with big files, oom is thrown java.io.IOException: Map failed at sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:758) Caused by: java.lang.OutOfMemoryError: Map failed at sun.nio.ch.FileChannelImpl.map0(Native Method) at sun.nio.ch.FileChannelImpl.map

Remove first line from delimited file

人盡茶涼 提交于 2019-12-13 02:28:11
问题 I have a delimited file which can contain around millions of records , now I want to delete the first line from the delimited file before processing it further. The length of the first line is variable , it differs as per the file type.. now I have done a readup on the FileChannel and RandomAccessFile which have been suggested as the best ways to delete the first line. But I am unable to figure it out , as to how to get the length of the first line and delete it. 回答1: Don't delete it, just

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

你离开我真会死。 提交于 2019-12-12 07:25:42
问题 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,

Incomplete File Copy Java NIO

独自空忆成欢 提交于 2019-12-12 03:28:39
问题 I'm having problem with reading my file. Im quite new to NIO too. The actual size of the file I want to send to the server is almost 900MB and only received 3MB . The server's side code for reading: private void read(SelectionKey key) throws IOException{ SocketChannel socket = (SocketChannel)key.channel(); RandomAccessFile aFile = null; ByteBuffer buffer = ByteBuffer.allocate(300000000); try{ aFile = new RandomAccessFile("D:/test2/test.rar","rw"); FileChannel inChannel = aFile.getChannel();

How to tail a file using a NIO selector, in other words, as lines are added to the file a channel is selected so you can read the lines?

笑着哭i 提交于 2019-12-12 03:21:55
问题 Because you cannot redirect GC logs I am left with the option to redirect it to a file with -Xloggc and then get the contents of this file inside my selector through a file channel of some kind. Basically as lines are being added to my file, the selector is being triggered to read them. That way I can get the GC logs programmatically. Is it possible to do that using NIO? 回答1: Given GC logs are buffered, I wouldn't worry about a bit of latency. You can just poll the file length periodically

FileChannel transferFrom's comments explanation

本秂侑毒 提交于 2019-12-11 09:28:44
问题 I've read the comment on FileChannel 's transferFrom * <p> This method is potentially much more efficient than a simple loop * that reads from the source channel and writes to this channel. Many * operating systems can transfer bytes directly from the source channel * into the filesystem cache without actually copying them. </p> What does it mean by Many operating systems can transfer bytes directly from the source channel into the filesystem cache without actually copying them. If I read

What is the use of CompletionHandler in AsynchronousFileChannel for reading data?

江枫思渺然 提交于 2019-12-10 12:09:33
问题 I am working with the AsynchronousFileChannel for reading on the data. For reading the data, i found two read methods as follows: //1. Future<Integer> java.nio.channels.AsynchronousFileChannel.read(ByteBuffer dst, long position); //2. void java.nio.channels.AsynchronousFileChannel.read(ByteBuffer dst, long position, A attachment, CompletionHandler<Integer, ? super A> handler) As the java documentation specified as below, there is no information about the CompletionHandler being used as the

Java Rolling File Creation Fails when attempting to read simultaneously

僤鯓⒐⒋嵵緔 提交于 2019-12-10 11:47:44
问题 I am using java.util logging classes to create a rolling file appender. I want to create a log reader that reads from these logs as data is written to them. The rolling log appender code works fine on its own. But once I start the reader thread new files are not created i.e. if rolling log appender is set to use 5 files it will create 1og.0, log.1, log.2 and so on but if the reader thread starts then it will only create log.0 no other files are created. I notice this in both java logging and