NettyDataBuffer.java 6.1 KB
Newer Older
A
Arjen Poutsma 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright 2002-2016 the original author or authors.
 *
 * 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 32 33 34

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

import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

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

43
	private final NettyDataBufferFactory dataBufferFactory;
44

A
Arjen Poutsma 已提交
45 46 47 48 49 50
	private ByteBuf byteBuf;

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

		this.byteBuf = byteBuf;
56
		this.dataBufferFactory = dataBufferFactory;
57 58 59
	}

	@Override
60 61
	public NettyDataBufferFactory factory() {
		return this.dataBufferFactory;
A
Arjen Poutsma 已提交
62 63 64 65 66 67 68 69 70 71 72
	}

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

	@Override
A
Arjen Poutsma 已提交
73 74 75 76 77 78 79 80 81 82 83
	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);
84 85 86
	}

	@Override
A
Arjen Poutsma 已提交
87 88 89 90 91 92 93 94
	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);

		return this.byteBuf.forEachByteDesc(0, fromIndex, predicate.negate()::test);
A
Arjen Poutsma 已提交
95 96 97 98 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 137 138 139 140
	}

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

	@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) {
		if (!ObjectUtils.isEmpty(buffers)) {
			if (buffers[0] instanceof NettyDataBuffer) {
141 142 143
				ByteBuf[] nativeBuffers = Arrays.stream(buffers)
						.map(b -> ((NettyDataBuffer) b).getNativeBuffer())
						.toArray(ByteBuf[]::new);
A
Arjen Poutsma 已提交
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 174 175 176 177 178 179

				write(nativeBuffers);
			}
			else {
				ByteBuffer[] byteBuffers =
						Arrays.stream(buffers).map(DataBuffer::asByteBuffer)
								.toArray(ByteBuffer[]::new);
				write(byteBuffers);
			}
		}
		return this;
	}

	@Override
	public NettyDataBuffer write(ByteBuffer... buffers) {
		Assert.notNull(buffers, "'buffers' must not be null");

		ByteBuf[] wrappedBuffers = Arrays.stream(buffers).map(Unpooled::wrappedBuffer)
				.toArray(ByteBuf[]::new);

		return write(wrappedBuffers);
	}

	/**
	 * 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");

		CompositeByteBuf composite =
				new CompositeByteBuf(this.byteBuf.alloc(), this.byteBuf.isDirect(),
						byteBufs.length + 1);
		composite.addComponent(this.byteBuf);
180
		composite.addComponents(byteBufs);
A
Arjen Poutsma 已提交
181 182 183 184 185 186 187 188 189 190

		int writerIndex = this.byteBuf.readableBytes() +
				Arrays.stream(byteBufs).mapToInt(ByteBuf::readableBytes).sum();
		composite.writerIndex(writerIndex);

		this.byteBuf = composite;

		return this;
	}

191 192 193
	@Override
	public DataBuffer slice(int index, int length) {
		ByteBuf slice = this.byteBuf.slice(index, length);
194
		return new NettyDataBuffer(slice, this.dataBufferFactory);
195 196
	}

A
Arjen Poutsma 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
	@Override
	public ByteBuffer asByteBuffer() {
		return this.byteBuf.nioBuffer();
	}

	@Override
	public InputStream asInputStream() {
		return new ByteBufInputStream(this.byteBuf);
	}

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

A
Arjen Poutsma 已提交
212 213
	@Override
	public PooledDataBuffer retain() {
214
		return new NettyDataBuffer(this.byteBuf.retain(), dataBufferFactory);
A
Arjen Poutsma 已提交
215 216 217 218 219 220 221
	}

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

A
Arjen Poutsma 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
	@Override
	public int hashCode() {
		return this.byteBuf.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		else if (obj instanceof NettyDataBuffer) {
			NettyDataBuffer other = (NettyDataBuffer) obj;
			return this.byteBuf.equals(other.byteBuf);
		}
		return false;
	}

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