make all data buffers releasable
This commit is contained in:
parent
9fec73d255
commit
508a3099a0
6 changed files with 17 additions and 30 deletions
|
@ -1,5 +1,5 @@
|
|||
group = org.xbib
|
||||
name = net
|
||||
version = 3.0.5
|
||||
version = 3.1.0
|
||||
|
||||
org.gradle.warning.mode = ALL
|
||||
|
|
|
@ -30,7 +30,7 @@ import java.util.function.IntPredicate;
|
|||
* <p>The {@linkplain #capacity() capacity} of a {@code DataBuffer} is expanded on demand,
|
||||
* similar to {@code StringBuilder}.
|
||||
*/
|
||||
public interface DataBuffer {
|
||||
public interface DataBuffer extends Releasable {
|
||||
|
||||
/**
|
||||
* Return the {@link DataBufferFactory} that created this buffer.
|
||||
|
|
|
@ -3,29 +3,17 @@ package org.xbib.net.buffer;
|
|||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class DataBufferUtil {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(DataBufferUtil.class.getName());
|
||||
|
||||
private DataBufferUtil() {
|
||||
}
|
||||
|
||||
public static boolean release(DataBuffer dataBuffer) {
|
||||
if (dataBuffer instanceof PooledDataBuffer) {
|
||||
PooledDataBuffer pooledDataBuffer = (PooledDataBuffer) dataBuffer;
|
||||
if (dataBuffer instanceof PooledDataBuffer pooledDataBuffer) {
|
||||
if (pooledDataBuffer.isAllocated()) {
|
||||
try {
|
||||
return pooledDataBuffer.release();
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
if (logger.isLoggable(Level.FINER)) {
|
||||
logger.log(Level.FINER, "failed to release PooledDataBuffer " + dataBuffer, ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
pooledDataBuffer.release();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
@ -38,8 +26,7 @@ public class DataBufferUtil {
|
|||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T extends DataBuffer> T retain(T dataBuffer) {
|
||||
if (dataBuffer instanceof PooledDataBuffer) {
|
||||
PooledDataBuffer pooledDataBuffer = (PooledDataBuffer) dataBuffer;
|
||||
if (dataBuffer instanceof PooledDataBuffer pooledDataBuffer) {
|
||||
return (T) pooledDataBuffer.retain();
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -259,12 +259,10 @@ public class DefaultDataBuffer implements DataBuffer {
|
|||
public DefaultDataBuffer write(byte[] source, int offset, int length) {
|
||||
Objects.requireNonNull(source, "Byte array must not be null");
|
||||
ensureCapacity(length);
|
||||
|
||||
ByteBuffer tmp = this.byteBuffer.duplicate();
|
||||
int limit = this.writePosition + length;
|
||||
tmp.clear().position(this.writePosition).limit(limit);
|
||||
tmp.put(source, offset, length);
|
||||
|
||||
this.writePosition += length;
|
||||
return this;
|
||||
}
|
||||
|
@ -319,7 +317,6 @@ public class DefaultDataBuffer implements DataBuffer {
|
|||
@Override
|
||||
public ByteBuffer asByteBuffer(int index, int length) {
|
||||
checkIndex(index, length);
|
||||
|
||||
ByteBuffer duplicate = this.byteBuffer.duplicate();
|
||||
duplicate.position(index);
|
||||
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 {
|
||||
|
||||
@Override
|
||||
|
|
|
@ -24,12 +24,4 @@ public interface PooledDataBuffer extends DataBuffer {
|
|||
*/
|
||||
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();
|
||||
|
||||
}
|
||||
|
|
6
net/src/main/java/org/xbib/net/buffer/Releasable.java
Normal file
6
net/src/main/java/org/xbib/net/buffer/Releasable.java
Normal file
|
@ -0,0 +1,6 @@
|
|||
package org.xbib.net.buffer;
|
||||
|
||||
public interface Releasable {
|
||||
|
||||
void release();
|
||||
}
|
Loading…
Reference in a new issue