org.springframework.core.io.buffer.DataBuffer#read ( )源码实例Demo

下面列出了org.springframework.core.io.buffer.DataBuffer#read ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

/**
 * 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;
}
 
@Test
public void resolveParts() {
	ServerHttpRequest request = generateMultipartRequest();
	ResolvableType elementType = forClassWithGenerics(MultiValueMap.class, String.class, Part.class);
	MultiValueMap<String, Part> parts = this.reader.readMono(elementType, request, emptyMap()).block();
	assertEquals(2, parts.size());

	assertTrue(parts.containsKey("fooPart"));
	Part part = parts.getFirst("fooPart");
	assertTrue(part instanceof FilePart);
	assertEquals("fooPart", part.name());
	assertEquals("foo.txt", ((FilePart) part).filename());
	DataBuffer buffer = DataBufferUtils.join(part.content()).block();
	assertEquals(12, buffer.readableByteCount());
	byte[] byteContent = new byte[12];
	buffer.read(byteContent);
	assertEquals("Lorem Ipsum.", new String(byteContent));

	assertTrue(parts.containsKey("barPart"));
	part = parts.getFirst("barPart");
	assertTrue(part instanceof FormFieldPart);
	assertEquals("barPart", part.name());
	assertEquals("bar", ((FormFieldPart) part).value());
}
 
@Test
public void resolveParts() {
	ServerHttpRequest request = generateMultipartRequest();
	ResolvableType elementType = forClassWithGenerics(MultiValueMap.class, String.class, Part.class);
	MultiValueMap<String, Part> parts = this.reader.readMono(elementType, request, emptyMap()).block();
	assertEquals(2, parts.size());

	assertTrue(parts.containsKey("fooPart"));
	Part part = parts.getFirst("fooPart");
	assertTrue(part instanceof FilePart);
	assertEquals("fooPart", part.name());
	assertEquals("foo.txt", ((FilePart) part).filename());
	DataBuffer buffer = DataBufferUtils.join(part.content()).block();
	assertEquals(12, buffer.readableByteCount());
	byte[] byteContent = new byte[12];
	buffer.read(byteContent);
	assertEquals("Lorem Ipsum.", new String(byteContent));

	assertTrue(parts.containsKey("barPart"));
	part = parts.getFirst("barPart");
	assertTrue(part instanceof FormFieldPart);
	assertEquals("barPart", part.name());
	assertEquals("bar", ((FormFieldPart) part).value());
}
 
源代码4 项目: spring-analysis-note   文件: ResourceDecoder.java
@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);
	}
}
 
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);
}
 
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);
}
 
源代码7 项目: java-technology-stack   文件: ResourceDecoder.java
@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);
	}
}
 
源代码8 项目: java-technology-stack   文件: ByteArrayDecoder.java
@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;
}
 
private static String bufferToString(DataBuffer buffer, Charset charset) {
	byte[] bytes = new byte[buffer.readableByteCount()];
	buffer.read(bytes);
	return new String(bytes, charset);
}
 
private static String toString(DataBuffer dataBuffer, Charset charset) {
	byte[] bytes = new byte[dataBuffer.readableByteCount()];
	dataBuffer.read(bytes);
	DataBufferUtils.release(dataBuffer);
	return new String(bytes, charset).trim();
}
 
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);
}
 
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);
}
 
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);
}
 
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);
}
 
源代码15 项目: armeria   文件: TestUtil.java
static String bufferToString(DataBuffer buffer) {
    final byte[] bytes = new byte[buffer.readableByteCount()];
    buffer.read(bytes);
    return new String(bytes);
}
 
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);
}
 
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);
}
 
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);
}
 
源代码19 项目: spring-analysis-note   文件: DataBufferTestUtils.java
/**
 * 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;
}
 
/**
 * 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;
}