8292279: (fs) Use try-with-resources to release NativeBuffer

Reviewed-by: alanb
This commit is contained in:
Andrey Turbanov 2022-08-17 07:18:29 +00:00
parent a25e1dc53c
commit 1d9c2f7a6e
13 changed files with 61 additions and 168 deletions

View file

@ -212,8 +212,7 @@ class LinuxDosFileAttributeView
private int getDosAttribute(int fd) throws UnixException {
final int size = 24;
NativeBuffer buffer = NativeBuffers.getNativeBuffer(size);
try {
try (NativeBuffer buffer = NativeBuffers.getNativeBuffer(size)) {
int len = LinuxNativeDispatcher
.fgetxattr(fd, DOS_XATTR_NAME_AS_BYTES, buffer.address(), size);
@ -243,8 +242,6 @@ class LinuxDosFileAttributeView
if (x.errno() == ENODATA)
return 0;
throw x;
} finally {
buffer.release();
}
}
@ -266,12 +263,9 @@ class LinuxDosFileAttributeView
}
if (newValue != oldValue) {
byte[] value = Util.toBytes("0x" + Integer.toHexString(newValue));
NativeBuffer buffer = NativeBuffers.asNativeBuffer(value);
try {
try (NativeBuffer buffer = NativeBuffers.asNativeBuffer(value)) {
LinuxNativeDispatcher.fsetxattr(fd, DOS_XATTR_NAME_AS_BYTES,
buffer.address(), value.length+1);
} finally {
buffer.release();
}
}
} catch (UnixException x) {

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -36,13 +36,9 @@ class LinuxNativeDispatcher extends UnixNativeDispatcher {
* FILE *setmntent(const char *filename, const char *type);
*/
static long setmntent(byte[] filename, byte[] type) throws UnixException {
NativeBuffer pathBuffer = NativeBuffers.asNativeBuffer(filename);
NativeBuffer typeBuffer = NativeBuffers.asNativeBuffer(type);
try {
try (NativeBuffer pathBuffer = NativeBuffers.asNativeBuffer(filename);
NativeBuffer typeBuffer = NativeBuffers.asNativeBuffer(type)) {
return setmntent0(pathBuffer.address(), typeBuffer.address());
} finally {
typeBuffer.release();
pathBuffer.release();
}
}
private static native long setmntent0(long pathAddress, long typeAddress)
@ -53,11 +49,8 @@ class LinuxNativeDispatcher extends UnixNativeDispatcher {
*/
static int getmntent(long fp, UnixMountEntry entry, int buflen) throws UnixException {
NativeBuffer buffer = NativeBuffers.getNativeBuffer(buflen);
try {
try (NativeBuffer buffer = NativeBuffers.getNativeBuffer(buflen)) {
return getmntent0(fp, entry, buffer.address(), buflen);
} finally {
buffer.release();
}
}

View file

@ -250,15 +250,10 @@ class LinuxWatchService
}
// register with inotify (replaces existing mask if already registered)
int wd = -1;
try {
NativeBuffer buffer =
NativeBuffers.asNativeBuffer(dir.getByteArrayForSysCalls());
try {
wd = inotifyAddWatch(ifd, buffer.address(), mask);
} finally {
buffer.release();
}
int wd;
try (NativeBuffer buffer =
NativeBuffers.asNativeBuffer(dir.getByteArrayForSysCalls())) {
wd = inotifyAddWatch(ifd, buffer.address(), mask);
} catch (UnixException x) {
if (x.errno() == ENOSPC) {
return new IOException("User limit of inotify watches reached");