Vaciar un dispositivo char de java al inicio de la aplicación

my application reads from a char device, which is filled from kernel space. Currently I'm accessing it with a RandomAccessFile which is working fine.

My Problem is, before I start my application, data is already written to the char device. (There is constant data at a minimum of 1 per second)

So, I want to "ignore" the data which was inserted before I started my application.

The problem is, I cannot tell, how much data is currently in this char device. It is like reading from a pipe, I tried some Reader classes but accessing the lenght always thorws an Illegal seek Exception.

I think there is a way to literally "empty up" the char device before I start reading data from it, maybe someone can point me in the right direction. I never worked with non blocking io, like the java.nio. stuff, is this the way to go?

Unfortunatly if have no code ready for posting, if you need some, let me know.

preguntado el 08 de noviembre de 11 a las 12:11

2 Respuestas

OK, I found no beautiful way to solve that problem.

The only solution I found is skipping it manually checking System.nanoTime (Scala Code!):

var run = true
val test = new Array[Byte](10000); //<- needs to be that big!
var count = 0;
device = new RandomAccessFile("/dev/mydevice", "r")
while (run) {
  val time = System.nanoTime();
  count = count +1
  if ((System.nanoTime() - time)>100000000) {
    run = false
println("Skipped ["+count+"] lines")

If anyone can bring up a better solution, I appreciate that.

respondido 08 nov., 11:23

i assume you already tried RandomAccessFile.setLength(0) (although i doubt that will work, that's not really how pipes are used)? What about using a FileInputStream and calling skip() until you can't skip any further?

respondido 08 nov., 11:16

Hi, no I didnt try that. As I read in the documentation of setLength, it tries to access length. Whenever length is accessed, I get an Illegal seek Exception. I tried using FileInputStream, but ran into the same problems with this Illegal Seek. I check if skip works for me. - mal muerto

Hi again, neither setLength nor skip works. I get always Illegal seek IOException. - mal muerto

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.