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

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

Introduction

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

Prototype

int readPosition();

Source Link

Document

Return the position from which this buffer will read.

Usage

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

/**
 * Indicates whether the given data buffer is not the last boundary, i.e. it does not start
 * with two hyphens.//from   www  .j  a  v a  2s.c  o m
 */
private static boolean notLastBoundary(DataBuffer dataBuffer) {
    if (dataBuffer.readableByteCount() >= 2) {
        int readPosition = dataBuffer.readPosition();
        if (dataBuffer.getByte(readPosition) == HYPHEN && dataBuffer.getByte(readPosition + 1) == HYPHEN) {
            DataBufferUtils.release(dataBuffer);
            return false;
        }
    }
    return true;
}

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  w w w .  ja  va2s. 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);
    }
}

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

private static String toString(DataBuffer dataBuffer) {
    byte[] bytes = new byte[dataBuffer.readableByteCount()];
    int j = 0;/*from w  ww  .j  ava 2  s  . co  m*/
    for (int i = dataBuffer.readPosition(); i < dataBuffer.writePosition(); i++) {
        bytes[j++] = dataBuffer.getByte(i);
    }
    return toString(bytes);
}