Java Code Examples for org.springframework.core.io.buffer.DataBufferFactory#allocateBuffer()

The following examples show how to use org.springframework.core.io.buffer.DataBufferFactory#allocateBuffer() . 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: ProtobufEncoder.java    From java-technology-stack with MIT License 6 votes vote down vote up
private DataBuffer encodeMessage(Message message, DataBufferFactory bufferFactory, boolean streaming) {
	DataBuffer buffer = bufferFactory.allocateBuffer();
	OutputStream outputStream = buffer.asOutputStream();
	try {
		if (streaming) {
			message.writeDelimitedTo(outputStream);
		}
		else {
			message.writeTo(outputStream);
		}
		return buffer;
	}
	catch (IOException ex) {
		throw new IllegalStateException("Unexpected I/O error while writing to data buffer", ex);
	}
}
 
Example 3
Source File: CharSequenceEncoder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public DataBuffer encodeValue(CharSequence charSequence, DataBufferFactory bufferFactory,
		ResolvableType valueType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

	if (!Hints.isLoggingSuppressed(hints)) {
		LogFormatUtils.traceDebug(logger, traceOn -> {
			String formatted = LogFormatUtils.formatValue(charSequence, !traceOn);
			return Hints.getLogPrefix(hints) + "Writing " + formatted;
		});
	}
	boolean release = true;
	Charset charset = getCharset(mimeType);
	int capacity = calculateCapacity(charSequence, charset);
	DataBuffer dataBuffer = bufferFactory.allocateBuffer(capacity);
	try {
		dataBuffer.write(charSequence, charset);
		release = false;
	}
	catch (CoderMalfunctionError ex) {
		throw new EncodingException("String encoding error: " + ex.getMessage(), ex);
	}
	finally {
		if (release) {
			DataBufferUtils.release(dataBuffer);
		}
	}
	return dataBuffer;
}
 
Example 4
Source File: HessianEncoder.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory, ResolvableType valueType, MimeType mimeType, Map<String, Object> hints) {
    if (value == null) {
        return bufferFactory.allocateBuffer(0);
    }
    try {
        return encode(value, bufferFactory);
    } catch (Exception e) {
        return bufferFactory.allocateBuffer(0);
    }
}
 
Example 5
Source File: HessianCodecSupport.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
public DataBuffer encode(Object obj, DataBufferFactory bufferFactory) throws Exception {
    DataBuffer dataBuffer = bufferFactory.allocateBuffer();
    HessianSerializerOutput output = new HessianSerializerOutput(dataBuffer.asOutputStream());
    output.writeObject(obj);
    output.flush();
    return dataBuffer;
}
 
Example 6
Source File: ApiVersionWebFilter.java    From spring-cloud-open-service-broker with Apache License 2.0 4 votes vote down vote up
private DataBuffer toDataBuffer(String value, DataBufferFactory factory) {
	byte[] data = value.getBytes(StandardCharsets.UTF_8);
	DataBuffer buffer = factory.allocateBuffer(data.length);
	buffer.write(data);
	return buffer;
}