make all data buffers releasable

This commit is contained in:
Jörg Prante 2023-03-30 10:37:37 +02:00
parent 9fec73d255
commit 508a3099a0
6 changed files with 17 additions and 30 deletions

View file

@ -1,5 +1,5 @@
group = org.xbib group = org.xbib
name = net name = net
version = 3.0.5 version = 3.1.0
org.gradle.warning.mode = ALL org.gradle.warning.mode = ALL

View file

@ -30,7 +30,7 @@ import java.util.function.IntPredicate;
* <p>The {@linkplain #capacity() capacity} of a {@code DataBuffer} is expanded on demand, * <p>The {@linkplain #capacity() capacity} of a {@code DataBuffer} is expanded on demand,
* similar to {@code StringBuilder}. * similar to {@code StringBuilder}.
*/ */
public interface DataBuffer { public interface DataBuffer extends Releasable {
/** /**
* Return the {@link DataBufferFactory} that created this buffer. * Return the {@link DataBufferFactory} that created this buffer.

View file

@ -3,29 +3,17 @@ package org.xbib.net.buffer;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel; import java.nio.channels.ReadableByteChannel;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DataBufferUtil { public class DataBufferUtil {
private static final Logger logger = Logger.getLogger(DataBufferUtil.class.getName());
private DataBufferUtil() { private DataBufferUtil() {
} }
public static boolean release(DataBuffer dataBuffer) { public static boolean release(DataBuffer dataBuffer) {
if (dataBuffer instanceof PooledDataBuffer) { if (dataBuffer instanceof PooledDataBuffer pooledDataBuffer) {
PooledDataBuffer pooledDataBuffer = (PooledDataBuffer) dataBuffer;
if (pooledDataBuffer.isAllocated()) { if (pooledDataBuffer.isAllocated()) {
try { pooledDataBuffer.release();
return pooledDataBuffer.release(); return true;
}
catch (IllegalStateException ex) {
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER, "failed to release PooledDataBuffer " + dataBuffer, ex);
}
return false;
}
} }
} }
return false; return false;
@ -38,8 +26,7 @@ public class DataBufferUtil {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T extends DataBuffer> T retain(T dataBuffer) { public static <T extends DataBuffer> T retain(T dataBuffer) {
if (dataBuffer instanceof PooledDataBuffer) { if (dataBuffer instanceof PooledDataBuffer pooledDataBuffer) {
PooledDataBuffer pooledDataBuffer = (PooledDataBuffer) dataBuffer;
return (T) pooledDataBuffer.retain(); return (T) pooledDataBuffer.retain();
} }
else { else {

View file

@ -259,12 +259,10 @@ public class DefaultDataBuffer implements DataBuffer {
public DefaultDataBuffer write(byte[] source, int offset, int length) { public DefaultDataBuffer write(byte[] source, int offset, int length) {
Objects.requireNonNull(source, "Byte array must not be null"); Objects.requireNonNull(source, "Byte array must not be null");
ensureCapacity(length); ensureCapacity(length);
ByteBuffer tmp = this.byteBuffer.duplicate(); ByteBuffer tmp = this.byteBuffer.duplicate();
int limit = this.writePosition + length; int limit = this.writePosition + length;
tmp.clear().position(this.writePosition).limit(limit); tmp.clear().position(this.writePosition).limit(limit);
tmp.put(source, offset, length); tmp.put(source, offset, length);
this.writePosition += length; this.writePosition += length;
return this; return this;
} }
@ -319,7 +317,6 @@ public class DefaultDataBuffer implements DataBuffer {
@Override @Override
public ByteBuffer asByteBuffer(int index, int length) { public ByteBuffer asByteBuffer(int index, int length) {
checkIndex(index, length); checkIndex(index, length);
ByteBuffer duplicate = this.byteBuffer.duplicate(); ByteBuffer duplicate = this.byteBuffer.duplicate();
duplicate.position(index); duplicate.position(index);
duplicate.limit(index + length); duplicate.limit(index + length);
@ -431,6 +428,11 @@ public class DefaultDataBuffer implements DataBuffer {
} }
} }
@Override
public void release() {
// nothing to do
}
private class DefaultDataBufferInputStream extends InputStream { private class DefaultDataBufferInputStream extends InputStream {
@Override @Override

View file

@ -24,12 +24,4 @@ public interface PooledDataBuffer extends DataBuffer {
*/ */
PooledDataBuffer touch(Object hint); PooledDataBuffer touch(Object hint);
/**
* Decrease the reference count for this buffer by one,
* and deallocate it once the count reaches zero.
* @return {@code true} if the buffer was deallocated;
* {@code false} otherwise
*/
boolean release();
} }

View file

@ -0,0 +1,6 @@
package org.xbib.net.buffer;
public interface Releasable {
void release();
}