代码之家  ›  专栏  ›  技术社区  ›  hema chandra

在Netty JAVA中,如何从连续缓冲区/字节数组中获取特定的字符串模式?

  •  1
  • hema chandra  · 技术社区  · 7 年前

    我目前正在使用Netty。现在的问题是,我想从缓冲区中检索一个特定的字符串。例如,如果缓冲区数据如下所示:

    8=FIX.4.2|9=00815|35=W|49=TT_PRICE|56=SAP0094X|10=134|8=FIX.4.2|9=00816|35=W49=TT_PRICE|56=SAP0094X|10=121
    

    1 回复  |  直到 7 年前
        1
  •  2
  •   rakwaht Sumit.Daksh    7 年前

    ByteBuffer bb = /* byte buffer */
    String text = new String(bb.array(), 0, bb.position(), bb.remaing(), Charset.defaultCharset());
    
    // I assume that this is the string: "8=FIX.4.2|9=00815|35=W|49=TT_PRICE|56=SAP0094X|10=134|8=FIX.4.2|9=00816|35=W49=TT_PRICE|56=SAP0094X|10=121"
    
    // If you need info on the regex just ask for it
    Pattern r = Pattern.compile("(8=\\w\\w\\w)[\\s\\S]*?(10=\\w\\w\\w)");
    Matcher m = r.matcher(text);
    
    while (m.find()) {
         System.out.println(m.group());
    }
    

    请注意 Charset.defaultCharset() 可以根据ByteArray使用的编码进行更改