當前位置: 首頁>>代碼示例>>Java>>正文


Java ByteBuf.toString方法代碼示例

本文整理匯總了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();
      }
    }
  }
}
 
開發者ID:spafka,項目名稱:spark_deep,代碼行數:19,代碼來源:SparkSaslServer.java

示例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");
}
 
開發者ID:HubSpot,項目名稱:NioSmtpClient,代碼行數:22,代碼來源:SmtpSessionTest.java

示例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;
}
 
開發者ID:datastax,項目名稱:simulacron,代碼行數:8,代碼來源:ByteBufCodec.java

示例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();
                }

            }
        }
    };
}
 
開發者ID:DevOpsStudio,項目名稱:Re-Collector,代碼行數:37,代碼來源:NewlineChunkSplitter.java

示例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;
}
 
開發者ID:F1r3w477,項目名稱:CustomWorldGen,代碼行數:15,代碼來源:ByteBufUtils.java

示例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);
}
 
開發者ID:AlexMog,項目名稱:SurvivalMMO,代碼行數:10,代碼來源:Packet.java

示例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();
  }
}
 
開發者ID:osswangxining,項目名稱:iotplatform,代碼行數:13,代碼來源:JsonMqttAdaptor.java

示例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")));
  }
}
 
開發者ID:fengchen8086,項目名稱:ditb,代碼行數:14,代碼來源:SaslClientHandler.java

示例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);
}
 
開發者ID:12315jack,項目名稱:j1st-mqtt,代碼行數:14,代碼來源:MqttDecoder.java

示例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);
    }
}
 
開發者ID:SnailFastGo,項目名稱:netty_op,代碼行數:22,代碼來源:TimeServerHandler.java

示例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();
}
 
開發者ID:D3adspaceEnterprises,項目名稱:echidna,代碼行數:12,代碼來源:HTTPDecoder.java

示例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);
	}
}
 
開發者ID:diegopacheco,項目名稱:Building_Effective_Microservices,代碼行數:14,代碼來源:RibbonMeetupClient.java

示例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);
        }
    };
}
 
開發者ID:DevOpsStudio,項目名稱:Re-Collector,代碼行數:63,代碼來源:PatternChunkSplitter.java

示例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;
}
 
開發者ID:qq1588518,項目名稱:JRediClients,代碼行數:7,代碼來源:KeyValueObjectDecoder.java

示例15: decode

import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
@Override
public Object decode(ByteBuf buf, State state) {
    return buf.toString(CharsetUtil.UTF_8);
}
 
開發者ID:qq1588518,項目名稱:JRediClients,代碼行數:5,代碼來源:SlotsDecoder.java


注:本文中的io.netty.buffer.ByteBuf.toString方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。