netty5/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AddressEncoder.java
田欧 6222101924 migrate java8: use lambda and method reference (#8781)
Motivation:

We can use lambdas now as we use Java8.

Modification:

use lambda function for all package, #8751 only migrate transport package.

Result:

Code cleanup.
2019-01-29 14:06:05 +01:00

68 lines
2.4 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.handler.codec.socksx.v5;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.EncoderException;
import io.netty.util.CharsetUtil;
import io.netty.util.NetUtil;
/**
* Encodes a SOCKS5 address into binary representation.
*
* @see Socks5ClientEncoder
* @see Socks5ServerEncoder
*/
public interface Socks5AddressEncoder {
Socks5AddressEncoder DEFAULT = (addrType, addrValue, out) -> {
final byte typeVal = addrType.byteValue();
if (typeVal == Socks5AddressType.IPv4.byteValue()) {
if (addrValue != null) {
out.writeBytes(NetUtil.createByteArrayFromIpAddressString(addrValue));
} else {
out.writeInt(0);
}
} else if (typeVal == Socks5AddressType.DOMAIN.byteValue()) {
if (addrValue != null) {
out.writeByte(addrValue.length());
out.writeCharSequence(addrValue, CharsetUtil.US_ASCII);
} else {
out.writeByte(0);
}
} else if (typeVal == Socks5AddressType.IPv6.byteValue()) {
if (addrValue != null) {
out.writeBytes(NetUtil.createByteArrayFromIpAddressString(addrValue));
} else {
out.writeLong(0);
out.writeLong(0);
}
} else {
throw new EncoderException("unsupported addrType: " + (addrType.byteValue() & 0xFF));
}
};
/**
* Encodes a SOCKS5 address.
*
* @param addrType the type of the address
* @param addrValue the string representation of the address
* @param out the output buffer where the encoded SOCKS5 address field will be written to
*/
void encodeAddress(Socks5AddressType addrType, String addrValue, ByteBuf out) throws Exception;
}