NettyDataBuffer.java 6.7 KB
Newer Older
A
Arjen Poutsma 已提交
1
/*
2
 * Copyright 2002-2018 the original author or authors.
A
Arjen Poutsma 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Licensed 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
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 */

package org.springframework.core.io.buffer;

import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;
23
import java.util.function.IntPredicate;
A
Arjen Poutsma 已提交
24 25 26 27 28 29 30 31

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;

import org.springframework.util.Assert;

/**
32 33
 * Implementation of the {@code DataBuffer} interface that wraps a Netty
 * {@link ByteBuf}. Typically constructed with {@link NettyDataBufferFactory}.
A
Arjen Poutsma 已提交
34 35
 *
 * @author Arjen Poutsma
36
 * @since 5.0
A
Arjen Poutsma 已提交
37
 */
A
Arjen Poutsma 已提交
38
public class NettyDataBuffer implements PooledDataBuffer {
A
Arjen Poutsma 已提交
39

40
	private final NettyDataBufferFactory dataBufferFactory;
41

42
	private final ByteBuf byteBuf;
A
Arjen Poutsma 已提交
43

44

A
Arjen Poutsma 已提交
45 46 47 48
	/**
	 * Creates a new {@code NettyDataBuffer} based on the given {@code ByteBuff}.
	 * @param byteBuf the buffer to base this buffer on
	 */
49
	NettyDataBuffer(ByteBuf byteBuf, NettyDataBufferFactory dataBufferFactory) {
A
Arjen Poutsma 已提交
50
		Assert.notNull(byteBuf, "'byteBuf' must not be null");
51
		Assert.notNull(dataBufferFactory, "'dataBufferFactory' must not be null");
A
Arjen Poutsma 已提交
52 53

		this.byteBuf = byteBuf;
54
		this.dataBufferFactory = dataBufferFactory;
55 56
	}

A
Arjen Poutsma 已提交
57 58 59 60 61 62 63 64 65

	/**
	 * Directly exposes the native {@code ByteBuf} that this buffer is based on.
	 * @return the wrapped byte buffer
	 */
	public ByteBuf getNativeBuffer() {
		return this.byteBuf;
	}

66 67 68 69 70
	@Override
	public NettyDataBufferFactory factory() {
		return this.dataBufferFactory;
	}

A
Arjen Poutsma 已提交
71
	@Override
A
Arjen Poutsma 已提交
72 73 74 75 76 77 78 79 80 81
	public int indexOf(IntPredicate predicate, int fromIndex) {
		Assert.notNull(predicate, "'predicate' must not be null");
		if (fromIndex < 0) {
			fromIndex = 0;
		}
		else if (fromIndex >= this.byteBuf.writerIndex()) {
			return -1;
		}
		int length = this.byteBuf.writerIndex() - fromIndex;
		return this.byteBuf.forEachByte(fromIndex, length, predicate.negate()::test);
82 83 84
	}

	@Override
A
Arjen Poutsma 已提交
85 86 87 88 89 90
	public int lastIndexOf(IntPredicate predicate, int fromIndex) {
		Assert.notNull(predicate, "'predicate' must not be null");
		if (fromIndex < 0) {
			return -1;
		}
		fromIndex = Math.min(fromIndex, this.byteBuf.writerIndex() - 1);
91
		return this.byteBuf.forEachByteDesc(0, fromIndex + 1, predicate.negate()::test);
A
Arjen Poutsma 已提交
92 93 94 95 96 97 98
	}

	@Override
	public int readableByteCount() {
		return this.byteBuf.readableBytes();
	}

A
Arjen Poutsma 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	@Override
	public int writableByteCount() {
		return this.byteBuf.writableBytes();
	}

	@Override
	public int readPosition() {
		return this.byteBuf.readerIndex();
	}

	@Override
	public DataBuffer readPosition(int readPosition) {
		this.byteBuf.readerIndex(readPosition);
		return this;
	}

	@Override
	public int writePosition() {
		return this.byteBuf.writerIndex();
	}

	@Override
	public DataBuffer writePosition(int writePosition) {
		this.byteBuf.writerIndex(writePosition);
		return this;
	}

	@Override
	public int capacity() {
		return this.byteBuf.capacity();
	}

	@Override
	public DataBuffer capacity(int capacity) {
		this.byteBuf.capacity(capacity);
		return this;
	}

A
Arjen Poutsma 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
	@Override
	public byte read() {
		return this.byteBuf.readByte();
	}

	@Override
	public NettyDataBuffer read(byte[] destination) {
		this.byteBuf.readBytes(destination);
		return this;
	}

	@Override
	public NettyDataBuffer read(byte[] destination, int offset, int length) {
		this.byteBuf.readBytes(destination, offset, length);
		return this;
	}

	@Override
	public NettyDataBuffer write(byte b) {
		this.byteBuf.writeByte(b);
		return this;
	}

	@Override
	public NettyDataBuffer write(byte[] source) {
		this.byteBuf.writeBytes(source);
		return this;
	}

	@Override
	public NettyDataBuffer write(byte[] source, int offset, int length) {
		this.byteBuf.writeBytes(source, offset, length);
		return this;
	}

	@Override
	public NettyDataBuffer write(DataBuffer... buffers) {
174 175 176 177
		Assert.notNull(buffers, "'buffers' must not be null");

		if (buffers.length > 0) {
			if (hasNettyDataBuffers(buffers)) {
178 179 180
				ByteBuf[] nativeBuffers = Arrays.stream(buffers)
						.map(b -> ((NettyDataBuffer) b).getNativeBuffer())
						.toArray(ByteBuf[]::new);
A
Arjen Poutsma 已提交
181 182 183 184 185 186 187 188 189 190 191 192
				write(nativeBuffers);
			}
			else {
				ByteBuffer[] byteBuffers =
						Arrays.stream(buffers).map(DataBuffer::asByteBuffer)
								.toArray(ByteBuffer[]::new);
				write(byteBuffers);
			}
		}
		return this;
	}

193 194 195 196 197 198 199 200 201
	private static boolean hasNettyDataBuffers(DataBuffer[] dataBuffers) {
		for (DataBuffer dataBuffer : dataBuffers) {
			if (!(dataBuffer instanceof NettyDataBuffer)) {
				return false;
			}
		}
		return true;
	}

A
Arjen Poutsma 已提交
202 203 204
	@Override
	public NettyDataBuffer write(ByteBuffer... buffers) {
		Assert.notNull(buffers, "'buffers' must not be null");
205 206 207 208 209

		for (ByteBuffer buffer : buffers) {
			this.byteBuf.writeBytes(buffer);
		}
		return this;
A
Arjen Poutsma 已提交
210 211 212 213 214 215 216 217 218 219 220
	}

	/**
	 * Writes one or more Netty {@link ByteBuf}s to this buffer, starting at the current
	 * writing position.
	 * @param byteBufs the buffers to write into this buffer
	 * @return this buffer
	 */
	public NettyDataBuffer write(ByteBuf... byteBufs) {
		Assert.notNull(byteBufs, "'byteBufs' must not be null");

221 222
		for (ByteBuf byteBuf : byteBufs) {
			this.byteBuf.writeBytes(byteBuf);
223
		}
A
Arjen Poutsma 已提交
224 225 226
		return this;
	}

227 228 229
	@Override
	public DataBuffer slice(int index, int length) {
		ByteBuf slice = this.byteBuf.slice(index, length);
230
		return new NettyDataBuffer(slice, this.dataBufferFactory);
231 232
	}

A
Arjen Poutsma 已提交
233 234 235 236 237
	@Override
	public ByteBuffer asByteBuffer() {
		return this.byteBuf.nioBuffer();
	}

A
Arjen Poutsma 已提交
238 239 240 241 242
	@Override
	public ByteBuffer asByteBuffer(int index, int length) {
		return this.byteBuf.nioBuffer(index, length);
	}

A
Arjen Poutsma 已提交
243 244 245 246 247 248 249 250 251 252
	@Override
	public InputStream asInputStream() {
		return new ByteBufInputStream(this.byteBuf);
	}

	@Override
	public OutputStream asOutputStream() {
		return new ByteBufOutputStream(this.byteBuf);
	}

A
Arjen Poutsma 已提交
253 254
	@Override
	public PooledDataBuffer retain() {
255
		return new NettyDataBuffer(this.byteBuf.retain(), this.dataBufferFactory);
A
Arjen Poutsma 已提交
256 257 258 259 260 261 262
	}

	@Override
	public boolean release() {
		return this.byteBuf.release();
	}

A
Arjen Poutsma 已提交
263 264 265 266 267 268

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
269 270
		if (!(obj instanceof NettyDataBuffer)) {
			return false;
A
Arjen Poutsma 已提交
271
		}
272 273 274 275 276 277 278
		NettyDataBuffer other = (NettyDataBuffer) obj;
		return this.byteBuf.equals(other.byteBuf);
	}

	@Override
	public int hashCode() {
		return this.byteBuf.hashCode();
A
Arjen Poutsma 已提交
279 280 281 282 283 284
	}

	@Override
	public String toString() {
		return this.byteBuf.toString();
	}
285

A
Arjen Poutsma 已提交
286
}