JAVA解析level2行情协议 行情解压方法

解压方法


在Java中,如果你使用Inflater(true)来创建一个Inflater对象并设置nowrap参数为true,这意味着你正在处理一个不使用zlib包装(即没有zlib头部和校验和)的原始DEFLATE数据流。在C++中,要实现相同的解压功能,你需要使用zlib库中的inflate函数,并适当设置其参数来模拟nowrap模式。

然而,需要注意的是,zlib的API并不直接提供一个类似于Java Inflater(true)的构造函数或单个函数调用来启用nowrap模式。相反,你需要通过调用inflateInit2(而不是inflateInit)并指定窗口位大小(通常是MAX_WBITS的某个值)来启用nowrap模式。对于nowrap模式,你应该将窗口位大小设置为-MAX_WBITS(即-15,如果MAX_WBITS定义为15)

Client.java

package com.client;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.Inflater;
import java.util.zip.DataFormatException;

public class Client extends WebSocketClient {

    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

    public Client(String url) throws URISyntaxException {
        super(new URI(url));
    }

    @Override
    public void onOpen(ServerHandshake shake) {
        //发送订阅命令
        this.send("add=lv1_600519,lv2_600519");
    }

    /**
     * 命令返回文本消息
     */
    @Override
    public void onMessage(String paramString) {
        System.out.println(sdf.format(new Date()) + " Text响应:" + paramString);
    }

    @Override
    public void onClose(int paramInt, String paramString, boolean paramBoolean) {
        System.out.println("连接关闭");
    }

    @Override
    public void onError(Exception e) {
        System.out.println("连接异常" + e);
    }

    /**
     * 行情接收处理
     */
    @Override
    public void onMessage(ByteBuffer bytes) {
        super.onMessage(bytes);
        String s="";
        try {
            //二进制解压缩
            byte[] dec=decompress(bytes.array());
            s = new String(dec, "UTF-8");
        }catch (IOException e){
            System.err.println("Binary解析IO异常:"+e.getMessage());
            return;
        }
        catch (DataFormatException e){
            System.err.println("Binary解析格式异常:"+e.getMessage());
            return;
        }
        System.out.println(sdf.format(new Date()) + " Binary响应:" + s);
    }

    /**
     * 解压缩方法
     */
    public static byte[] decompress(byte[] compressedData) throws DataFormatException {
        Inflater inflater = new Inflater(true);
        inflater.setInput(compressedData);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(compressedData.length);
        byte[] buffer = new byte[1024];
        while (!inflater.finished()) {
            int count = inflater.inflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        inflater.end();
        return outputStream.toByteArray();
    }
}

Main.java

package com.client;

import java.net.URISyntaxException;

public class Main {
    public static void main(String[] args) throws URISyntaxException {
        String wsUrl = "ws://<服务器地址>?token=<jvQuant token>";
        Client fd = new Client(wsUrl);
        fd.connect();
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.client</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>socketClient</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>18</maven.compiler.source>
        <maven.compiler.target>18</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.java-websocket</groupId>
            <artifactId>Java-WebSocket</artifactId>
            <version>1.3.5</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.client.Main</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

参考资料

详细代码参考:

GitHub - jvQuant/OpenAPIDemo: jvQuant OpenAPI Demo

jvQuant量化OpenAPI 开发文档

  • 14
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值