Java Code Examples for org.springframework.core.io.buffer.DataBuffer#asByteBuffer()

The following examples show how to use org.springframework.core.io.buffer.DataBuffer#asByteBuffer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private ContentChunk toContentChunk(DataBuffer buffer) {
	return new ContentChunk(buffer.asByteBuffer(), new Callback() {
		@Override
		public void succeeded() {
			DataBufferUtils.release(buffer);
		}

		@Override
		public void failed(Throwable x) {
			DataBufferUtils.release(buffer);
			throw Exceptions.propagate(x);
		}
	});
}
 
Example 2
@Override
protected DataBuffer readFromInputStream() throws IOException {
	boolean release = true;
	int capacity = this.bufferSize;
	DataBuffer dataBuffer = this.factory.allocateBuffer(capacity);
	try {
		ByteBuffer byteBuffer = dataBuffer.asByteBuffer(0, capacity);
		ServletRequest request = getNativeRequest();
		int read = ((CoyoteInputStream) request.getInputStream()).read(byteBuffer);
		logBytesRead(read);
		if (read > 0) {
			dataBuffer.writePosition(read);
			release = false;
			return dataBuffer;
		}
		else if (read == -1) {
			return EOF_BUFFER;
		}
		else {
			return null;
		}
	}
	finally {
		if (release) {
			DataBufferUtils.release(dataBuffer);
		}
	}
}
 
Example 3
@Override
protected int writeToOutputStream(DataBuffer dataBuffer) throws IOException {
	ByteBuffer input = dataBuffer.asByteBuffer();
	int len = input.remaining();
	ServletResponse response = getNativeResponse();
	((CoyoteOutputStream) response.getOutputStream()).write(input);
	return len;
}
 
Example 4
@Override
protected int writeToOutputStream(DataBuffer dataBuffer) throws IOException {
	ByteBuffer input = dataBuffer.asByteBuffer();
	int len = input.remaining();
	ServletResponse response = getNativeResponse();
	((HttpOutput) response.getOutputStream()).write(input);
	return len;
}
 
Example 5
private ContentChunk toContentChunk(DataBuffer buffer) {
	return new ContentChunk(buffer.asByteBuffer(), new Callback() {
		@Override
		public void succeeded() {
			DataBufferUtils.release(buffer);
		}

		@Override
		public void failed(Throwable x) {
			DataBufferUtils.release(buffer);
			throw Exceptions.propagate(x);
		}
	});
}
 
Example 6
@Override
protected DataBuffer readFromInputStream() throws IOException {
	boolean release = true;
	int capacity = this.bufferSize;
	DataBuffer dataBuffer = this.factory.allocateBuffer(capacity);
	try {
		ByteBuffer byteBuffer = dataBuffer.asByteBuffer(0, capacity);
		ServletRequest request = getNativeRequest();
		int read = ((CoyoteInputStream) request.getInputStream()).read(byteBuffer);
		logBytesRead(read);
		if (read > 0) {
			dataBuffer.writePosition(read);
			release = false;
			return dataBuffer;
		}
		else if (read == -1) {
			return EOF_BUFFER;
		}
		else {
			return null;
		}
	}
	finally {
		if (release) {
			DataBufferUtils.release(dataBuffer);
		}
	}
}
 
Example 7
@Override
protected int writeToOutputStream(DataBuffer dataBuffer) throws IOException {
	ByteBuffer input = dataBuffer.asByteBuffer();
	int len = input.remaining();
	ServletResponse response = getNativeResponse();
	((CoyoteOutputStream) response.getOutputStream()).write(input);
	return len;
}
 
Example 8
@Override
protected int writeToOutputStream(DataBuffer dataBuffer) throws IOException {
	ByteBuffer input = dataBuffer.asByteBuffer();
	int len = input.remaining();
	ServletResponse response = getNativeResponse();
	((HttpOutput) response.getOutputStream()).write(input);
	return len;
}
 
Example 9
/**
 * Converts a {@link DataBuffer} into an {@link HttpData}.
 */
HttpData toHttpData(DataBuffer dataBuffer) {
    if (dataBuffer instanceof NettyDataBuffer) {
        return PooledHttpData.wrap((((NettyDataBuffer) dataBuffer).getNativeBuffer()));
    }
    final ByteBuffer buf =
            dataBuffer instanceof DefaultDataBuffer ? ((DefaultDataBuffer) dataBuffer).getNativeBuffer()
                                                    : dataBuffer.asByteBuffer();
    return PooledHttpData.wrap(Unpooled.wrappedBuffer(buf));
}
 
Example 10
private static String asString(DataBuffer dataBuffer) {
	ByteBuffer byteBuffer = dataBuffer.asByteBuffer();
	final byte[] bytes = new byte[byteBuffer.remaining()];
	byteBuffer.get(bytes);
	return new String(bytes, StandardCharsets.UTF_8);
}
 
Example 11
@Override
protected void dataReceived(DataBuffer dataBuffer) {
	super.dataReceived(dataBuffer);
	this.byteBuffer = dataBuffer.asByteBuffer();
}
 
Example 12
private static String asString(DataBuffer dataBuffer) {
	ByteBuffer byteBuffer = dataBuffer.asByteBuffer();
	final byte[] bytes = new byte[byteBuffer.remaining()];
	byteBuffer.get(bytes);
	return new String(bytes, StandardCharsets.UTF_8);
}
 
Example 13
@Override
protected void dataReceived(DataBuffer dataBuffer) {
	super.dataReceived(dataBuffer);
	this.byteBuffer = dataBuffer.asByteBuffer();
}