Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

image - JAVA NIO package, getting unexpected network error occured

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);

question from:https://stackoverflow.com/questions/65950218/java-nio-package-getting-unexpected-network-error-occured

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...