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

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

Introduction

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

Prototype

int readableByteCount();

Source Link

Document

Return the number of bytes that can be read from this data buffer.

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./* w  ww.j  a  va  2  s . 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./* w  ww.  j ava2s  . co  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

/**
 * 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)./*w  w w  .j a va  2  s .  co 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;
}

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  www. j  ava 2 s. c o  m
    for (int i = dataBuffer.readPosition(); i < dataBuffer.writePosition(); i++) {
        bytes[j++] = dataBuffer.getByte(i);
    }
    return toString(bytes);
}