简体   繁体   中英

Java OutputStream.write() throws Bad file descriptor but flush works

I am writing code that uses a file Stream reversible like this

public static void writeBlock(OutputStream stream, float[] data, byte startPosition)
{
   int i = 0;
   for(i = startPosition; i < data.length; i++)
   {
      // Code
      stream.flush();
      stream.write(someByte); // throws Exception at second call
      if(someCondition)
      {
        break;
      }
   }
   stream.flush();
   if(i < data.length)
   {
      writeBlock(stream, data, i)
   }
}

I am getting IOExceptions with the message: Bad file descriptor

The method is getting a FileOutputStream .

But how can the file descriptor be bad if it works the first time. Where is my Problem? Thanks for your help in advance

flush() depends on the implementation of OutputStream being used. The abstract OutputStream class implementation of this method does nothing. If the OutputStream subclass never buffers output, it will most likely not implement this method and flush() will do nothing. Even if flush() does something for the implementation that was passed if there is no buffered output it is very likely to just return immediately.

There is something wrong with the file passed to the OutputStream. Would need more information to figure out what the issue is.

I figured it out by myself

I opened another Stream (BitOutputStream) in the method that writes to stream and closed this bitStream afterwards.

public static void writeBlock(OutputStream stream, float[] data, byte startPosition)
{
  int i = 0;

  //Bitstream bitstream init

  for(i = startPosition; i < data.length; i++)
  {
     // Code
     stream.flush();
     stream.write(someByte); // throws Exception at second call
     if(someCondition)
     {
        break;
     }
  }
  bitstream.close();
  stream.flush();
  if(i < data.length)
  {
     writeBlock(stream, data, i)
  }
}

After I deleted that line ( bistream.close() ) it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM