288 Star 979 Fork 227

helyho / Voovan

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
StringFilter.java 1.28 KB
Copy Edit Raw Blame History
helyho authored 2019-03-29 14:05 . imp: org.voovan.tools 包结构重组
package org.voovan.network.filter;
import org.voovan.network.IoFilter;
import org.voovan.network.IoSession;
import org.voovan.tools.buffer.TByteBuffer;
import java.nio.ByteBuffer;
/**
* String 过滤器
*
* @author helyho
*
* Voovan Framework.
* WebSite: https://github.com/helyho/Voovan
* Licence: Apache v2 License
*/
public class StringFilter implements IoFilter {
private boolean isByteBuffer;
/**
* 构造函数
* @param isByteBuffer 过滤类型是否是 ByteBuffer, true: 过滤类型 Bytebuffer, false: 过滤类型byte
*/
public StringFilter(boolean isByteBuffer){
this.isByteBuffer = isByteBuffer;
}
public StringFilter(){
this.isByteBuffer = true;
}
@Override
public Object encode(IoSession session, Object object) {
if(object instanceof String){
String sourceString = object.toString();
if(isByteBuffer) {
return ByteBuffer.wrap(sourceString.getBytes());
} else {
return sourceString.getBytes();
}
}
return object;
}
@Override
public Object decode(IoSession session,Object object) {
if(object instanceof ByteBuffer && isByteBuffer){
return TByteBuffer.toString((ByteBuffer)object);
}
if(object.getClass() == ByteFilter.BYTE_ARRAY_CLASS && !isByteBuffer){
return new String((byte[])object);
}
return object;
}
}
Java
1
https://gitee.com/helyho/Voovan.git
git@gitee.com:helyho/Voovan.git
helyho
Voovan
Voovan
master

Search