Code Monkey home page Code Monkey logo

Comments (2)

MisterChangRay avatar MisterChangRay commented on September 3, 2024

不会的, 这两个可以共同使用;
检查下,代码中应该存在两个可变数据区。

from magic-byte.

FULaBUla avatar FULaBUla commented on September 3, 2024

不会的, 这两个可以共同使用; 检查下,代码中应该存在两个可变数据区。
下面这个代码就会报 dynamicSize & dynamicSizeOf only use one in the class; 异常

@MagicClass
public class A {

    @MagicField(order = 1)
    private int id;

    @MagicField(order = 2)
    @MagicConverter(converter = BConvert.class)
    private String b;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        A a = (A) o;
        return id == a.id && Objects.equals(b, a.b);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, b);
    }

    public static class BConvert implements MConverter<String> {

        @Override
        public MResult<String> pack(int nextReadIndex, byte[] fullBytes, String[] attachParams, Class clz, Object obj) {
            A a = (A) obj;
            if (a.getId() == 1) {
                return MResult.build(1,  new String(Arrays.copyOfRange(fullBytes, nextReadIndex, nextReadIndex + 1)));
            } else {
                return MResult.build(2, new String(Arrays.copyOfRange(fullBytes, nextReadIndex, nextReadIndex + 2)));
            }
        }

        @Override
        public byte[] unpack(String object, String[] attachParams) {
            return object.getBytes(StandardCharsets.UTF_8);
        }
    }
}

@MagicClass
public class B {

    @MagicField(order = 1)
    private A a;

    @MagicField(order = 2, dynamicSize = true, size = 10)
    private byte[] data;

    @MagicField(order = 3, size = 4)
    private byte[] check;

    public A getA() {
        return a;
    }

    public void setA(A a) {
        this.a = a;
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }

    public byte[] getCheck() {
        return check;
    }

    public void setCheck(byte[] check) {
        this.check = check;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        B b = (B) o;
        return Objects.equals(a, b.a) && Arrays.equals(data, b.data) && Arrays.equals(check, b.check);
    }

    @Override
    public int hashCode() {
        int result = Objects.hash(a);
        result = 31 * result + Arrays.hashCode(data);
        result = 31 * result + Arrays.hashCode(check);
        return result;
    }
}

    @Test
    public void testDynamicSizeWithConvert() {
        MagicByte.configDefaultCharset(StandardCharsets.UTF_8);
        A a = new A();
        a.setId(1);
        a.setB("1");
        byte[] data = MagicByte.unpackToByte(a);
        A a1 = MagicByte.pack(data, A.class);
        Assert.assertEquals(a, a1);

        B b = new B();
        b.setA(a);
        b.setData("你好".getBytes());
        b.setCheck(new byte[4]);

        data = MagicByte.unpackToByte(b);
        B b1 = MagicByte.pack(data, B.class);
        Assert.assertEquals(b, b1);

    }

from magic-byte.

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.