Example usage for org.springframework.core.io.buffer DataBuffer read

List of usage examples for org.springframework.core.io.buffer DataBuffer read

Introduction

In this page you can find the example usage for org.springframework.core.io.buffer DataBuffer read.

Prototype

DataBuffer read(byte[] destination);

Source Link

Document

Read this buffer's data into the specified destination, starting at the current reading position of this buffer.

Usage

From source file:org.springframework.http.codec.multipart.DefaultMultipartMessageReader.java

/**
 * 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)./*from www.ja v  a2s .c o  m*/
 */
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;
}