Make `release(int)` methods handle errors better

Exception types and error messages are now unified and more descriptive.
This commit is contained in:
Chris Vest 2021-05-31 15:06:48 +02:00
parent db7ac99699
commit a48a12794d
4 changed files with 28 additions and 12 deletions

View File

@ -1678,13 +1678,14 @@ public final class ByteBufAdaptor extends ByteBuf {
@Override
public boolean release(int decrement) {
if (!buffer.isAccessible() || decrement > 1 + Statics.countBorrows((ResourceSupport<?, ?>) buffer)) {
throw new IllegalReferenceCountException();
int refCount = 1 + Statics.countBorrows((ResourceSupport<?, ?>) buffer);
if (!buffer.isAccessible() || decrement > refCount) {
throw new IllegalReferenceCountException(refCount, -decrement);
}
for (int i = 0; i < decrement; i++) {
try {
buffer.close();
} catch (IllegalStateException e) {
} catch (RuntimeException e) {
throw new IllegalReferenceCountException(e);
}
}

View File

@ -1246,11 +1246,16 @@ class NioBuffer extends ResourceSupport<Buffer, NioBuffer> implements Buffer, Re
@Override
public boolean release(int decrement) {
if (!isAccessible() || decrement > 1 + countBorrows()) {
throw new IllegalReferenceCountException();
int refCount = 1 + countBorrows();
if (!isAccessible() || decrement > refCount) {
throw new IllegalReferenceCountException(refCount, -decrement);
}
for (int i = 0; i < decrement; i++) {
close();
try {
close();
} catch (RuntimeException e) {
throw new IllegalReferenceCountException(e);
}
}
return !isAccessible();
}

View File

@ -1661,11 +1661,16 @@ class UnsafeBuffer extends ResourceSupport<Buffer, UnsafeBuffer> implements Buff
@Override
public boolean release(int decrement) {
if (!isAccessible() || decrement > 1 + countBorrows()) {
throw new IllegalReferenceCountException();
int refCount = 1 + countBorrows();
if (!isAccessible() || decrement > refCount) {
throw new IllegalReferenceCountException(refCount, -decrement);
}
for (int i = 0; i < decrement; i++) {
close();
try {
close();
} catch (RuntimeException e) {
throw new IllegalReferenceCountException(e);
}
}
return !isAccessible();
}

View File

@ -1267,11 +1267,16 @@ class MemSegBuffer extends ResourceSupport<Buffer, MemSegBuffer> implements Buff
@Override
public boolean release(int decrement) {
if (!isAccessible() || decrement > 1 + countBorrows()) {
throw new IllegalReferenceCountException();
int refCount = 1 + countBorrows();
if (!isAccessible() || decrement > refCount) {
throw new IllegalReferenceCountException(refCount, -decrement);
}
for (int i = 0; i < decrement; i++) {
close();
try {
close();
} catch (RuntimeException e) {
throw new IllegalReferenceCountException(e);
}
}
return !isAccessible();
}