UF.NIO

UF (Use Freed) issues are reported when there is an attempt to use resources after they were released. The UF.NIO warning indicates an attempt to use a java.nio.* object after it was closed.

Example 1

15      public boolean checkMeta(ReadableByteChannel channel) {
16         final ByteBuffer b = ByteBuffer.allocate(8);
17         try {
18             final int i = channel.read(b);
19             if (i < 8) {
20                 return false;
21             }
22         } catch (IOException e) {
23             return false;
24         } finally {
25             try {
26                 channel.close();
27             } catch (IOException e) {
28                 // do nothing
29             }
30         }
31 
32         // Cast unsigned character constants prior to comparison
33         return (b.get(0) == (byte) 'm' && b.get(1) == (byte) 'y' &&
34                 b.get(2) == (byte) 'f' && b.get(3) == (byte) 'o' &&
35                 b.get(4) == (byte) 'r' && b.get(5) == (byte) 'm' &&
36                 b.get(6) == (byte) 'a' && b.get(7) == (byte) 't');
37     }
38 
39     public void printOut(final String path) throws IOException {
40         final ReadableByteChannel ch = new FileInputStream(path).getChannel();
41         if (checkMeta(ch)) {
42             try {
43                 final ByteBuffer b = ByteBuffer.allocate(1024);
44                 int i;
45                 while ((i = ch.read(b)) > 0) {
46                     print(b);
47                 }
48             } catch (IOException e) {
49                 e.printStackTrace();
50             }
51         }
52     }
53 
54     public void print(final ByteBuffer b) {
55         while (b.hasRemaining()) {
56             System.out.print(b);
57         }
58         b.clear();
59     }

UF.NIO is reported for the snippet on line 45: There is an attempt to read from the channel which is closed on line 26