Java Code Examples for io.netty.util.CharsetUtil#ISO_8859_1

The following examples show how to use io.netty.util.CharsetUtil#ISO_8859_1 . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestDelimitedLengthFieldBasedFrameDecoder.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testDelimitedLengths() throws Exception {
  Charset charset = CharsetUtil.ISO_8859_1;
  EmbeddedChannel ch = getTestChannel(charset, 100, 0, false);

  String v1 = "a";
  String v2 = "abcdefghij";
  String v3 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrsxt"
      + "uvwxyz";

  writeStringAndAssert(ch, v1, charset, false, false);
  writeStringAndAssert(ch, v2, charset, false, false);
  writeStringAndAssert(ch, v3, charset, false, true);

  writeStringAndAssert(ch, v1, charset, true, false);
  writeStringAndAssert(ch, v2, charset, true, false);
  writeStringAndAssert(ch, v3, charset, true, true);

  writeStringAndAssert(ch, v1, charset, false, false);

  ch.close();
}
 
Example 2
Source File: TestDelimitedLengthFieldBasedFrameDecoder.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testMaxFrameLengthOverflow() throws Exception {
  Charset charset = CharsetUtil.ISO_8859_1;
  // maxFrameLength plus adjustment would overflow an int
  final long numBytes = Integer.MAX_VALUE - 1;
  final int lengthAdjustment = 10;
  EmbeddedChannel ch = getTestChannel(charset, (int) numBytes, lengthAdjustment, true);

  //this is a bad frame, but will still test the overflow condition
  String longString = String.valueOf(numBytes) + " abcd";

  try {
    ch.writeInbound(Unpooled.copiedBuffer(longString, charset));
    Assert.fail("TooLongFrameException should have been thrown");
  } catch (TooLongFrameException ignored) {
    //ignored
  }
  Assert.assertNull(ch.readInbound());

  ch.close();
}
 
Example 3
Source File: Sender.java    From dht-spider with MIT License 5 votes vote down vote up
/**
 * 回复find_node回复
 */
public  void findNodeReceive(String messageId,InetSocketAddress address, String nodeId, List<Node> nodeList,int num) {
    if(!channels.get(num).isWritable()){
        return;
    }
    FindNodeResponse findNodeResponse=new FindNodeResponse(messageId,nodeId,new String(Node.toBytes(nodeList), CharsetUtil.ISO_8859_1));
    channels.get(num).writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(bencode.encode(DHTUtil.beanToMap(findNodeResponse))), address));
}
 
Example 4
Source File: Sender.java    From dht-spider with MIT License 5 votes vote down vote up
/**
 * 回复get_peers
 */
public  void getPeersReceive(String messageId,InetSocketAddress address, String nodeId, String token, List<Node> nodeList,int num) {
    if(!channels.get(num).isWritable()){
        return;
    }
    GetPeersResponse getPeersResponse = new GetPeersResponse(messageId,nodeId, token, new String(Node.toBytes(nodeList), CharsetUtil.ISO_8859_1));
    channels.get(num).writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(bencode.encode(DHTUtil.beanToMap(getPeersResponse))), address));
}
 
Example 5
Source File: DHTUtil.java    From dht-spider with MIT License 5 votes vote down vote up
public static String generateMessageId(){
    int value;
    if((value=msgIdGenerate.incrementAndGet())>maxMessageID){
        msgIdGenerate.set(1);
    }
    byte[] b=new byte[2];
    b[1]=(byte)(value & 0xff);
    b[0]=(byte)(value>>8 & 0xff);
    return new String(b, CharsetUtil.ISO_8859_1);
}
 
Example 6
Source File: HttpUtil.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch charset from Content-Type header value.
 *
 * @param contentTypeValue Content-Type header value to parse
 * @return the charset from message's Content-Type header or {@link CharsetUtil#ISO_8859_1}
 * if charset is not presented or unparsable
 */
public static Charset getCharset(CharSequence contentTypeValue) {
    if (contentTypeValue != null) {
        return getCharset(contentTypeValue, CharsetUtil.ISO_8859_1);
    } else {
        return CharsetUtil.ISO_8859_1;
    }
}
 
Example 7
Source File: BaseResponseInfoBuilderTest.java    From riposte with Apache License 2.0 5 votes vote down vote up
@Test
public void builder_stores_values_as_expected() {
    // given
    String content = UUID.randomUUID().toString();
    int httpStatusCode = 200;
    HttpHeaders headers = new DefaultHttpHeaders();
    String mimeType = "text/text";
    Charset contentCharset = CharsetUtil.ISO_8859_1;
    Set<Cookie> cookies = Sets.newHashSet(new DefaultCookie("key1", "val1"), new DefaultCookie("key2", "val2"));
    boolean preventCompressedOutput = true;

    // when
    BaseResponseInfoBuilder<String> responseInfoBuilder = new BaseResponseInfoBuilder<String>(){}
            .withHttpStatusCode(httpStatusCode)
            .withHeaders(headers)
            .withDesiredContentWriterMimeType(mimeType)
            .withDesiredContentWriterEncoding(contentCharset).withCookies(cookies)
            .withPreventCompressedOutput(preventCompressedOutput);

    // then
    assertThat(responseInfoBuilder.getHttpStatusCode(), is(httpStatusCode));
    assertThat(responseInfoBuilder.getHeaders(), is(headers));
    assertThat(responseInfoBuilder.getDesiredContentWriterMimeType(), is(mimeType));
    assertThat(responseInfoBuilder.getDesiredContentWriterEncoding(), is(contentCharset));
    assertThat(responseInfoBuilder.getCookies(), is(cookies));
    assertThat(responseInfoBuilder.isPreventCompressedOutput(), is(preventCompressedOutput));
}
 
Example 8
Source File: FullResponseInfoTest.java    From riposte with Apache License 2.0 5 votes vote down vote up
@Test
public void builder_sets_values_as_expected() {
    // given
    String content = UUID.randomUUID().toString();
    int httpStatusCode = 200;
    HttpHeaders headers = new DefaultHttpHeaders();
    String mimeType = "text/text";
    Charset contentCharset = CharsetUtil.ISO_8859_1;
    Set<Cookie> cookies = Sets.newHashSet(new DefaultCookie("key1", "val1"), new DefaultCookie("key2", "val2"));
    boolean preventCompressedOutput = true;

    // when
    FullResponseInfo<String> responseInfo = ResponseInfo.<String>newBuilder()
                                                    .withContentForFullResponse(content)
                                                    .withHttpStatusCode(httpStatusCode)
                                                    .withHeaders(headers)
                                                    .withDesiredContentWriterMimeType(mimeType)
                                                    .withDesiredContentWriterEncoding(contentCharset).withCookies(cookies)
                                                    .withPreventCompressedOutput(preventCompressedOutput).build();

    // then
    assertThat(responseInfo.getContentForFullResponse(), is(content));
    assertThat(responseInfo.getHttpStatusCode(), is(httpStatusCode));
    assertThat(responseInfo.getHeaders(), is(headers));
    assertThat(responseInfo.getDesiredContentWriterMimeType(), is(mimeType));
    assertThat(responseInfo.getDesiredContentWriterEncoding(), is(contentCharset));
    assertThat(responseInfo.getCookies(), is(cookies));
    assertThat(responseInfo.getUncompressedRawContentLength(), nullValue());
    assertThat(responseInfo.getFinalContentLength(), nullValue());
    assertThat(responseInfo.isPreventCompressedOutput(), is(preventCompressedOutput));
    assertThat(responseInfo.isChunkedResponse(), is(false));
    assertThat(responseInfo.isResponseSendingStarted(), is(false));
    assertThat(responseInfo.isResponseSendingLastChunkSent(), is(false));
}
 
Example 9
Source File: DHTUtil.java    From dht-spider with MIT License 4 votes vote down vote up
public static String generateNodeIdString() {
    return new String(RandomUtils.nextBytes(20), CharsetUtil.ISO_8859_1);
}
 
Example 10
Source File: DHTUtil.java    From dht-spider with MIT License 4 votes vote down vote up
/**
 * 包装generateSimilarInfoHash()方法参数和返回值为string
 */
public static String generateSimilarInfoHashString(byte[] nodeId,int num) {
    return new String(generateSimilarNodeId(nodeId,num),CharsetUtil.ISO_8859_1);
}
 
Example 11
Source File: OrderHandler.java    From jframe with Apache License 2.0 4 votes vote down vote up
@Override
protected Charset getReqCharset() {
    return ReqOp.UPMPBACK.code.equals(getReqOp()) ? CharsetUtil.ISO_8859_1 : super.getReqCharset();
}