本文整理汇总了Java中io.netty.buffer.ByteBuf.toString方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBuf.toString方法的具体用法?Java ByteBuf.toString怎么用?Java ByteBuf.toString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.buffer.ByteBuf
的用法示例。
在下文中一共展示了ByteBuf.toString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getBase64EncodedString
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
/** Return a Base64-encoded string. */
private static String getBase64EncodedString(String str) {
ByteBuf byteBuf = null;
ByteBuf encodedByteBuf = null;
try {
byteBuf = Unpooled.wrappedBuffer(str.getBytes(StandardCharsets.UTF_8));
encodedByteBuf = Base64.encode(byteBuf);
return encodedByteBuf.toString(StandardCharsets.UTF_8);
} finally {
// The release is called to suppress the memory leak error messages raised by netty.
if (byteBuf != null) {
byteBuf.release();
if (encodedByteBuf != null) {
encodedByteBuf.release();
}
}
}
}
示例2: itCanAuthenticateWithAuthLogin
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Test
public void itCanAuthenticateWithAuthLogin() throws Exception {
String username = "user";
String password = "password";
// do the initial request, which just includes the username
session.authLogin("user", "password");
verify(channel).writeAndFlush(new DefaultSmtpRequest("AUTH", "LOGIN", encodeBase64(username)));
// now the second request, which sends the password
responseFuture.complete(Lists.newArrayList(INTERMEDIATE_RESPONSE));
// this is sent to the second invocation of writeAndFlush
ArgumentCaptor<Object> bufCaptor = ArgumentCaptor.forClass(Object.class);
verify(channel, times(2)).writeAndFlush(bufCaptor.capture());
ByteBuf capturedBuffer = (ByteBuf) bufCaptor.getAllValues().get(1);
String actualString = capturedBuffer.toString(0, capturedBuffer.readableBytes(), StandardCharsets.UTF_8);
assertThat(actualString).isEqualTo(encodeBase64(password) + "\r\n");
}
示例3: readLongString
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
public String readLongString(ByteBuf source) {
int len = readInt(source);
String str = source.toString(source.readerIndex(), len, CharsetUtil.UTF_8);
source.readerIndex(source.readerIndex() + len);
return str;
}
示例4: split
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
public Iterable<String> split(final ByteBuf buffer, final Charset charset, final boolean includeRemainingData) {
return () -> new AbstractIterator<String>() {
@Override
protected String computeNext() {
ByteBuf fullLine = null;
try {
if (!buffer.isReadable()) {
return endOfData();
}
final int i = buffer.forEachByte(ByteBufProcessor.FIND_CRLF);
if (i == -1) {
if (includeRemainingData) {
final ByteBuf remaining = buffer.readBytes(buffer.readableBytes());
return remaining.toString(charset);
} else {
return endOfData();
}
}
fullLine = buffer.readBytes(i);
// Strip the \r/\n bytes from the buffer.
final byte readByte = buffer.readByte(); // the \r or \n byte
if (readByte == '\r') {
buffer.readByte(); // the \n byte if previous was \r
}
return fullLine.toString(charset);
} finally {
buffer.discardReadBytes();
if (fullLine != null) {
fullLine.release();
}
}
}
};
}
示例5: readUTF8String
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
/**
* Read a UTF8 string from the byte buffer.
* It is encoded as <varint length>[<UTF8 char bytes>]
*
* @param from The buffer to read from
* @return The string
*/
public static String readUTF8String(ByteBuf from)
{
int len = readVarInt(from,2);
String str = from.toString(from.readerIndex(), len, Charsets.UTF_8);
from.readerIndex(from.readerIndex() + len);
return str;
}
示例6: readUTF8
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
public static String readUTF8(ByteBuf in) {
ByteBuf buffer = in.alloc().buffer();
byte b;
while (in.readableBytes() > 0 && (b = in.readByte()) != 0) {
buffer.writeByte(b);
}
return buffer.toString(Charsets.UTF_8);
}
示例7: validatePayload
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
public static String validatePayload(SessionId sessionId, ByteBuf payloadData) throws AdaptorException {
try {
String payload = payloadData.toString(UTF8);
if (payload == null) {
log.warn("[{}] Payload is empty!", sessionId.toUidStr());
throw new AdaptorException(new IllegalArgumentException("Payload is empty!"));
}
return payload;
} finally {
payloadData.release();
}
}
示例8: readStatus
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
/**
* Get the read status
*
* @param inStream to read
* @throws org.apache.hadoop.ipc.RemoteException if status was not success
*/
private static void readStatus(ByteBuf inStream) throws RemoteException {
int status = inStream.readInt(); // read status
if (status != SaslStatus.SUCCESS.state) {
throw new RemoteException(inStream.toString(Charset.forName("UTF-8")),
inStream.toString(Charset.forName("UTF-8")));
}
}
示例9: decodeString
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
private static Result<String> decodeString(ByteBuf buffer, int minBytes, int maxBytes) {
final Result<Integer> decodedSize = decodeMsbLsb(buffer);
int size = decodedSize.value;
int numberOfBytesConsumed = decodedSize.numberOfBytesConsumed;
if (size < minBytes || size > maxBytes) {
buffer.skipBytes(size);
numberOfBytesConsumed += size;
return new Result<>(null, numberOfBytesConsumed);
}
ByteBuf buf = buffer.readBytes(size);
numberOfBytesConsumed += size;
return new Result<>(buf.toString(CharsetUtil.UTF_8), numberOfBytesConsumed);
}
示例10: channelRead
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
/**
*@description 接收客户端的请求数据
*@time 创建时间:2017年7月24日下午2:30:30
*@param ctx
*@param msg
*@throws Exception
*@author dzn
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if(msg instanceof ByteBuf){
ByteBuf req = (ByteBuf)msg;
String content = req.toString(Charset.defaultCharset());
System.out.println("接收到ByteBuf类型的请求数据 : " + content);
ctx.channel().writeAndFlush(msg);
}else if(msg instanceof String){
System.out.println("接收到String类型的请求数据 : " + msg.toString());
ctx.channel().writeAndFlush(msg + "\r\n");
System.out.println("向客户端返回响应数据 : " + msg);
}
}
示例11: decode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf,
List<Object> list) throws Exception {
String rawData = byteBuf.toString(CharsetUtil.UTF_8);
HTTPRequest request = HTTPRequestParser.parseRequest(rawData);
list.add(request);
byteBuf.clear();
}
示例12: execute
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
private <T> Observable<T> execute(Class<T> t,HttpRequestTemplate<ByteBuf> apiTemplate){
RibbonResponse<ByteBuf> result = apiTemplate.requestBuilder().withHeader("client", "client-microservice").build()
.withMetadata().execute();
ByteBuf buf = result.content();
String json = buf.toString(Charset.forName("UTF-8"));
try {
return Observable.just(mapper.readValue(json, t));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例13: split
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
public Iterable<String> split(final ByteBuf buffer, final Charset charset, final boolean includeRemainingData) {
return () -> new AbstractIterator<String>() {
// TODO Might throw an exception if multibyte charset is used and buffer is not complete.
// Use CharsetDecoder to create a CharBuffer and match on that!
private final String inputAsString = buffer.toString(charset);
final Matcher matcher = pattern.matcher(inputAsString);
private int positionInString = 0;
@Override
protected String computeNext() {
try {
if (!buffer.isReadable()) {
return endOfData();
}
if (matcher.find()) {
int firstByte = matcher.start();
if (firstByte == 0) {
// advance further, the buffer begins with our pattern.
if (matcher.find()) {
firstByte = matcher.start();
} else {
if (!includeRemainingData) {
// couldn't find the end of the entry (i.e. there wasn't a next line yet)
return endOfData();
} else {
// couldn't find another line, but we are asked to finish up, include everything that remains
return getRemainingContent();
}
}
}
if (firstByte == 0) {
// still haven't found a non-zero length string, keep waiting for more data.
return endOfData();
}
final String substring = inputAsString.substring(positionInString, firstByte);
positionInString = firstByte;
buffer.skipBytes(substring.getBytes(charset).length); // TODO performance
return substring;
} else {
if (includeRemainingData) {
return getRemainingContent();
}
return endOfData();
}
} catch (IllegalStateException e) {
// the cause contains the CharacterCodingException from the ChannelBuffer.toString() methods
// this usually means the buffer ended with an incomplete encoding of a unicode character.
// WHY U SO SUCK CHARACTER ENCODINGS?
// we need to wait until more data is available
return endOfData();
} finally {
buffer.discardReadBytes();
}
}
private String getRemainingContent() {
final ByteBuf channelBuffer = buffer.readBytes(buffer.readableBytes());
return channelBuffer.toString(charset);
}
};
}
示例14: decode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
public Object decode(ByteBuf buf, State state) {
String status = buf.toString(CharsetUtil.UTF_8);
buf.skipBytes(1);
return status;
}
示例15: decode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
public Object decode(ByteBuf buf, State state) {
return buf.toString(CharsetUtil.UTF_8);
}