Example usage for org.springframework.core.io.buffer DataBufferUtils matcher

List of usage examples for org.springframework.core.io.buffer DataBufferUtils matcher

Introduction

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

Prototype

public static Matcher matcher(byte[]... delimiters) 

Source Link

Document

Return a Matcher for the given delimiters.

Usage

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

/**
 * Finds the fist occurrence of the boundary in the given stream of data buffers, and skips
 * all data until then. Note that the first boundary of a multipart message does not contain
 * the initial \r\n, hence the need for a special boundary matcher.
 *//*from  w w w. j a va2 s .  c o  m*/
private static Flux<DataBuffer> skipUntilFirstBoundary(Flux<DataBuffer> dataBuffers, byte[] boundary) {
    byte[] needle = concat(FIRST_BOUNDARY_PREFIX, boundary);
    DataBufferUtils.Matcher matcher = DataBufferUtils.matcher(needle);
    AtomicBoolean found = new AtomicBoolean();

    return dataBuffers.concatMap(dataBuffer -> {
        if (found.get()) {
            return Mono.just(dataBuffer);
        } else {
            int endIdx = matcher.match(dataBuffer);
            if (endIdx != -1) {
                found.set(true);
                int length = dataBuffer.writePosition() - 1 - endIdx;
                DataBuffer slice = dataBuffer.retainedSlice(endIdx + 1, length);
                DataBufferUtils.release(dataBuffer);
                if (logger.isTraceEnabled()) {
                    logger.trace("Found first boundary at " + endIdx + " in " + toString(dataBuffer));
                }
                return Mono.just(slice);
            } else {
                DataBufferUtils.release(dataBuffer);
                return Mono.empty();
            }
        }
    });
}