io.netty.util.ByteProcessor Maven / Gradle / Ivy
/*
* 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:
*
* https://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;
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy