All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.qinsoft.qmemache.QMemecacheRequest Maven / Gradle / Ivy

There is a newer version: 1.0.1-beta1
Show newest version
package com.qinsoft.qmemache;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * QMemcache请求模型
 */
public class QMemecacheRequest {
    //命令
    private QMemcacheCommand command;
    //参数
    private Object[]params;
    //数据
    private String bodyData;

    public QMemcacheCommand getCommand() {
        return command;
    }

    public void setCommand(QMemcacheCommand command) {
        this.command = command;
    }

    public Object[] getParams() {
        return params;
    }

    public void setParams(Object[] params) {
        this.params = params;
    }

    public String getBodyData() {
        return bodyData;
    }

    public void setBodyData(String bodyData) {
        this.bodyData = bodyData;
    }

    public QMemecacheRequest(QMemcacheCommand command,Object []params,String bodyData){
        if(command==null)throw new NullPointerException("command");
        this.command=command;
        this.params=params;
        this.bodyData=bodyData;
    }

    /**
     * 编码QMemecacheRequest
     * @param request memcache请求模型
     * @return 编码结果
     */
    public static byte[]encode(QMemecacheRequest request, Charset charset) throws IOException {
        if(request==null)throw new NullPointerException("request");
        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
        Listdata=new ArrayList<>();
        data.add(request.getCommand().getCommandName());
        if(request.getParams()!=null){
           for(Object param : request.getParams()){
               data.add(String.valueOf(param));
           }
        }
        byteArrayOutputStream.write(String.join(" ",data).getBytes(charset));
        byteArrayOutputStream.write(QMemcacheConst.LINE_FLAG_BYTES);
        if(request.getBodyData()!=null){
            byteArrayOutputStream.write(request.getBodyData().getBytes(charset));
            byteArrayOutputStream.write(QMemcacheConst.LINE_FLAG_BYTES);
        }

        return byteArrayOutputStream.toByteArray();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy