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

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

Introduction

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

Prototype

byte getByte(int index);

Source Link

Document

Read a single byte at the given index 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 w  w  .ja va 2  s.  com
 */
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  ww w  .  j a  v  a  2s  .  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

private static String toString(DataBuffer dataBuffer) {
    byte[] bytes = new byte[dataBuffer.readableByteCount()];
    int j = 0;//  w  w w .  j  a  v  a 2 s .  c o m
    for (int i = dataBuffer.readPosition(); i < dataBuffer.writePosition(); i++) {
        bytes[j++] = dataBuffer.getByte(i);
    }
    return toString(bytes);
}