为什么Java在尝试获取文件锁时返回错误而不是IOException

时间:2016-03-01 15:43:09

标签: java nio ioexception

此代码在Windows 8.1上以Java 1.8.0_72 25.72-b15 64位运行

protected FileLock getFileLockForWriting(FileChannel fileChannel, String filePath) throws IOException
    {
        logger.finest("locking fileChannel for " + filePath);
        FileLock fileLock;
        try
        {
            fileLock = fileChannel.tryLock();
        }
        //Assumes locking is not supported on this platform so just returns null
        catch (IOException exception)
        {
            return null;
        }

        //Couldnt getFields lock because file is already locked by another application
        if (fileLock == null)
        {
            throw new IOException(ErrorMessage.GENERAL_WRITE_FAILED_FILE_LOCKED.getMsg(filePath));
        }
        return fileLock;
    }

正在为特定用户报告此错误

java.lang.Error: java.io.IOException: The specified server cannot perform the requested operation
    at sun.nio.ch.FileKey.create(Unknown Source)
    at sun.nio.ch.SharedFileLockTable.<init>(Unknown Source)
    at sun.nio.ch.FileLockTable.newSharedFileLockTable(Unknown Source)
    at sun.nio.ch.FileChannelImpl.fileLockTable(Unknown Source)
    at sun.nio.ch.FileChannelImpl.tryLock(Unknown Source)
    at java.nio.channels.FileChannel.tryLock(Unknown Source)
    at org.jaudiotagger.tag.id3.AbstractID3v2Tag.getFileLockForWriting(AbstractID3v2Tag.java:1080)
    at org.jaudiotagger.tag.id3.AbstractID3v2Tag.writeBufferToFile(AbstractID3v2Tag.java:1462)
    at org.jaudiotagger.tag.id3.ID3v23Tag.write(ID3v23Tag.java:751)
    at org.jaudiotagger.audio.mp3.MP3File.save(MP3File.java:1011)
    at org.jaudiotagger.audio.mp3.MP3File.save(MP3File.java:920)
    at org.jaudiotagger.audio.mp3.MP3File.commit(MP3File.java:932)
    at com.jthink.songkong.analyse.analyser.SongSaver.realSave(SongSaver.java:798)
    at com.jthink.songkong.analyse.analyser.SongSaver.saveSongToFile(SongSaver.java:623)
    at com.jthink.songkong.analyse.analyser.SongSaver.saveChanges(SongSaver.java:185)
    at com.jthink.songkong.analyse.analyser.SongSaver.call(SongSaver.java:160)
    at com.jthink.songkong.analyse.analyser.SongSaver.call(SongSaver.java:54)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: The specified server cannot perform the requested operation
    at sun.nio.ch.FileKey.init(Native Method)
    ... 21 more

似乎它返回一个错误,因为不支持文件锁定,但它应该返回一个IOException,(我的代码需要一个IOException),为什么这个

更新 我的情况相关它是西部数据MyBook Live 3TBNAS,固件版本是02.43.10-048,运行Debian 5.0.4

1 个答案:

答案 0 :(得分:3)

FileKey.create以上的所有代码都可以抛出IOException,因此您认为此行为不正确是正确的。

确实这已经归档为bug。在Java 9中,Error将会消失,IOException将成为thrown

因此,现在您应该准备好捕获错误并评估内部IOException。