netty5/common/src/main/java/io/netty/util/ByteProcessor.java
Scott Mitchell 4921f62c8a
HttpResponseStatus object allocation reduction
Motivation:
Usages of HttpResponseStatus may result in more object allocation then necessary due to not looking for cached objects and the AsciiString parsing method not being used due to CharSequence method being used instead.

Modifications:
- HttpResponseDecoder should attempt to get the HttpResponseStatus from cache instead of allocating a new object
- HttpResponseStatus#parseLine(CharSequence) should check if the type is AsciiString and redirect to the AsciiString parsing method which may not require an additional toString call
- HttpResponseStatus#parseLine(AsciiString) can be optimized and doesn't require and may not require object allocation

Result:
Less allocations when dealing with HttpResponseStatus.
2018-01-24 22:01:52 -08:00

149 lines
4.3 KiB
Java

/*
* Copyright 2015 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License, version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package io.netty.util;
import static io.netty.util.ByteProcessorUtils.CARRIAGE_RETURN;
import static io.netty.util.ByteProcessorUtils.HTAB;
import static io.netty.util.ByteProcessorUtils.LINE_FEED;
import static io.netty.util.ByteProcessorUtils.SPACE;
/**
* Provides a mechanism to iterate over a collection of bytes.
*/
public interface ByteProcessor {
/**
* A {@link ByteProcessor} which finds the first appearance of a specific byte.
*/
class IndexOfProcessor implements ByteProcessor {
private final byte byteToFind;
public IndexOfProcessor(byte byteToFind) {
this.byteToFind = byteToFind;
}
@Override
public boolean process(byte value) {
return value != byteToFind;
}
}
/**
* A {@link ByteProcessor} which finds the first appearance which is not of a specific byte.
*/
class IndexNotOfProcessor implements ByteProcessor {
private final byte byteToNotFind;
public IndexNotOfProcessor(byte byteToNotFind) {
this.byteToNotFind = byteToNotFind;
}
@Override
public boolean process(byte value) {
return value == byteToNotFind;
}
}
/**
* Aborts on a {@code NUL (0x00)}.
*/
ByteProcessor FIND_NUL = new IndexOfProcessor((byte) 0);
/**
* Aborts on a non-{@code NUL (0x00)}.
*/
ByteProcessor FIND_NON_NUL = new IndexNotOfProcessor((byte) 0);
/**
* Aborts on a {@code CR ('\r')}.
*/
ByteProcessor FIND_CR = new IndexOfProcessor(CARRIAGE_RETURN);
/**
* Aborts on a non-{@code CR ('\r')}.
*/
ByteProcessor FIND_NON_CR = new IndexNotOfProcessor(CARRIAGE_RETURN);
/**
* Aborts on a {@code LF ('\n')}.
*/
ByteProcessor FIND_LF = new IndexOfProcessor(LINE_FEED);
/**
* Aborts on a non-{@code LF ('\n')}.
*/
ByteProcessor FIND_NON_LF = new IndexNotOfProcessor(LINE_FEED);
/**
* Aborts on a semicolon {@code (';')}.
*/
ByteProcessor FIND_SEMI_COLON = new IndexOfProcessor((byte) ';');
/**
* Aborts on a comma {@code (',')}.
*/
ByteProcessor FIND_COMMA = new IndexOfProcessor((byte) ',');
/**
* Aborts on a ascii space character ({@code ' '}).
*/
ByteProcessor FIND_ASCII_SPACE = new IndexOfProcessor(SPACE);
/**
* Aborts on a {@code CR ('\r')} or a {@code LF ('\n')}.
*/
ByteProcessor FIND_CRLF = new ByteProcessor() {
@Override
public boolean process(byte value) {
return value != CARRIAGE_RETURN && value != LINE_FEED;
}
};
/**
* Aborts on a byte which is neither a {@code CR ('\r')} nor a {@code LF ('\n')}.
*/
ByteProcessor FIND_NON_CRLF = new ByteProcessor() {
@Override
public boolean process(byte value) {
return value == CARRIAGE_RETURN || value == LINE_FEED;
}
};
/**
* Aborts on a linear whitespace (a ({@code ' '} or a {@code '\t'}).
*/
ByteProcessor FIND_LINEAR_WHITESPACE = new ByteProcessor() {
@Override
public boolean process(byte value) {
return value != SPACE && value != HTAB;
}
};
/**
* Aborts on a byte which is not a linear whitespace (neither {@code ' '} nor {@code '\t'}).
*/
ByteProcessor FIND_NON_LINEAR_WHITESPACE = new ByteProcessor() {
@Override
public boolean process(byte value) {
return value == SPACE || value == HTAB;
}
};
/**
* @return {@code true} if the processor wants to continue the loop and handle the next byte in the buffer.
* {@code false} if the processor wants to stop handling bytes and abort the loop.
*/
boolean process(byte value) throws Exception;
}