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
Source File: JettyClientHttpRequest.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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
Source File: TomcatHttpHandlerAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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
Source File: TomcatHttpHandlerAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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
Source File: JettyHttpHandlerAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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
Source File: JettyClientHttpRequest.java    From java-technology-stack with MIT License 5 votes vote down vote up
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
Source File: TomcatHttpHandlerAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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
Source File: TomcatHttpHandlerAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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
Source File: JettyHttpHandlerAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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
Source File: DataBufferFactoryWrapper.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * 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
Source File: FreeMarkerViewTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
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
Source File: UndertowServerHttpResponse.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected void dataReceived(DataBuffer dataBuffer) {
	super.dataReceived(dataBuffer);
	this.byteBuffer = dataBuffer.asByteBuffer();
}
 
Example 12
Source File: FreeMarkerViewTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
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
Source File: UndertowServerHttpResponse.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
protected void dataReceived(DataBuffer dataBuffer) {
	super.dataReceived(dataBuffer);
	this.byteBuffer = dataBuffer.asByteBuffer();
}