Netty指定分隔的字符
package org.zln.netty.five.part02; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder; /**
* Created by sherry on 16/11/5.
*/
public class EchoServerInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception { //指定分隔符
ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes("UTF-8")); ChannelPipeline pipeline = socketChannel.pipeline(); pipeline.addLast(new DelimiterBasedFrameDecoder(1024,delimiter));
pipeline.addLast(new StringDecoder());
pipeline.addLast(new EchoServerHandler()); }
}
使用 DelimiterBasedFrameDecoder 解码器
通过
ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes("UTF-8")) ;
构建了自定义的分隔符,在
pipeline.addLast(new DelimiterBasedFrameDecoder(1024,delimiter));
中传入
delimiter 也可以是一个数组
new ByteBuf[] {
Unpooled.wrappedBuffer(new byte[] { '\r', '\n' }),
Unpooled.wrappedBuffer(new byte[] { '\n' }),
};
Netty提供了一个类,保存了常用的分隔符
/*
* Copyright 2012 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; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; /**
* A set of commonly used delimiters for {@link DelimiterBasedFrameDecoder}.
*/
public final class Delimiters { /**
* Returns a {@code NUL (0x00)} delimiter, which could be used for
* Flash XML socket or any similar protocols.
*/
public static ByteBuf[] nulDelimiter() {
return new ByteBuf[] {
Unpooled.wrappedBuffer(new byte[] { 0 }) };
} /**
* Returns {@code CR ('\r')} and {@code LF ('\n')} delimiters, which could
* be used for text-based line protocols.
*/
public static ByteBuf[] lineDelimiter() {
return new ByteBuf[] {
Unpooled.wrappedBuffer(new byte[] { '\r', '\n' }),
Unpooled.wrappedBuffer(new byte[] { '\n' }),
};
} private Delimiters() {
// Unused
}
}
Netty指定分隔的字符的更多相关文章
- 字串符相关 split() 字串符分隔 substring() 提取字符串 substr()提取指定数目的字符 parseInt() 函数可解析一个字符串,并返回一个整数。
split() 方法将字符串分割为字符串数组,并返回此数组. stringObject.split(separator,limit) 我们将按照不同的方式来分割字符串: 使用指定符号分割字符串,代码如 ...
- [SQL]SUTFF内置函数的用法 (删除指定长度的字符并在指定的起始点插入另一组字符)
STUFF 删除指定长度的字符并在指定的起始点插入另一组字符. 语法 STUFF ( character_expression , start , length , character_express ...
- 《Python CookBook2》 第一章 文本 - 过滤字符串中不属于指定集合的字符 && 检查一个字符串是文本还是二进制
过滤字符串中不属于指定集合的字符 任务: 给定一个需要保留的字符串的集合,构建一个过滤函数,并可将其应用于任何字符串s,函数返回一个s的拷贝,该拷贝只包含指定字符集合中的元素. 解决方案: impor ...
- 在当前光标处按指定属性显示字符 - BOIS中断
在当前光标处按指定属性显示字符 - BOIS中断 最简单的调试方式是打印. 编写MBR时,判断MBR是否加载并运行,最直接的方式就是打印一个字符. INT 0x10 功能描述: 在当前光标处按指定属性 ...
- hiho一下 第165周#1327 : 分隔相同字符
题目要求: 时间限制:10000ms单点时限:1000ms内存限制:256MB 描述给定一个只包含小写字母'a'-'z'的字符串 S ,你需要将 S 中的字符重新排序,使得任意两个相同的字符不连在一起 ...
- Oracle 截取指定长度的字符
去掉回车,换行符号,截取指定长度的字符 具体代码示例: --Function --去掉前后空格,截取字符,字符长度为P_Length create or replace function get_St ...
- .NET如何将字符串分隔为字符
.NET如何将字符串分隔为字符 如果这是一道面试题,答案也许非常简单:.ToCharArray(),这基本正确-- 我们以"AB吉
- 【转载】C#中string类使用Remove方法来移除指定位置的字符
在C#的字符串操作过程中,有时候需要将字符串中指定位置的字符移除,此时就可能使用到字符串类string类中的Remove方法,此方法允许指定移除开始的开始的索引位置,以及移除的长度信息等,共有2个重载 ...
- JS对象 提取指定数目的字符substr() substr() 方法从字符串中提取从 startPos位置开始的指定数目的字符串。
提取指定数目的字符substr() substr() 方法从字符串中提取从 startPos位置开始的指定数目的字符串. 语法: stringObject.substr(startPos,length ...
随机推荐
- 自定义SharePoint 2013 元数据选择控件
元数据在Sharepoint中是一个很常用的功能,他提供一个方法来管理企业中常用的关键词,可以更加统一的使用和更新.默认情况下,绑定在列表或库中的元数据字段可以设置是否允许为多个值.但是无法控制在弹出 ...
- Office 365 - SharePoint 2013 Online 之母版页和页面布局
1.打开https://login.microsoftonline.com,登陆,点击SharePoint,如下图: 2.打开网站集,可以查看.添加.删除等管理操作: 3.打开一个站点,如下图: 4. ...
- oracle断电重启之ORA-00600[4194]
1.问题描述 Oracle服务器断电重启以后无法数据库无法正常连接,使用sqlplus envision/envision连接报错.常见的错误有以下这些: ORA-12518: TNS:listene ...
- C99标准的新特性
C语言标准的发展 C语言的发展历史大致上分为4个阶段:Old Style C.C89.C99和C11. C89是最早的C语言规范,于1989年提出,1990年先由ANSI(美国国家标准委员会,Amer ...
- IOS异常日志记录与展现功能
在平常的APP开发过程中经常碰到程序遇到异常闪退的问题,通过日志可以把相关的详细错误信息进行记录,本实例要记录不管在哪个页面出错都要进行记录,这边使用到的日志记录插件CocoaLumberjack,以 ...
- UITabBarController的创建等基本方法
#import "AppDelegate.h" @interface AppDelegate () <UITabBarControllerDelegate> @end ...
- iOS之UI--自定义IOS的HYCheckBox源码的使用
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- OC的项目网址(自己编写的项目)
因为便于方便快速打开自己曾经写过的项目,所以就把链接保存在博客里了.一点击就能找到. <附注学习github排版:https://github.com/yangxuanxc/guide?file ...
- linux命令----网络地址
IP即时生效(重启后失效): ifconfig eth0 192.168.1.102 netmask 255.255.255.0 route add default gw 192.168.1.1 ...
- eclipse常用快捷键及调试方法(虽然现在看不懂,但是感觉以后肯定会用到,先转了)
常用快捷键 Eclipse最全快捷键,熟悉快捷键可以帮助开发事半功倍,节省更多的时间来用于做有意义的事情. Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行 Ctrl ...