Drop finalize()

Now it is solely embedders responisbility to close things that hold native resources. No more "safety net".

Consider "try-with-resources". For longer lasting items (e.g. native PreparedDictionary) use Cleaner as a last resort.

PiperOrigin-RevId: 807584792
This commit is contained in:
Evgenii Kliuchnikov
2025-09-16 01:23:08 -07:00
committed by Copybara-Service
parent 41a22f07f2
commit 85d46ce6b5
5 changed files with 18 additions and 44 deletions

View File

@@ -15,7 +15,7 @@ import java.util.ArrayList;
/**
* Base class for InputStream / Channel implementations.
*/
public class Decoder {
public class Decoder implements AutoCloseable {
private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0);
private final ReadableByteChannel source;
private final DecoderJNI.Wrapper decoder;
@@ -129,7 +129,8 @@ public class Decoder {
return limit;
}
void close() throws IOException {
@Override
public void close() throws IOException {
if (closed) {
return;
}
@@ -140,9 +141,9 @@ public class Decoder {
/** Decodes the given data buffer starting at offset till length. */
public static byte[] decompress(byte[] data, int offset, int length) throws IOException {
DecoderJNI.Wrapper decoder = new DecoderJNI.Wrapper(length);
ArrayList<byte[]> output = new ArrayList<byte[]>();
ArrayList<byte[]> output = new ArrayList<>();
int totalOutputSize = 0;
DecoderJNI.Wrapper decoder = new DecoderJNI.Wrapper(length);
try {
decoder.getInputBuffer().put(data, offset, length);
decoder.push(length);

View File

@@ -122,14 +122,5 @@ public class DecoderJNI {
nativeDestroy(context);
context[0] = 0;
}
@Override
protected void finalize() throws Throwable {
if (context[0] != 0) {
/* TODO(eustas): log resource leak? */
destroy();
}
super.finalize();
}
}
}

View File

@@ -6,10 +6,10 @@
package org.brotli.wrapper.enc;
import org.brotli.enc.PreparedDictionary;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.channels.Channels;
import org.brotli.enc.PreparedDictionary;
/**
* Output stream that wraps native brotli encoder.

View File

@@ -17,7 +17,7 @@ import org.brotli.enc.PreparedDictionary;
/**
* Base class for OutputStream / Channel implementations.
*/
public class Encoder {
public class Encoder implements AutoCloseable {
private final WritableByteChannel destination;
private final List<PreparedDictionary> dictionaries;
private final EncoderJNI.Wrapper encoder;
@@ -65,12 +65,6 @@ public class Encoder {
public Parameters() { }
private Parameters(Parameters other) {
this.quality = other.quality;
this.lgwin = other.lgwin;
this.mode = other.mode;
}
/**
* Setup encoder quality.
*
@@ -199,7 +193,8 @@ public class Encoder {
encode(EncoderJNI.Operation.FLUSH);
}
void close() throws IOException {
@Override
public void close() throws IOException {
if (closed) {
return;
}
@@ -221,10 +216,10 @@ public class Encoder {
return empty;
}
/* data.length > 0 */
ArrayList<byte[]> output = new ArrayList<>();
int totalOutputSize = 0;
EncoderJNI.Wrapper encoder =
new EncoderJNI.Wrapper(length, params.quality, params.lgwin, params.mode);
ArrayList<byte[]> output = new ArrayList<byte[]>();
int totalOutputSize = 0;
try {
encoder.getInputBuffer().put(data, offset, length);
encoder.push(EncoderJNI.Operation.FINISH, length);

View File

@@ -6,9 +6,9 @@
package org.brotli.wrapper.enc;
import org.brotli.enc.PreparedDictionary;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.brotli.enc.PreparedDictionary;
/**
* JNI wrapper for brotli encoder.
@@ -28,7 +28,7 @@ class EncoderJNI {
FINISH
}
private static class PreparedDictionaryImpl implements PreparedDictionary {
private static class PreparedDictionaryImpl implements AutoCloseable, PreparedDictionary {
private ByteBuffer data;
/** Reference to (non-copied) LZ data. */
private ByteBuffer rawData;
@@ -43,15 +43,11 @@ class EncoderJNI {
}
@Override
protected void finalize() throws Throwable {
try {
ByteBuffer data = this.data;
this.data = null;
this.rawData = null;
nativeDestroyDictionary(data);
} finally {
super.finalize();
}
public void close() {
ByteBuffer data = this.data;
this.data = null;
this.rawData = null;
nativeDestroyDictionary(data);
}
}
@@ -168,14 +164,5 @@ class EncoderJNI {
nativeDestroy(context);
context[0] = 0;
}
@Override
protected void finalize() throws Throwable {
if (context[0] != 0) {
/* TODO(eustas): log resource leak? */
destroy();
}
super.finalize();
}
}
}