ByteArrayOutputStream以及DataOutputStream的组合使用

本文介绍如何通过DataOutputStream和ByteArrayOutputStream实现序列化过程,包括数据的写入与读取,并解释了基本数据类型的转换原理及其在ZooKeeper jute序列化组件中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DataOutputStreamByteArrayOutputStream组合起来可以实现序列化,那么如何进行组合起来呢?


@Test
    public void testDataOutputStreamAndDataInputStream() throws IOException {
        ByteArrayOutputStream bOutputStream = new ByteArrayOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream( bOutputStream );
        String name = "ming";
        int age = 2;
        dataOutputStream.writeInt( 0 );
        dataOutputStream.writeUTF( name );
        dataOutputStream.writeInt( age );
        dataOutputStream.writeLong( 4 );
        byte[] bytes = bOutputStream.toByteArray();
        ByteBuffer byteBuffer = ByteBuffer.wrap( bytes );
        ByteArrayInputStream bInputStream = new ByteArrayInputStream( bytes );
        DataInputStream dataInputStream = new DataInputStream( bInputStream );
        dataInputStream.readInt();
        String userName = dataInputStream.readUTF();
        Integer userAge = dataInputStream.readInt();

        Long longValueLong = dataInputStream.readLong();
        System.out.println( "userName:" + userName );
        System.out.println( "userAge: " + userAge );
        System.out.println( " longValue:" + longValueLong );
    }

    我们可以看到将ByteArrayOutputStream传入DataOutputStream,就可以实现序列化,并且写入的顺序和读入的顺序必须一致,否则会出现错误。例如我们先写入一个int类型的数据,那么当我们读的时候也首先读出来一个int类型的数据。在ZooKeeper的序列化组件jute中就用到了ByteArrayOutputStream和DataOutputStream组合。
    那么如何实现的?ByteArrayOutputStream是面向字节,当我们使用DataOutputStream可以面向java简单的数据类型,那么为什么会这样的,因为在DataOutputStream里面做了一个简单的转化,例如写入一个int的时候,就转化为4个字节然后依次写入。类似的 DataInputStream里面读取一个int的时候,然后连续读取4个字节,然后拼装成一个int。其他基本数据类型一样。

我要发送的命令中,protocolId关键点是key,这个可以的来源是下面的buffer[0] StringBuilder paramString = new StringBuilder();//完整命令,需拼接 paramString.append("AA 03 ");//起始码 长度 paramString.append(devCode).append(" ");//地址 paramString.append("14 ");//操作码 paramString.append(relayMode).append(" ");//闪灯模式 paramString.append(relayStatus).append(" ");//闪灯状态 paramString.append("BB EF");//校验码(固定为BB) 结束码 NettyServer server = NettyUtil.getServer(devType, port); if(server==null) { sr.setResult(SendResult.Disconnection); } else { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); try { dos.writeBytes(paramString.toString()); byte[] buffer = baos.toByteArray(); 而接收方是Convert.toHex((byte[]) msg)中msg转换成byte[] data = (byte[]) msg;后的data[0],Convert.toHex((byte[]) msg)的数据是30 31 20 45 31 20 7b 22 61 64 64 72 65 73 73 22 3a 22 30 31 22 2c 22 63 6f 64 65 56 65 72 73 69 6f 6e 22 3a 22 32 2e 31 2e 37 22 2c 22 63 6f 72 65 56 65 72 73 69 6f 6e 22 3a 22 53 4f 4e 49 43 43 4f 52 45 56 31 2e 31 2e 31 33 22 2c 22 64 65 76 69 63 65 43 6f 64 65 22 3a 22 61 75 64 69 6f 30 31 22 2c 22 66 6c 61 73 68 46 72 65 65 53 70 61 63 65 22 3a 34 34 38 32 30 2c 22 66 75 6e 63 74 69 6f 6e 56 65 72 73 69 6f 6e 22 3a 22 43 4f 4d 4d 4f 4e 22 2c 22 67 61 74 65 77 61 79 22 3a 22 31 39 32 2e 31 36 38 2e 35 30 2e 32 35 34 22 2c 22 68 61 72 64 77 61 72 65 4d 6f 64 65 6c 49 64 22 3a 32 2c 22 68 61 72 64 77 61 72 65 52 65 6c 65 61 73 65 54 69 6d 65 22 3a 22 56 31 2e 31 2e 31 33 5f 53 4f 4e 49 43 43 4f 52 45 5f 32 34 31 31 32 30 5f 31 39 32 31 35 36 22 2c 22 68 61 72 64 77 61 72 65 56 65 72 73 69 6f 6e 22 3a 22 37 36 32 31 22 2c 22 69 6d 65 69 22 3a 22 61 75 64 69 6f 30 31 22 2c 22 69 70 22 3a 22 31 39 32 2e 31 36 38 2e 35 30 2e 32 22 2c 22 6e 65 74 6d 61 73 6b 22 3a 22 32 35 35 2e 32 35 35 2e 32 35 35 2e 30 22 2c 22 70 61 73 73 77 6f 72 64
最新发布
06-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值