-3

入力ストリームから読み取り、finallyブロックでin.close();を呼び出して閉じようとします。ただし、mainの実行はブロックされます。抜け道は何ですか?

提案では、私が使用したコードは、

if (!processed) {
                System.out.println("in processed");
                byte[] contents = new byte[(int) fileSplit.getLength()];
                Path file = fileSplit.getPath();
                FileSystem fs = file.getFileSystem(conf);
                FSDataInputStream in = null;
                try {
                    in = fs.open(file);
                    IOUtils.readFully(in, contents, 0, contents.length);

                    value.set(contents, 0, contents.length);
                } finally {
                    System.out.println("before close stream");
                    IOUtils.closeStream(in);
                }
                processed = true;
                return true;
            }
            System.out.println("out of processed");
            return false;
        }
4

1 に答える 1

-1

java.io.InputStream.closeはブロックしていません、少なくともAPIはそれを決して言いません。InputStream.readを比較します

 Reads the next byte of data from the input stream. The value byte is
 returned as an <code>int</code> in the range <code>0</code> to
 <code>255</code>. If no byte is available because the end of the stream
 has been reached, the value <code>-1</code> is returned. This method
 blocks until input data is available, the end of the stream is detected,
 or an exception is thrown.

およびInputStream.close

  Closes this file input stream and releases any system resources
  associated with the stream.

問題を解決するために、Java 7 Files.readAllBytesを使用して、パズルを忘れることをお勧めします。

于 2012-12-10T06:35:04.697 に答える