Questions tagged [filechannel]

In Java FileChannel is an abstract class for reading, writing, mapping, and manipulating a file

In Java FileChannel is an abstract class for reading, writing, mapping, and manipulating a file.

A file channel is a SeekableByteChannel that is connected to a file.

It has a current position within its file which can be both queried and modified.

The file itself contains a variable-length sequence of bytes that can be read and written and whose current size can be queried.

The size of the file increases when bytes are written beyond its current size; the size of the file decreases when it is truncated.

The file may also have some associated metadata such as access permissions, content type, and last-modification time; this class does not define methods for metadata access.

More info

193 questions
27
votes
3 answers

Is there a way to prevent ClosedByInterruptException?

In the following example, I have one file being used by two threads (in the real example I could have any number of threads) import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import…
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
26
votes
7 answers

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

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…
Alex Miller
  • 69,183
  • 25
  • 122
  • 167
19
votes
7 answers

Java NIO: transferFrom until end of stream

I'm playing around with the NIO library. I'm attempting to listen for a connection on port 8888 and once a connection is accepted, dump everything from that channel to somefile. I know how to do it with ByteBuffers, but I'd like to get it working…
aioobe
  • 413,195
  • 112
  • 811
  • 826
19
votes
2 answers

Concurrency of RandomAccessFile in Java

I am creating a RandomAccessFile object to write to a file (on SSD) by multiple threads. Each thread tries to write a direct byte buffer at a specific position within the file and I ensure that the position at which a thread writes won't overlap…
user1715122
  • 947
  • 1
  • 11
  • 26
17
votes
1 answer

Will FileChannel#write always write the whole buffer?

(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 =…
Marco13
  • 53,703
  • 9
  • 80
  • 159
16
votes
2 answers

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

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…
android developer
  • 114,585
  • 152
  • 739
  • 1,270
14
votes
2 answers

Using FileChannel to write any InputStream?

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…
KiKi
  • 505
  • 1
  • 4
  • 12
13
votes
2 answers

Should I close the FileChannel?

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…
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
11
votes
1 answer

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

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…
10
votes
2 answers

Bufferunderflowexception Java

I am writing values to a file. The values are written correct. In another application I can read the file without any exceptions. But in my new application, I get a Bufferunderflowexception when trying to read the file. The bufferunderflowexception…
Swag
  • 2,090
  • 9
  • 33
  • 63
10
votes
2 answers

Is this an off-by-one bug in Java 7?

I don't know where to seek clarifications and confirmations on Java API documentation and Java code, so I'm doing it here. In the API documentation for FileChannel, I'm finding off-by-one errors w.r.t. to file position and file size in more places…
Harry
  • 3,684
  • 6
  • 39
  • 48
9
votes
2 answers

Reading a GZIP file from a FileChannel (Java NIO)

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…
8
votes
1 answer

FileChannel#force and buffering

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…
Alexander Shukaev
  • 16,674
  • 8
  • 70
  • 85
7
votes
0 answers

How do memory-mapped files work in a multi-process scenario?

This question has been confusing me for days: Assume that I have two processes (p_write and p_read) which run on the same machine. Process p_write is for writing/updating the mmap file. Process p_read is for consuming the mmap file, or in other…
Lubor
  • 989
  • 3
  • 10
  • 33
7
votes
2 answers

In-memory version of Java's FileChannel

I'm in the process of making some changes to a library that I'm using. In order to reduce memory usage the library is writing its temporary data to disk instead of keeping it in memory. However, for my usage scenario it is more efficient to keep it…
Yrlec
  • 3,401
  • 6
  • 39
  • 75
1
2 3
12 13