2013-01-01 16:03:18 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2013 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
2020-10-23 14:44:18 +02:00
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
2013-01-01 16:03:18 +01:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2021-09-17 16:28:14 +02:00
|
|
|
package io.net5.buffer;
|
2013-01-01 16:03:18 +01:00
|
|
|
|
2019-02-04 10:32:25 +01:00
|
|
|
import static java.util.Objects.requireNonNull;
|
|
|
|
|
2021-09-17 16:28:14 +02:00
|
|
|
import io.net5.util.internal.StringUtil;
|
2013-06-13 06:14:21 +02:00
|
|
|
|
2013-01-01 16:03:18 +01:00
|
|
|
/**
|
|
|
|
* Default implementation of a {@link ByteBufHolder} that holds it's data in a {@link ByteBuf}.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class DefaultByteBufHolder implements ByteBufHolder {
|
2013-02-10 05:10:09 +01:00
|
|
|
|
2013-01-01 16:03:18 +01:00
|
|
|
private final ByteBuf data;
|
2013-02-10 05:10:09 +01:00
|
|
|
|
2013-01-01 16:03:18 +01:00
|
|
|
public DefaultByteBufHolder(ByteBuf data) {
|
2019-02-04 10:32:25 +01:00
|
|
|
requireNonNull(data, "data");
|
2013-01-01 16:03:18 +01:00
|
|
|
this.data = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-05-01 10:04:43 +02:00
|
|
|
public ByteBuf content() {
|
2020-08-28 09:18:13 +02:00
|
|
|
return ByteBufUtil.ensureAccessible(data);
|
2013-01-01 16:03:18 +01:00
|
|
|
}
|
|
|
|
|
2016-04-14 10:31:48 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
* <p>
|
|
|
|
* This method calls {@code replace(content().copy())} by default.
|
|
|
|
*/
|
2013-01-01 16:03:18 +01:00
|
|
|
@Override
|
2013-02-10 05:10:09 +01:00
|
|
|
public ByteBufHolder copy() {
|
2016-04-14 10:31:48 +02:00
|
|
|
return replace(data.copy());
|
2013-01-01 16:03:18 +01:00
|
|
|
}
|
|
|
|
|
2016-04-14 10:31:48 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
* <p>
|
|
|
|
* This method calls {@code replace(content().duplicate())} by default.
|
|
|
|
*/
|
2013-07-06 21:17:51 +02:00
|
|
|
@Override
|
|
|
|
public ByteBufHolder duplicate() {
|
2016-04-14 10:31:48 +02:00
|
|
|
return replace(data.duplicate());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
* <p>
|
|
|
|
* This method calls {@code replace(content().retainedDuplicate())} by default.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public ByteBufHolder retainedDuplicate() {
|
|
|
|
return replace(data.retainedDuplicate());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
* <p>
|
|
|
|
* Override this method to return a new instance of this object whose content is set to the specified
|
|
|
|
* {@code content}. The default implementation of {@link #copy()}, {@link #duplicate()} and
|
|
|
|
* {@link #retainedDuplicate()} invokes this method to create a copy.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public ByteBufHolder replace(ByteBuf content) {
|
|
|
|
return new DefaultByteBufHolder(content);
|
2013-07-06 21:17:51 +02:00
|
|
|
}
|
|
|
|
|
2013-01-01 16:03:18 +01:00
|
|
|
@Override
|
2013-02-10 05:10:09 +01:00
|
|
|
public int refCnt() {
|
|
|
|
return data.refCnt();
|
2013-01-01 16:03:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-02-13 08:09:33 +01:00
|
|
|
public ByteBufHolder retain() {
|
2013-02-10 05:10:09 +01:00
|
|
|
data.retain();
|
2013-02-13 08:09:33 +01:00
|
|
|
return this;
|
2013-02-10 05:10:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-02-13 08:09:33 +01:00
|
|
|
public ByteBufHolder retain(int increment) {
|
2013-02-10 05:10:09 +01:00
|
|
|
data.retain(increment);
|
2013-02-13 08:09:33 +01:00
|
|
|
return this;
|
2013-02-10 05:10:09 +01:00
|
|
|
}
|
|
|
|
|
2014-01-28 12:00:28 +01:00
|
|
|
@Override
|
|
|
|
public ByteBufHolder touch() {
|
|
|
|
data.touch();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2014-01-29 03:44:59 +01:00
|
|
|
@Override
|
|
|
|
public ByteBufHolder touch(Object hint) {
|
|
|
|
data.touch(hint);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2013-02-10 05:10:09 +01:00
|
|
|
@Override
|
|
|
|
public boolean release() {
|
|
|
|
return data.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean release(int decrement) {
|
|
|
|
return data.release(decrement);
|
2013-01-01 16:03:18 +01:00
|
|
|
}
|
|
|
|
|
2015-10-07 15:30:34 +02:00
|
|
|
/**
|
2017-04-19 22:37:03 +02:00
|
|
|
* Return {@link ByteBuf#toString()} without checking the reference count first. This is useful to implement
|
2015-10-07 15:30:34 +02:00
|
|
|
* {@link #toString()}.
|
|
|
|
*/
|
|
|
|
protected final String contentToString() {
|
|
|
|
return data.toString();
|
|
|
|
}
|
|
|
|
|
2013-01-01 16:03:18 +01:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2015-10-07 15:30:34 +02:00
|
|
|
return StringUtil.simpleClassName(this) + '(' + contentToString() + ')';
|
2013-01-01 16:03:18 +01:00
|
|
|
}
|
2016-05-13 18:47:01 +02:00
|
|
|
|
2019-12-10 11:29:44 +01:00
|
|
|
/**
|
|
|
|
* This implementation of the {@code equals} operation is restricted to
|
|
|
|
* work only with instances of the same class. The reason for that is that
|
|
|
|
* Netty library already has a number of classes that extend {@link DefaultByteBufHolder} and
|
|
|
|
* override {@code equals} method with an additional comparison logic and we
|
|
|
|
* need the symmetric property of the {@code equals} operation to be preserved.
|
|
|
|
*
|
|
|
|
* @param o the reference object with which to compare.
|
|
|
|
* @return {@code true} if this object is the same as the obj
|
|
|
|
* argument; {@code false} otherwise.
|
|
|
|
*/
|
2016-05-13 18:47:01 +02:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (this == o) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-10 11:29:44 +01:00
|
|
|
if (o != null && getClass() == o.getClass()) {
|
|
|
|
return data.equals(((DefaultByteBufHolder) o).data);
|
2016-05-13 18:47:01 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return data.hashCode();
|
|
|
|
}
|
2013-01-01 16:03:18 +01:00
|
|
|
}
|