Java Code Examples for org.springframework.core.io.buffer.DataBufferUtils#release()

The following examples show how to use org.springframework.core.io.buffer.DataBufferUtils#release() . 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: ProtobufEncoder.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private DataBuffer encodeValue(Message message, DataBufferFactory bufferFactory, boolean delimited) {

		DataBuffer buffer = bufferFactory.allocateBuffer();
		boolean release = true;
		try {
			if (delimited) {
				message.writeDelimitedTo(buffer.asOutputStream());
			}
			else {
				message.writeTo(buffer.asOutputStream());
			}
			release = false;
			return buffer;
		}
		catch (IOException ex) {
			throw new IllegalStateException("Unexpected I/O error while writing to data buffer", ex);
		}
		finally {
			if (release) {
				DataBufferUtils.release(buffer);
			}
		}
	}
 
Example 2
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 3
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 4
Source File: UndertowServerHttpResponse.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected boolean write(DataBuffer dataBuffer) throws IOException {
	ByteBuffer buffer = this.byteBuffer;
	if (buffer == null) {
		return false;
	}

	// Track write listener calls from here on..
	this.writePossible = false;

	// In case of IOException, onError handling should call discardData(DataBuffer)..
	int total = buffer.remaining();
	int written = writeByteBuffer(buffer);

	if (logger.isTraceEnabled()) {
		logger.trace(getLogPrefix() + "Wrote " + written + " of " + total + " bytes");
	}
	else if (rsWriteLogger.isTraceEnabled()) {
		rsWriteLogger.trace(getLogPrefix() + "Wrote " + written + " of " + total + " bytes");
	}
	if (written != total) {
		return false;
	}

	// We wrote all, so can still write more..
	this.writePossible = true;

	DataBufferUtils.release(dataBuffer);
	this.byteBuffer = null;
	return true;
}
 
Example 5
Source File: ServletServerHttpResponse.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected boolean write(DataBuffer dataBuffer) throws IOException {
	if (ServletServerHttpResponse.this.flushOnNext) {
		if (rsWriteLogger.isTraceEnabled()) {
			rsWriteLogger.trace(getLogPrefix() + "Flush attempt");
		}
		flush();
	}

	boolean ready = ServletServerHttpResponse.this.isWritePossible();
	int remaining = dataBuffer.readableByteCount();
	if (ready && remaining > 0) {
		// In case of IOException, onError handling should call discardData(DataBuffer)..
		int written = writeToOutputStream(dataBuffer);
		if (logger.isTraceEnabled()) {
			logger.trace(getLogPrefix() + "Wrote " + written + " of " + remaining + " bytes");
		}
		else if (rsWriteLogger.isTraceEnabled()) {
			rsWriteLogger.trace(getLogPrefix() + "Wrote " + written + " of " + remaining + " bytes");
		}
		if (written == remaining) {
			DataBufferUtils.release(dataBuffer);
			return true;
		}
	}
	else {
		if (rsWriteLogger.isTraceEnabled()) {
			rsWriteLogger.trace(getLogPrefix() + "ready: " + ready + ", remaining: " + remaining);
		}
	}

	return false;
}
 
Example 6
Source File: ResourceDecoder.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected Resource decodeDataBuffer(DataBuffer dataBuffer, ResolvableType elementType,
		@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

	byte[] bytes = new byte[dataBuffer.readableByteCount()];
	dataBuffer.read(bytes);
	DataBufferUtils.release(dataBuffer);

	if (logger.isDebugEnabled()) {
		logger.debug(Hints.getLogPrefix(hints) + "Read " + bytes.length + " bytes");
	}

	Class<?> clazz = elementType.toClass();
	if (clazz == InputStreamResource.class) {
		return new InputStreamResource(new ByteArrayInputStream(bytes));
	}
	else if (Resource.class.isAssignableFrom(clazz)) {
		return new ByteArrayResource(bytes);
	}
	else {
		throw new IllegalStateException("Unsupported resource class: " + clazz);
	}
}
 
Example 7
Source File: ChannelSendOperator.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void releaseCachedItem() {
	synchronized (this) {
		Object item = this.item;
		if (item instanceof DataBuffer) {
			DataBufferUtils.release((DataBuffer) item);
		}
		this.item = null;
	}
}
 
Example 8
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 9
Source File: DataBufferDecoderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private Consumer<DataBuffer> expectDataBuffer(byte[] expected) {
	return actual -> {
		byte[] actualBytes = new byte[actual.readableByteCount()];
		actual.read(actualBytes);
		assertArrayEquals(expected, actualBytes);

		DataBufferUtils.release(actual);
	};
}
 
Example 10
Source File: ByteArrayDecoder.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected byte[] decodeDataBuffer(DataBuffer dataBuffer, ResolvableType elementType,
		@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

	byte[] result = new byte[dataBuffer.readableByteCount()];
	dataBuffer.read(result);
	DataBufferUtils.release(dataBuffer);
	if (logger.isDebugEnabled()) {
		logger.debug(Hints.getLogPrefix(hints) + "Read " + result.length + " bytes");
	}
	return result;
}
 
Example 11
Source File: DefaultMultipartMessageReader.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Indicates whether the given data buffer is not the last boundary, i.e. it does not start
 * with two hyphens.
 */
private static boolean notLastBoundary(DataBuffer dataBuffer) {
	if (dataBuffer.readableByteCount() >= 2) {
		int readPosition = dataBuffer.readPosition();
		if (dataBuffer.getByte(readPosition) == HYPHEN && dataBuffer.getByte(readPosition + 1) == HYPHEN) {
			DataBufferUtils.release(dataBuffer);
			return false;
		}
	}
	return true;
}
 
Example 12
Source File: StringDecoder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public String decode(DataBuffer dataBuffer, ResolvableType elementType,
		@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

	Charset charset = getCharset(mimeType);
	CharBuffer charBuffer = charset.decode(dataBuffer.asByteBuffer());
	DataBufferUtils.release(dataBuffer);
	String value = charBuffer.toString();
	LogFormatUtils.traceDebug(logger, traceOn -> {
		String formatted = LogFormatUtils.formatValue(value, !traceOn);
		return Hints.getLogPrefix(hints) + "Decoded " + formatted;
	});
	return value;
}
 
Example 13
Source File: ProtobufEncoderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
protected final Consumer<DataBuffer> expect(Msg msg) {
	return dataBuffer -> {
		try {
			assertEquals(msg, Msg.parseDelimitedFrom(dataBuffer.asInputStream()));

		}
		catch (IOException ex) {
			throw new UncheckedIOException(ex);
		}
		finally {
			DataBufferUtils.release(dataBuffer);
		}
	};
}
 
Example 14
Source File: StringDecoder.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Split the given data buffer on delimiter boundaries.
 * The returned Flux contains an {@link #END_FRAME} buffer after each delimiter.
 */
private List<DataBuffer> splitOnDelimiter(DataBuffer dataBuffer, List<byte[]> delimiterBytes) {
	List<DataBuffer> frames = new ArrayList<>();
	do {
		int length = Integer.MAX_VALUE;
		byte[] matchingDelimiter = null;
		for (byte[] delimiter : delimiterBytes) {
			int index = indexOf(dataBuffer, delimiter);
			if (index >= 0 && index < length) {
				length = index;
				matchingDelimiter = delimiter;
			}
		}
		DataBuffer frame;
		int readPosition = dataBuffer.readPosition();
		if (matchingDelimiter != null) {
			if (this.stripDelimiter) {
				frame = dataBuffer.slice(readPosition, length);
			}
			else {
				frame = dataBuffer.slice(readPosition, length + matchingDelimiter.length);
			}
			dataBuffer.readPosition(readPosition + length + matchingDelimiter.length);

			frames.add(DataBufferUtils.retain(frame));
			frames.add(END_FRAME);
		}
		else {
			frame = dataBuffer.slice(readPosition, dataBuffer.readableByteCount());
			dataBuffer.readPosition(readPosition + dataBuffer.readableByteCount());
			frames.add(DataBufferUtils.retain(frame));
		}
	}
	while (dataBuffer.readableByteCount() > 0);

	DataBufferUtils.release(dataBuffer);
	return frames;
}
 
Example 15
Source File: UndertowServerHttpRequest.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public boolean release() {
	boolean result;
	try {
		result = DataBufferUtils.release(this.dataBuffer);
	}
	finally {
		this.pooledByteBuffer.close();
	}
	return result && this.pooledByteBuffer.isOpen();
}
 
Example 16
Source File: XmlEventDecoder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public List<? extends XMLEvent> apply(DataBuffer dataBuffer) {
	try {
		this.streamReader.getInputFeeder().feedInput(dataBuffer.asByteBuffer());
		List<XMLEvent> events = new ArrayList<>();
		while (true) {
			if (this.streamReader.next() == AsyncXMLStreamReader.EVENT_INCOMPLETE) {
				// no more events with what currently has been fed to the reader
				break;
			}
			else {
				XMLEvent event = this.eventAllocator.allocate(this.streamReader);
				events.add(event);
				if (event.isEndDocument()) {
					break;
				}
			}
		}
		return events;
	}
	catch (XMLStreamException ex) {
		throw Exceptions.propagate(ex);
	}
	finally {
		DataBufferUtils.release(dataBuffer);
	}
}
 
Example 17
Source File: ExchangeFilterFunctionsTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private String string(DataBuffer buffer) {
	String value = DataBufferTestUtils.dumpString(buffer, StandardCharsets.UTF_8);
	DataBufferUtils.release(buffer);
	return value;
}
 
Example 18
Source File: ServletServerHttpResponse.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
protected void discardData(DataBuffer dataBuffer) {
	DataBufferUtils.release(dataBuffer);
}
 
Example 19
Source File: ProtobufDecoder.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public void discard() {
	if (this.output != null) {
		DataBufferUtils.release(this.output);
	}
}
 
Example 20
Source File: UndertowServerHttpResponse.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected void discardData(DataBuffer dataBuffer) {
	DataBufferUtils.release(dataBuffer);
}