Skip to content

Commit 85d46ce

Browse files
eustascopybara-github
authored andcommitted
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
1 parent 41a22f0 commit 85d46ce

File tree

5 files changed

+18
-44
lines changed

5 files changed

+18
-44
lines changed

java/org/brotli/wrapper/dec/Decoder.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* Base class for InputStream / Channel implementations.
1717
*/
18-
public class Decoder {
18+
public class Decoder implements AutoCloseable {
1919
private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0);
2020
private final ReadableByteChannel source;
2121
private final DecoderJNI.Wrapper decoder;
@@ -129,7 +129,8 @@ int consume(ByteBuffer dst) {
129129
return limit;
130130
}
131131

132-
void close() throws IOException {
132+
@Override
133+
public void close() throws IOException {
133134
if (closed) {
134135
return;
135136
}
@@ -140,9 +141,9 @@ void close() throws IOException {
140141

141142
/** Decodes the given data buffer starting at offset till length. */
142143
public static byte[] decompress(byte[] data, int offset, int length) throws IOException {
143-
DecoderJNI.Wrapper decoder = new DecoderJNI.Wrapper(length);
144-
ArrayList<byte[]> output = new ArrayList<byte[]>();
144+
ArrayList<byte[]> output = new ArrayList<>();
145145
int totalOutputSize = 0;
146+
DecoderJNI.Wrapper decoder = new DecoderJNI.Wrapper(length);
146147
try {
147148
decoder.getInputBuffer().put(data, offset, length);
148149
decoder.push(length);

java/org/brotli/wrapper/dec/DecoderJNI.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,5 @@ public void destroy() {
122122
nativeDestroy(context);
123123
context[0] = 0;
124124
}
125-
126-
@Override
127-
protected void finalize() throws Throwable {
128-
if (context[0] != 0) {
129-
/* TODO(eustas): log resource leak? */
130-
destroy();
131-
}
132-
super.finalize();
133-
}
134125
}
135126
}

java/org/brotli/wrapper/enc/BrotliOutputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
package org.brotli.wrapper.enc;
88

9-
import org.brotli.enc.PreparedDictionary;
109
import java.io.IOException;
1110
import java.io.OutputStream;
1211
import java.nio.channels.Channels;
12+
import org.brotli.enc.PreparedDictionary;
1313

1414
/**
1515
* Output stream that wraps native brotli encoder.

java/org/brotli/wrapper/enc/Encoder.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* Base class for OutputStream / Channel implementations.
1919
*/
20-
public class Encoder {
20+
public class Encoder implements AutoCloseable {
2121
private final WritableByteChannel destination;
2222
private final List<PreparedDictionary> dictionaries;
2323
private final EncoderJNI.Wrapper encoder;
@@ -65,12 +65,6 @@ public static final class Parameters {
6565

6666
public Parameters() { }
6767

68-
private Parameters(Parameters other) {
69-
this.quality = other.quality;
70-
this.lgwin = other.lgwin;
71-
this.mode = other.mode;
72-
}
73-
7468
/**
7569
* Setup encoder quality.
7670
*
@@ -199,7 +193,8 @@ void flush() throws IOException {
199193
encode(EncoderJNI.Operation.FLUSH);
200194
}
201195

202-
void close() throws IOException {
196+
@Override
197+
public void close() throws IOException {
203198
if (closed) {
204199
return;
205200
}
@@ -221,10 +216,10 @@ public static byte[] compress(byte[] data, int offset, int length, Parameters pa
221216
return empty;
222217
}
223218
/* data.length > 0 */
219+
ArrayList<byte[]> output = new ArrayList<>();
220+
int totalOutputSize = 0;
224221
EncoderJNI.Wrapper encoder =
225222
new EncoderJNI.Wrapper(length, params.quality, params.lgwin, params.mode);
226-
ArrayList<byte[]> output = new ArrayList<byte[]>();
227-
int totalOutputSize = 0;
228223
try {
229224
encoder.getInputBuffer().put(data, offset, length);
230225
encoder.push(EncoderJNI.Operation.FINISH, length);

java/org/brotli/wrapper/enc/EncoderJNI.java

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
package org.brotli.wrapper.enc;
88

9-
import org.brotli.enc.PreparedDictionary;
109
import java.io.IOException;
1110
import java.nio.ByteBuffer;
11+
import org.brotli.enc.PreparedDictionary;
1212

1313
/**
1414
* JNI wrapper for brotli encoder.
@@ -28,7 +28,7 @@ enum Operation {
2828
FINISH
2929
}
3030

31-
private static class PreparedDictionaryImpl implements PreparedDictionary {
31+
private static class PreparedDictionaryImpl implements AutoCloseable, PreparedDictionary {
3232
private ByteBuffer data;
3333
/** Reference to (non-copied) LZ data. */
3434
private ByteBuffer rawData;
@@ -43,15 +43,11 @@ public ByteBuffer getData() {
4343
}
4444

4545
@Override
46-
protected void finalize() throws Throwable {
47-
try {
48-
ByteBuffer data = this.data;
49-
this.data = null;
50-
this.rawData = null;
51-
nativeDestroyDictionary(data);
52-
} finally {
53-
super.finalize();
54-
}
46+
public void close() {
47+
ByteBuffer data = this.data;
48+
this.data = null;
49+
this.rawData = null;
50+
nativeDestroyDictionary(data);
5551
}
5652
}
5753

@@ -168,14 +164,5 @@ void destroy() {
168164
nativeDestroy(context);
169165
context[0] = 0;
170166
}
171-
172-
@Override
173-
protected void finalize() throws Throwable {
174-
if (context[0] != 0) {
175-
/* TODO(eustas): log resource leak? */
176-
destroy();
177-
}
178-
super.finalize();
179-
}
180167
}
181168
}

0 commit comments

Comments
 (0)