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

The following examples show how to use org.springframework.core.io.buffer.DataBuffer#readableByteCount() . 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: DefaultMultipartMessageReader.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Convert the given data buffer into a {@link HttpHeaders} instance. The given string is read
 * as US-ASCII, then split along \r\n line boundaries, each line containing a header name and
 * value(s).
 */
private static HttpHeaders toHeaders(DataBuffer dataBuffer) {
	byte[] bytes = new byte[dataBuffer.readableByteCount()];
	dataBuffer.read(bytes);
	DataBufferUtils.release(dataBuffer);
	String string = new String(bytes, StandardCharsets.US_ASCII);
	String[] lines = string.split(HEADER_SEPARATOR);
	HttpHeaders result = new HttpHeaders();
	for (String line : lines) {
		int idx = line.indexOf(':');
		if (idx != -1) {
			String name = line.substring(0, idx);
			String value = line.substring(idx + 1);
			while (value.startsWith(" ")) {
				value = value.substring(1);
			}
			String[] tokens = StringUtils.tokenizeToStringArray(value, ",");
			for (String token : tokens) {
				result.add(name, token);
			}
		}
	}
	return result;
}
 
Example 2
Source File: ByteBufferDecoder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public ByteBuffer decode(DataBuffer dataBuffer, ResolvableType elementType,
		@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

	int byteCount = dataBuffer.readableByteCount();
	ByteBuffer copy = ByteBuffer.allocate(byteCount);
	copy.put(dataBuffer.asByteBuffer());
	copy.flip();
	DataBufferUtils.release(dataBuffer);
	if (logger.isDebugEnabled()) {
		logger.debug(Hints.getLogPrefix(hints) + "Read " + byteCount + " bytes");
	}
	return copy;
}
 
Example 3
Source File: ResourceDecoder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Resource decode(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();
	String filename = hints != null ? (String) hints.get(FILENAME_HINT) : null;
	if (clazz == InputStreamResource.class) {
		return new InputStreamResource(new ByteArrayInputStream(bytes)) {
			@Override
			public String getFilename() {
				return filename;
			}
		};
	}
	else if (Resource.class.isAssignableFrom(clazz)) {
		return new ByteArrayResource(bytes) {
			@Override
			public String getFilename() {
				return filename;
			}
		};
	}
	else {
		throw new IllegalStateException("Unsupported resource class: " + clazz);
	}
}
 
Example 4
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 5
Source File: MockServerHttpResponse.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private static String bufferToString(DataBuffer buffer, Charset charset) {
	Assert.notNull(charset, "'charset' must not be null");
	byte[] bytes = new byte[buffer.readableByteCount()];
	buffer.read(bytes);
	DataBufferUtils.release(buffer);
	return new String(bytes, charset);
}
 
Example 6
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 7
Source File: DefaultMultipartMessageReader.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private static String toString(DataBuffer dataBuffer) {
	byte[] bytes = new byte[dataBuffer.readableByteCount()];
	int j = 0;
	for (int i = dataBuffer.readPosition(); i < dataBuffer.writePosition(); i++) {
		bytes[j++] = dataBuffer.getByte(i);
	}
	return toString(bytes);
}
 
Example 8
Source File: MockClientHttpRequest.java    From java-technology-stack with MIT License 4 votes vote down vote up
private static String bufferToString(DataBuffer buffer, Charset charset) {
	Assert.notNull(charset, "'charset' must not be null");
	byte[] bytes = new byte[buffer.readableByteCount()];
	buffer.read(bytes);
	return new String(bytes, charset);
}
 
Example 9
Source File: TestUtil.java    From armeria with Apache License 2.0 4 votes vote down vote up
static String bufferToString(DataBuffer buffer) {
    final byte[] bytes = new byte[buffer.readableByteCount()];
    buffer.read(bytes);
    return new String(bytes);
}
 
Example 10
Source File: MockClientHttpResponse.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private static String dumpString(DataBuffer buffer, Charset charset) {
	Assert.notNull(charset, "'charset' must not be null");
	byte[] bytes = new byte[buffer.readableByteCount()];
	buffer.read(bytes);
	return new String(bytes, charset);
}
 
Example 11
Source File: MockClientHttpRequest.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private static String bufferToString(DataBuffer buffer, Charset charset) {
	Assert.notNull(charset, "'charset' must not be null");
	byte[] bytes = new byte[buffer.readableByteCount()];
	buffer.read(bytes);
	return new String(bytes, charset);
}
 
Example 12
Source File: MockClientHttpRequest.java    From java-technology-stack with MIT License 4 votes vote down vote up
private static String bufferToString(DataBuffer buffer, Charset charset) {
	Assert.notNull(charset, "'charset' must not be null");
	byte[] bytes = new byte[buffer.readableByteCount()];
	buffer.read(bytes);
	return new String(bytes, charset);
}
 
Example 13
Source File: MockServerHttpResponse.java    From java-technology-stack with MIT License 4 votes vote down vote up
private static String bufferToString(DataBuffer buffer, Charset charset) {
	Assert.notNull(charset, "'charset' must not be null");
	byte[] bytes = new byte[buffer.readableByteCount()];
	buffer.read(bytes);
	return new String(bytes, charset);
}
 
Example 14
Source File: MockClientHttpRequest.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private static String bufferToString(DataBuffer buffer, Charset charset) {
	Assert.notNull(charset, "'charset' must not be null");
	byte[] bytes = new byte[buffer.readableByteCount()];
	buffer.read(bytes);
	return new String(bytes, charset);
}
 
Example 15
Source File: MockServerHttpResponse.java    From java-technology-stack with MIT License 4 votes vote down vote up
private static String bufferToString(DataBuffer buffer, Charset charset) {
	byte[] bytes = new byte[buffer.readableByteCount()];
	buffer.read(bytes);
	return new String(bytes, charset);
}
 
Example 16
Source File: MockClientHttpResponse.java    From java-technology-stack with MIT License 4 votes vote down vote up
private static String dumpString(DataBuffer buffer, Charset charset) {
	Assert.notNull(charset, "'charset' must not be null");
	byte[] bytes = new byte[buffer.readableByteCount()];
	buffer.read(bytes);
	return new String(bytes, charset);
}
 
Example 17
Source File: ServletServerHttpResponse.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected boolean isDataEmpty(DataBuffer dataBuffer) {
	return dataBuffer.readableByteCount() == 0;
}
 
Example 18
Source File: ServletServerHttpResponse.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
protected boolean isDataEmpty(DataBuffer dataBuffer) {
	return dataBuffer.readableByteCount() == 0;
}
 
Example 19
Source File: MockClientHttpResponse.java    From java-technology-stack with MIT License 4 votes vote down vote up
private static String dumpString(DataBuffer buffer, Charset charset) {
	Assert.notNull(charset, "'charset' must not be null");
	byte[] bytes = new byte[buffer.readableByteCount()];
	buffer.read(bytes);
	return new String(bytes, charset);
}
 
Example 20
Source File: DataBufferTestUtils.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Dump all the bytes in the given data buffer, and returns them as a byte array.
 * <p>Note that this method reads the entire buffer into the heap,  which might
 * consume a lot of memory.
 * @param buffer the data buffer to dump the bytes of
 * @return the bytes in the given data buffer
 */
public static byte[] dumpBytes(DataBuffer buffer) {
	Assert.notNull(buffer, "'buffer' must not be null");
	byte[] bytes = new byte[buffer.readableByteCount()];
	buffer.read(bytes);
	return bytes;
}