Coder Social home page Coder Social logo

Comments (2)

hylexus avatar hylexus commented on August 17, 2024

在边界情况下内置的转义有问题😑,下一个版本修复。
@ryngrok 可以先自己声明一个 BytesEncoder,覆盖内置逻辑。

@Configuration
public class Jt808Config extends Jt808ServerConfigure {

	// io.github.hylexus.jt.config.JtProtocolConstant#BEAN_NAME_JT808_BYTES_ENCODER
    @Bean(BEAN_NAME_JT808_BYTES_ENCODER)
    public BytesEncoder bytesEncoder() {
        return new BytesEncoder.DefaultBytesEncoder() {
            @Override
            public byte[] doEscapeForReceive(byte[] bs, int start, int end) throws MsgEscapeException {
                if (start < 0 || end > bs.length) {
                    throw new ArrayIndexOutOfBoundsException("doEscape4Receive error : index out of bounds(start=" + start
                            + ",end=" + end + ",bytes length=" + bs.length + ")");
                }
                try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
                    for (int i = 0; i < start; i++) {
                        outputStream.write(bs[i]);
                    }
                    for (int i = start; i < end - 2; i++) {
                        if (bs[i] == 0x7d && bs[i + 1] == 0x01) {
                            outputStream.write(0x7d);
                            i++;
                        } else if (bs[i] == 0x7d && bs[i + 1] == 0x02) {
                            outputStream.write(0x7e);
                            i++;
                        } else {
                            outputStream.write(bs[i]);
                        }
                    }
                    if (bs[end - 2] == 0x7d && bs[end - 1] == 0x01) {
                        outputStream.write(0x7d);
                    } else if (bs[end - 2] == 0x7d && bs[end - 1] == 0x02) {
                        outputStream.write(0x7e);
                    } else {
                        for (int i = end - 2; i < bs.length; i++) {
                            outputStream.write(bs[i]);
                        }
                    }
                    return outputStream.toByteArray();
                } catch (IOException e) {
                    throw new MsgEscapeException(e);
                }
            }

            @Override
            public byte[] doEscapeForSend(byte[] bs, int start, int end) throws MsgEscapeException {
                if (start < 0 || end > bs.length) {
                    throw new ArrayIndexOutOfBoundsException("doEscape4Send error : index out of bounds(start=" + start
                            + ",end=" + end + ",bytes length=" + bs.length + ")");
                }
                try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
                    for (int i = 0; i < start; i++) {
                        outputStream.write(bs[i]);
                    }
                    for (int i = start; i <= end; i++) {
                        if (bs[i] == 0x7e) {
                            outputStream.write(0x7d);
                            outputStream.write(0x02);
                        } else {
                            outputStream.write(bs[i]);
                        }
                    }
                    for (int i = end + 1; i < bs.length; i++) {
                        outputStream.write(bs[i]);
                    }
                    return outputStream.toByteArray();
                } catch (IOException e) {
                    throw new MsgEscapeException(e);
                }
            }
        };

    }
}

from jt-framework.

hylexus avatar hylexus commented on August 17, 2024

@ryngrok 请升级到 1.0.6-RELEASE

from jt-framework.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.