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

The following examples show how to use org.springframework.core.io.buffer.DataBuffer#write() . 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 DataBuffer generateHeaders() {
	DataBuffer buffer = this.bufferFactory.allocateBuffer();
	for (Map.Entry<String, List<String>> entry : this.headers.entrySet()) {
		byte[] headerName = entry.getKey().getBytes(this.charset);
		for (String headerValueString : entry.getValue()) {
			byte[] headerValue = headerValueString.getBytes(this.charset);
			buffer.write(headerName);
			buffer.write((byte)':');
			buffer.write((byte)' ');
			buffer.write(headerValue);
			buffer.write((byte)'\r');
			buffer.write((byte)'\n');
		}
	}
	buffer.write((byte)'\r');
	buffer.write((byte)'\n');
	return buffer;
}
 
Example 2
/**
 * Read from the request body InputStream and return a DataBuffer.
 * Invoked only when {@link ServletInputStream#isReady()} returns "true".
 * @return a DataBuffer with data read, or {@link #EOF_BUFFER} if the input
 * stream returned -1, or null if 0 bytes were read.
 */
@Nullable
DataBuffer readFromInputStream() throws IOException {
	int read = this.request.getInputStream().read(this.buffer);
	logBytesRead(read);

	if (read > 0) {
		DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(read);
		dataBuffer.write(this.buffer, 0, read);
		return dataBuffer;
	}

	if (read == -1) {
		return EOF_BUFFER;
	}

	return null;
}
 
Example 3
/**
 * Read from the request body InputStream and return a DataBuffer.
 * Invoked only when {@link ServletInputStream#isReady()} returns "true".
 * @return a DataBuffer with data read, or {@link #EOF_BUFFER} if the input
 * stream returned -1, or null if 0 bytes were read.
 */
@Nullable
DataBuffer readFromInputStream() throws IOException {
	int read = this.request.getInputStream().read(this.buffer);
	logBytesRead(read);

	if (read > 0) {
		DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(read);
		dataBuffer.write(this.buffer, 0, read);
		return dataBuffer;
	}

	if (read == -1) {
		return EOF_BUFFER;
	}

	return null;
}
 
Example 4
@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 5
@Override
public Flux<DataBuffer> content() {
	byte[] bytes = this.content.getBytes(getCharset());
	DataBuffer buffer = getBufferFactory().allocateBuffer(bytes.length);
	buffer.write(bytes);
	return Flux.just(buffer);
}
 
Example 6
@Test
public void dumpString() {
	DataBuffer buffer = this.bufferFactory.allocateBuffer(4);
	String source = "abcd";
	buffer.write(source.getBytes(StandardCharsets.UTF_8));

	String result = DataBufferTestUtils.dumpString(buffer, StandardCharsets.UTF_8);

	assertEquals(source, result);

	release(buffer);
}
 
Example 7
private DataBuffer toDataBuffer(ContentChunk chunk) {

		// We must copy until this is resolved:
		// https://github.com/eclipse/jetty.project/issues/2429

		// Use copy instead of buffer wrapping because Callback#succeeded() is
		// used not only to release the buffer but also to request more data
		// which is a problem for codecs that buffer data.

		DataBuffer buffer = this.bufferFactory.allocateBuffer(chunk.buffer.capacity());
		buffer.write(chunk.buffer);
		chunk.callback.succeeded();
		return buffer;
	}
 
Example 8
@Override
public Flux<DataBuffer> content() {
	byte[] bytes = this.content.getBytes(getCharset());
	DataBuffer buffer = getBufferFactory().allocateBuffer(bytes.length);
	buffer.write(bytes);
	return Flux.just(buffer);
}
 
Example 9
private DataBuffer randomBuffer(int size) {
	byte[] bytes = new byte[size];
	rnd.nextBytes(bytes);
	DataBuffer buffer = dataBufferFactory.allocateBuffer(size);
	buffer.write(bytes);
	return buffer;
}
 
Example 10
private DataBuffer generateLastLine(byte[] boundary) {
	DataBuffer buffer = this.bufferFactory.allocateBuffer(boundary.length + 6);
	buffer.write((byte)'-');
	buffer.write((byte)'-');
	buffer.write(boundary);
	buffer.write((byte)'-');
	buffer.write((byte)'-');
	buffer.write((byte)'\r');
	buffer.write((byte)'\n');
	return buffer;
}
 
Example 11
@Test
public void dumpBytes() {
	DataBuffer buffer = this.bufferFactory.allocateBuffer(4);
	byte[] source = {'a', 'b', 'c', 'd'};
	buffer.write(source);

	byte[] result = DataBufferTestUtils.dumpBytes(buffer);

	assertArrayEquals(source, result);

	release(buffer);
}
 
Example 12
private DataBuffer toDataBuffer(ContentChunk chunk) {

		// We must copy until this is resolved:
		// https://github.com/eclipse/jetty.project/issues/2429

		// Use copy instead of buffer wrapping because Callback#succeeded() is
		// used not only to release the buffer but also to request more data
		// which is a problem for codecs that buffer data.

		DataBuffer buffer = this.bufferFactory.allocateBuffer(chunk.buffer.capacity());
		buffer.write(chunk.buffer);
		chunk.callback.succeeded();
		return buffer;
	}
 
Example 13
private DataBuffer generateBoundaryLine(byte[] boundary) {
	DataBuffer buffer = this.bufferFactory.allocateBuffer(boundary.length + 4);
	buffer.write((byte)'-');
	buffer.write((byte)'-');
	buffer.write(boundary);
	buffer.write((byte)'\r');
	buffer.write((byte)'\n');
	return buffer;
}
 
Example 14
@Override
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
	DataBuffer buffer = response.bufferFactory().allocateBuffer(body.length);
	buffer.write(body);
	return response.writeAndFlushWith(Flux.just(Flux.just(buffer)));
}
 
Example 15
private DataBuffer stringBuffer(String value) {
	byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
	DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length);
	buffer.write(bytes);
	return buffer;
}
 
Example 16
private DataBuffer stringBuffer(String value) {
	byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
	DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length);
	buffer.write(bytes);
	return buffer;
}
 
Example 17
private DataBuffer stringBuffer(String value) {
	byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
	DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length);
	buffer.write(bytes);
	return buffer;
}
 
Example 18
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;
}
 
Example 19
private DataBuffer stringBuffer(String value) {
	byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
	DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length);
	buffer.write(bytes);
	return buffer;
}
 
Example 20
private DataBuffer stringBuffer(String value) {
	byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
	DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length);
	buffer.write(bytes);
	return buffer;
}