1

I have a code in which I read images on a network drive. i read thousands of images, but only sometimes i get following exception occasionally.

java.io.IOException: An unexpected network error occurred
    at java.base/sun.nio.ch.FileDispatcherImpl.read0(Native Method)
    at java.base/sun.nio.ch.FileDispatcherImpl.read(FileDispatcherImpl.java:54)
    at java.base/sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:276)
    at java.base/sun.nio.ch.IOUtil.read(IOUtil.java:245)
    at java.base/sun.nio.ch.FileChannelImpl.read(FileChannelImpl.java:223)
    at java.base/sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:65)
    at java.base/sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:109)
    at java.base/sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:103)
    at java.base/java.io.InputStream.read(InputStream.java:205)

below is the code for which i get it

`

public static int getEPSSectionOffset(File file) throws Exception {
    int result = 0;
    try (InputStream inputStream =  
      Files.newInputStream(Paths.get(file.getAbsolutePath()),StandardOpenOption.READ);) {
      byte[] fourBytes = new byte[4];
      int totalBytesRead = inputStream.read(fourBytes);
      if (log.isDebugEnabled())
        log.debug("Total bytes read is " + totalBytesRead + " for file " + file.getPath());

      if (fourBytes[0] == (byte) 0xC5 && fourBytes[1] == (byte) 0xD0 && fourBytes[2] == (byte) 0xD3 
      && fourBytes[3] == (byte) 0xC6) {
        totalBytesRead = inputStream.read(fourBytes);
        if (log.isDebugEnabled())
          log.debug("Total bytes read is " + totalBytesRead + " for file " + file.getPath());

        result = makeInt(fourBytes);
      }
      return (result);
    } catch (Exception e) {
      log.error("Get EPS Section Offset - " + e.getMessage(), e);
    }
    return 0;
  }`

I get the exception at this line- int totalBytesRead = inputStream.read(fourBytes);

sogytots
  • 35
  • 4
  • Looks like a network I/O issue that can happen... You can't change your code to avoid this happening. Instead, you need to handle it at some point, and either try again later, or fail gracefully (ie. without leaving the file or rest of your program in a bad state). – Harald K Jan 29 '21 at 08:24
  • I used to see periodic errors in my Java apps network file transfers, which weren't related to a particular file read. It turned out to be a 2 month warning of total NAS drive failure. – DuncG Jan 29 '21 at 11:38

1 Answers1

1

You probably have a issue with the underlying network connection. This is not a type of problem you can fix, there will always be intermittent problems with networks. This means that you will have to live with it, and mitigate the impact.

Maybe something like this:

public static int getEPSSectionOffsetWithRetry(File file) {
   int retryCount = 3;
   for(int i=0; i < retryCount i++) {
       try {
           int offset = getEPSSectionOffset()
           return offset;
       } catch (IOException ex) {
          //Maybe wait i little
       }
   }
   throw new IOException("Retry count exceeded");
}
Fedearne
  • 7,049
  • 4
  • 27
  • 31