Java 8 migration. Auto close for try catch blocks (#8752)
Motivation: Since Java 7 we can automatically close resources in try () construction. Modification: Changed all try catches in the code with autoclose try (resource) Result: Less boiler-plate
This commit is contained in:
parent
b2a9a8e9fe
commit
c34340fff8
@ -125,8 +125,7 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData {
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
FileOutputStream outputStream = new FileOutputStream(file);
|
try (FileOutputStream outputStream = new FileOutputStream(file)) {
|
||||||
try {
|
|
||||||
FileChannel localfileChannel = outputStream.getChannel();
|
FileChannel localfileChannel = outputStream.getChannel();
|
||||||
ByteBuffer byteBuffer = buffer.nioBuffer();
|
ByteBuffer byteBuffer = buffer.nioBuffer();
|
||||||
int written = 0;
|
int written = 0;
|
||||||
@ -135,8 +134,6 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData {
|
|||||||
}
|
}
|
||||||
buffer.readerIndex(buffer.readerIndex() + written);
|
buffer.readerIndex(buffer.readerIndex() + written);
|
||||||
localfileChannel.force(false);
|
localfileChannel.force(false);
|
||||||
} finally {
|
|
||||||
outputStream.close();
|
|
||||||
}
|
}
|
||||||
setCompleted();
|
setCompleted();
|
||||||
} finally {
|
} finally {
|
||||||
@ -217,9 +214,8 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData {
|
|||||||
delete();
|
delete();
|
||||||
}
|
}
|
||||||
file = tempFile();
|
file = tempFile();
|
||||||
FileOutputStream outputStream = new FileOutputStream(file);
|
|
||||||
int written = 0;
|
int written = 0;
|
||||||
try {
|
try (FileOutputStream outputStream = new FileOutputStream(file)) {
|
||||||
FileChannel localfileChannel = outputStream.getChannel();
|
FileChannel localfileChannel = outputStream.getChannel();
|
||||||
byte[] bytes = new byte[4096 * 4];
|
byte[] bytes = new byte[4096 * 4];
|
||||||
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
|
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
|
||||||
@ -231,8 +227,6 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData {
|
|||||||
read = inputStream.read(bytes);
|
read = inputStream.read(bytes);
|
||||||
}
|
}
|
||||||
localfileChannel.force(false);
|
localfileChannel.force(false);
|
||||||
} finally {
|
|
||||||
outputStream.close();
|
|
||||||
}
|
}
|
||||||
size = written;
|
size = written;
|
||||||
if (definedSize > 0 && definedSize < size) {
|
if (definedSize > 0 && definedSize < size) {
|
||||||
@ -422,17 +416,14 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData {
|
|||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"File too big to be loaded in memory");
|
"File too big to be loaded in memory");
|
||||||
}
|
}
|
||||||
FileInputStream inputStream = new FileInputStream(src);
|
|
||||||
byte[] array = new byte[(int) srcsize];
|
byte[] array = new byte[(int) srcsize];
|
||||||
try {
|
try (FileInputStream inputStream = new FileInputStream(src)) {
|
||||||
FileChannel fileChannel = inputStream.getChannel();
|
FileChannel fileChannel = inputStream.getChannel();
|
||||||
ByteBuffer byteBuffer = ByteBuffer.wrap(array);
|
ByteBuffer byteBuffer = ByteBuffer.wrap(array);
|
||||||
int read = 0;
|
int read = 0;
|
||||||
while (read < srcsize) {
|
while (read < srcsize) {
|
||||||
read += fileChannel.read(byteBuffer);
|
read += fileChannel.read(byteBuffer);
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
inputStream.close();
|
|
||||||
}
|
}
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
@ -175,22 +175,12 @@ public class LzmaFrameEncoder extends MessageToByteEncoder<ByteBuf> {
|
|||||||
@Override
|
@Override
|
||||||
protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception {
|
protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception {
|
||||||
final int length = in.readableBytes();
|
final int length = in.readableBytes();
|
||||||
InputStream bbIn = null;
|
try (InputStream bbIn = new ByteBufInputStream(in);
|
||||||
ByteBufOutputStream bbOut = null;
|
ByteBufOutputStream bbOut = new ByteBufOutputStream(out)) {
|
||||||
try {
|
|
||||||
bbIn = new ByteBufInputStream(in);
|
|
||||||
bbOut = new ByteBufOutputStream(out);
|
|
||||||
bbOut.writeByte(properties);
|
bbOut.writeByte(properties);
|
||||||
bbOut.writeInt(littleEndianDictionarySize);
|
bbOut.writeInt(littleEndianDictionarySize);
|
||||||
bbOut.writeLong(Long.reverseBytes(length));
|
bbOut.writeLong(Long.reverseBytes(length));
|
||||||
encoder.code(bbIn, bbOut, -1, -1, null);
|
encoder.code(bbIn, bbOut, -1, -1, null);
|
||||||
} finally {
|
|
||||||
if (bbIn != null) {
|
|
||||||
bbIn.close();
|
|
||||||
}
|
|
||||||
if (bbOut != null) {
|
|
||||||
bbOut.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,12 +62,13 @@ public class CompatibleMarshallingDecoder extends ReplayingDecoder<Void> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Unmarshaller unmarshaller = provider.getUnmarshaller(ctx);
|
// Call close in a finally block as the ReplayingDecoder will throw an Error if not enough bytes are
|
||||||
ByteInput input = new ChannelBufferByteInput(buffer);
|
// readable. This helps to be sure that we do not leak resource
|
||||||
if (maxObjectSize != Integer.MAX_VALUE) {
|
try (Unmarshaller unmarshaller = provider.getUnmarshaller(ctx)) {
|
||||||
input = new LimitingByteInput(input, maxObjectSize);
|
ByteInput input = new ChannelBufferByteInput(buffer);
|
||||||
}
|
if (maxObjectSize != Integer.MAX_VALUE) {
|
||||||
try {
|
input = new LimitingByteInput(input, maxObjectSize);
|
||||||
|
}
|
||||||
unmarshaller.start(input);
|
unmarshaller.start(input);
|
||||||
Object obj = unmarshaller.readObject();
|
Object obj = unmarshaller.readObject();
|
||||||
unmarshaller.finish();
|
unmarshaller.finish();
|
||||||
@ -75,10 +76,6 @@ public class CompatibleMarshallingDecoder extends ReplayingDecoder<Void> {
|
|||||||
} catch (LimitingByteInput.TooBigObjectException ignored) {
|
} catch (LimitingByteInput.TooBigObjectException ignored) {
|
||||||
discardingTooLongFrame = true;
|
discardingTooLongFrame = true;
|
||||||
throw new TooLongFrameException();
|
throw new TooLongFrameException();
|
||||||
} finally {
|
|
||||||
// Call close in a finally block as the ReplayingDecoder will throw an Error if not enough bytes are
|
|
||||||
// readable. This helps to be sure that we do not leak resource
|
|
||||||
unmarshaller.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,18 +65,14 @@ public class MarshallingDecoder extends LengthFieldBasedFrameDecoder {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Unmarshaller unmarshaller = provider.getUnmarshaller(ctx);
|
// Call close in a finally block as the ReplayingDecoder will throw an Error if not enough bytes are
|
||||||
ByteInput input = new ChannelBufferByteInput(frame);
|
// readable. This helps to be sure that we do not leak resource
|
||||||
|
try (Unmarshaller unmarshaller = provider.getUnmarshaller(ctx)) {
|
||||||
try {
|
ByteInput input = new ChannelBufferByteInput(frame);
|
||||||
unmarshaller.start(input);
|
unmarshaller.start(input);
|
||||||
Object obj = unmarshaller.readObject();
|
Object obj = unmarshaller.readObject();
|
||||||
unmarshaller.finish();
|
unmarshaller.finish();
|
||||||
return obj;
|
return obj;
|
||||||
} finally {
|
|
||||||
// Call close in a finally block as the ReplayingDecoder will throw an Error if not enough bytes are
|
|
||||||
// readable. This helps to be sure that we do not leak resource
|
|
||||||
unmarshaller.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,11 +73,10 @@ public class CompatibleObjectEncoder extends MessageToByteEncoder<Serializable>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception {
|
protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception {
|
||||||
ObjectOutputStream oos = newObjectOutputStream(new ByteBufOutputStream(out));
|
try (ObjectOutputStream oos = newObjectOutputStream(new ByteBufOutputStream(out))) {
|
||||||
try {
|
|
||||||
if (resetInterval != 0) {
|
if (resetInterval != 0) {
|
||||||
// Resetting will prevent OOM on the receiving side.
|
// Resetting will prevent OOM on the receiving side.
|
||||||
writtenObjects ++;
|
writtenObjects++;
|
||||||
if (writtenObjects % resetInterval == 0) {
|
if (writtenObjects % resetInterval == 0) {
|
||||||
oos.reset();
|
oos.reset();
|
||||||
}
|
}
|
||||||
@ -85,8 +84,6 @@ public class CompatibleObjectEncoder extends MessageToByteEncoder<Serializable>
|
|||||||
|
|
||||||
oos.writeObject(msg);
|
oos.writeObject(msg);
|
||||||
oos.flush();
|
oos.flush();
|
||||||
} finally {
|
|
||||||
oos.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,11 +71,9 @@ public class ObjectDecoder extends LengthFieldBasedFrameDecoder {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
ObjectInputStream ois = new CompactObjectInputStream(new ByteBufInputStream(frame, true), classResolver);
|
try (ObjectInputStream ois =
|
||||||
try {
|
new CompactObjectInputStream(new ByteBufInputStream(frame, true), classResolver)) {
|
||||||
return ois.readObject();
|
return ois.readObject();
|
||||||
} finally {
|
|
||||||
ois.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -82,12 +82,9 @@ public class ObjectEncoderOutputStream extends OutputStream implements
|
|||||||
public void writeObject(Object obj) throws IOException {
|
public void writeObject(Object obj) throws IOException {
|
||||||
ByteBuf buf = Unpooled.buffer(estimatedLength);
|
ByteBuf buf = Unpooled.buffer(estimatedLength);
|
||||||
try {
|
try {
|
||||||
ObjectOutputStream oout = new CompactObjectOutputStream(new ByteBufOutputStream(buf));
|
try (ObjectOutputStream oout = new CompactObjectOutputStream(new ByteBufOutputStream(buf))) {
|
||||||
try {
|
|
||||||
oout.writeObject(obj);
|
oout.writeObject(obj);
|
||||||
oout.flush();
|
oout.flush();
|
||||||
} finally {
|
|
||||||
oout.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int objectSize = buf.readableBytes();
|
int objectSize = buf.readableBytes();
|
||||||
|
@ -317,8 +317,7 @@ public final class NetUtil {
|
|||||||
try {
|
try {
|
||||||
InputStream is = process.getInputStream();
|
InputStream is = process.getInputStream();
|
||||||
InputStreamReader isr = new InputStreamReader(is);
|
InputStreamReader isr = new InputStreamReader(is);
|
||||||
BufferedReader br = new BufferedReader(isr);
|
try (BufferedReader br = new BufferedReader(isr)) {
|
||||||
try {
|
|
||||||
String line = br.readLine();
|
String line = br.readLine();
|
||||||
if (line.startsWith(sysctlKey)) {
|
if (line.startsWith(sysctlKey)) {
|
||||||
for (int i = line.length() - 1; i > sysctlKey.length(); --i) {
|
for (int i = line.length() - 1; i > sysctlKey.length(); --i) {
|
||||||
@ -328,13 +327,9 @@ public final class NetUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
|
||||||
br.close();
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (process != null) {
|
process.destroy();
|
||||||
process.destroy();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,16 +72,10 @@ public final class Version {
|
|||||||
Enumeration<URL> resources = classLoader.getResources("META-INF/io.netty.versions.properties");
|
Enumeration<URL> resources = classLoader.getResources("META-INF/io.netty.versions.properties");
|
||||||
while (resources.hasMoreElements()) {
|
while (resources.hasMoreElements()) {
|
||||||
URL url = resources.nextElement();
|
URL url = resources.nextElement();
|
||||||
InputStream in = url.openStream();
|
try (InputStream in = url.openStream()) {
|
||||||
try {
|
|
||||||
props.load(in);
|
props.load(in);
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
in.close();
|
|
||||||
} catch (Exception ignore) {
|
|
||||||
// Ignore.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// Ignore.
|
||||||
}
|
}
|
||||||
} catch (Exception ignore) {
|
} catch (Exception ignore) {
|
||||||
// Not critical. Just ignore.
|
// Not critical. Just ignore.
|
||||||
|
@ -170,11 +170,8 @@ public class OcspServerExample {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(in, CharsetUtil.US_ASCII));
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, CharsetUtil.US_ASCII))) {
|
||||||
try {
|
|
||||||
return parseCertificates(reader);
|
return parseCertificates(reader);
|
||||||
} finally {
|
|
||||||
reader.close();
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
in.close();
|
in.close();
|
||||||
|
@ -130,13 +130,11 @@ public final class OcspUtils {
|
|||||||
connection.setRequestProperty("accept", OCSP_RESPONSE_TYPE);
|
connection.setRequestProperty("accept", OCSP_RESPONSE_TYPE);
|
||||||
connection.setRequestProperty("content-length", String.valueOf(encoded.length));
|
connection.setRequestProperty("content-length", String.valueOf(encoded.length));
|
||||||
|
|
||||||
OutputStream out = connection.getOutputStream();
|
try (OutputStream out = connection.getOutputStream()) {
|
||||||
try {
|
|
||||||
out.write(encoded);
|
out.write(encoded);
|
||||||
out.flush();
|
out.flush();
|
||||||
|
|
||||||
InputStream in = connection.getInputStream();
|
try (InputStream in = connection.getInputStream()) {
|
||||||
try {
|
|
||||||
int code = connection.getResponseCode();
|
int code = connection.getResponseCode();
|
||||||
if (code != HttpsURLConnection.HTTP_OK) {
|
if (code != HttpsURLConnection.HTTP_OK) {
|
||||||
throw new IOException("Unexpected status-code=" + code);
|
throw new IOException("Unexpected status-code=" + code);
|
||||||
@ -169,11 +167,7 @@ public final class OcspUtils {
|
|||||||
baos.close();
|
baos.close();
|
||||||
}
|
}
|
||||||
return new OCSPResp(baos.toByteArray());
|
return new OCSPResp(baos.toByteArray());
|
||||||
} finally {
|
|
||||||
in.close();
|
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
out.close();
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
connection.disconnect();
|
connection.disconnect();
|
||||||
|
@ -156,11 +156,8 @@ public final class TestUtils {
|
|||||||
long lastLogTime = System.nanoTime();
|
long lastLogTime = System.nanoTime();
|
||||||
long counter = 0;
|
long counter = 0;
|
||||||
|
|
||||||
InputStream in = null;
|
try (InputStream in = new FileInputStream(filename);
|
||||||
OutputStream out = null;
|
OutputStream out = new XZOutputStream(new FileOutputStream(xzFilename), options)) {
|
||||||
try {
|
|
||||||
in = new FileInputStream(filename);
|
|
||||||
out = new XZOutputStream(new FileOutputStream(xzFilename), options);
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int readBytes = in.read(buf);
|
int readBytes = in.read(buf);
|
||||||
if (readBytes < 0) {
|
if (readBytes < 0) {
|
||||||
@ -180,25 +177,8 @@ public final class TestUtils {
|
|||||||
lastLogTime = currentTime;
|
lastLogTime = currentTime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out.close();
|
|
||||||
in.close();
|
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
logger.warn("Failed to compress the heap dump: {}", xzFilename, t);
|
logger.warn("Failed to compress the heap dump: {}", xzFilename, t);
|
||||||
} finally {
|
|
||||||
if (in != null) {
|
|
||||||
try {
|
|
||||||
in.close();
|
|
||||||
} catch (IOException ignored) {
|
|
||||||
// Ignore.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (out != null) {
|
|
||||||
try {
|
|
||||||
out.close();
|
|
||||||
} catch (IOException ignored) {
|
|
||||||
// Ignore.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete the uncompressed dump in favor of the compressed one.
|
// Delete the uncompressed dump in favor of the compressed one.
|
||||||
|
Loading…
Reference in New Issue
Block a user