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

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

Introduction

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

Prototype

default DataBuffer retainedSlice(int index, int length) 

Source Link

Document

Create a new DataBuffer whose contents is a shared, retained subsequence of this data buffer's content.

Usage

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

/**
 * Convert the given data buffer into a Part. All data up until the header separator (\r\n\r\n)
 * is passed to {@link #toHeaders(DataBuffer)}, the remaining data is considered to be the
 * body.//from   ww w .  ja  v  a2  s .  c  o  m
 */
private static Part toPart(DataBuffer dataBuffer) {
    int readPosition = dataBuffer.readPosition();
    if (dataBuffer.readableByteCount() >= 2) {
        if (dataBuffer.getByte(readPosition) == CR && dataBuffer.getByte(readPosition + 1) == LF) {
            dataBuffer.readPosition(readPosition + 2);
        }
    }

    if (logger.isTraceEnabled()) {
        logger.trace("Part data: " + toString(dataBuffer));
    }
    int endIdx = HEADER_MATCHER.match(dataBuffer);

    HttpHeaders headers;
    DataBuffer body;
    if (endIdx > 0) {
        readPosition = dataBuffer.readPosition();
        int headersLength = endIdx + 1 - (readPosition + HEADER_BODY_SEPARATOR.length);
        DataBuffer headersBuffer = dataBuffer.retainedSlice(readPosition, headersLength);
        int bodyLength = dataBuffer.writePosition() - (1 + endIdx);
        body = dataBuffer.retainedSlice(endIdx + 1, bodyLength);
        headers = toHeaders(headersBuffer);
    } else {
        headers = new HttpHeaders();
        body = DataBufferUtils.retain(dataBuffer);
    }
    DataBufferUtils.release(dataBuffer);

    ContentDisposition cd = headers.getContentDisposition();
    MediaType contentType = headers.getContentType();
    if (StringUtils.hasLength(cd.getFilename())) {
        return new DefaultFilePart(headers, body);
    } else if (StringUtils.hasLength(cd.getName())
            && (contentType == null || MediaType.TEXT_PLAIN.isCompatibleWith(contentType))) {
        return new DefaultFormPart(headers, body);
    } else {
        return new DefaultPart(headers, body);
    }
}