55import java .nio .Buffer ;
66import java .nio .ByteBuffer ;
77import java .nio .ByteOrder ;
8+ import java .util .concurrent .atomic .AtomicBoolean ;
89
910/**
1011 * File that is backed by a byte buffer.
1314 */
1415public final class BufferData implements ByteData {
1516 private final ByteBuffer buffer ;
16- private volatile boolean cleaned ;
17+ private final AtomicBoolean cleaned ;
1718
18- private BufferData (ByteBuffer buffer ) {
19+ private BufferData (ByteBuffer buffer , AtomicBoolean cleaned ) {
1920 this .buffer = buffer ;
21+ this .cleaned = cleaned ;
2022 }
2123
2224 @ Override
@@ -43,8 +45,7 @@ public void get(long position, byte[] b, int off, int len) {
4345 ensureOpen ();
4446 // Left intentionally as unchained calls due to API differences across Java versions
4547 // and how the compiler changes output.
46- ByteBuffer buffer = this .buffer ;
47- buffer .slice ();
48+ ByteBuffer buffer = this .buffer .slice ();
4849 buffer .order (buffer .order ());
4950 ((Buffer ) buffer ).position (validate (position ));
5051 buffer .get (b , off , len );
@@ -73,7 +74,7 @@ public void transferTo(OutputStream out, byte[] buf) throws IOException {
7374 @ Override
7475 public ByteData slice (long startIndex , long endIndex ) {
7576 ensureOpen ();
76- return new BufferData (ByteDataUtil .sliceExact (buffer , validate (startIndex ), validate (endIndex )));
77+ return new BufferData (ByteDataUtil .sliceExact (buffer , validate (startIndex ), validate (endIndex )), cleaned );
7778 }
7879
7980 @ Override
@@ -97,11 +98,11 @@ public int hashCode() {
9798
9899 @ Override
99100 public void close () {
100- if (!cleaned ) {
101+ if (!cleaned . get () ) {
101102 synchronized (this ) {
102- if (cleaned )
103+ if (cleaned . get () )
103104 return ;
104- cleaned = true ;
105+ cleaned . set ( true ) ;
105106 ByteBuffer buffer = this .buffer ;
106107 if (buffer .isDirect ()) {
107108 CleanerUtil .invokeCleaner (buffer );
@@ -110,17 +111,8 @@ public void close() {
110111 }
111112 }
112113
113- @ Override
114- protected void finalize () throws Throwable {
115- try {
116- close ();
117- } finally {
118- super .finalize ();
119- }
120- }
121-
122114 private void ensureOpen () {
123- if (cleaned )
115+ if (cleaned . get () )
124116 throw new IllegalStateException ("Cannot access data after close" );
125117 }
126118
@@ -138,7 +130,7 @@ private static int validate(long v) {
138130 * @return Buffer data.
139131 */
140132 public static BufferData wrap (ByteBuffer buffer ) {
141- return new BufferData (buffer );
133+ return new BufferData (buffer , new AtomicBoolean () );
142134 }
143135
144136 /**
@@ -148,6 +140,6 @@ public static BufferData wrap(ByteBuffer buffer) {
148140 * @return Buffer data.
149141 */
150142 public static BufferData wrap (byte [] array ) {
151- return new BufferData (ByteBuffer .wrap (array ).order (ByteOrder .LITTLE_ENDIAN ));
143+ return new BufferData (ByteBuffer .wrap (array ).order (ByteOrder .LITTLE_ENDIAN ), new AtomicBoolean () );
152144 }
153145}
0 commit comments