<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                JDK 提供了 ObjectOutputStream 和 ObjectInputStream 通過網絡將原始數據類型和 POJO 進行序列化和反序列化。API并不復雜,可以應用到任何對象,支持 java.io.Serializable 接口。但它也不是非常高效的。在本節中,我們將看到 Netty 所提供的。 ### [](https://github.com/waylau/essential-netty-in-action/blob/master/CORE%20FUNCTIONS/Serializing%20data.md#jdk-序列化)JDK 序列化 如果程序與端對端間的交互是使用 ObjectOutputStream 和 ObjectInputStream,并且主要面臨的問題是兼容性,那么,[JDK 序列化](http://docs.oracle.com/javase/7/docs/technotes/guides/serialization/)?是不錯的選擇。 表8.8列出了序列化類,Netty 提供了與 JDK 的互操作。 Table 8.8 JDK Serialization codecs | 名稱 | 描述 | | --- | --- | | CompatibleObjectDecoder | 該解碼器使用 JDK 序列化,用于與非 Netty 進行互操作。 | | CompatibleObjectEncoder | 該編碼器使用 JDK 序列化,用于與非 Netty 進行互操作。 | | ObjectDecoder | 基于 JDK 序列化來使用自定義序列化解碼。外部依賴被排除在外時,提供了一個速度提升。否則選擇其他序列化實現 | | ObjectEncoder | 基于 JDK 序列化來使用自定義序列化編碼。外部依賴被排除在外時,提供了一個速度提升。否則選擇其他序列化實現 | ### [](https://github.com/waylau/essential-netty-in-action/blob/master/CORE%20FUNCTIONS/Serializing%20data.md#jboss-marshalling-序列化)JBoss Marshalling 序列化 如果可以使用外部依賴 JBoss Marshalling 是個明智的選擇。比 JDK 序列化快3倍且更加簡練。 *[JBoss Marshalling](https://www.jboss.org/jbossmarshalling)?是另一個序列化 API,修復的許多 JDK序列化 API 中發現的問題,它與 java.io.Serializable 完全兼容。并添加了一些新的可調參數和附加功能,所有這些都可插入通過工廠配置外部化,類/實例查找表,類決議,對象替換,等等)* 下表展示了 Netty 支持 JBoss Marshalling 的編解碼器。 Table 8.9 JBoss Marshalling codecs | 名稱 | 描述 | | --- | --- | | CompatibleMarshallingDecoder | 為了與使用 JDK 序列化的端對端間兼容。 | | CompatibleMarshallingEncoder | 為了與使用 JDK 序列化的端對端間兼容。 | | MarshallingDecoder | 使用自定義序列化用于解碼,必須使用 | MarshallingEncoder MarshallingEncoder | 使用自定義序列化用于編碼,必須使用 MarshallingDecoder 下面展示了使用 MarshallingDecoder 和 MarshallingEncoder Listing 8.13 Using JBoss Marshalling ~~~ public class MarshallingInitializer extends ChannelInitializer<Channel> { private final MarshallerProvider marshallerProvider; private final UnmarshallerProvider unmarshallerProvider; public MarshallingInitializer(UnmarshallerProvider unmarshallerProvider, MarshallerProvider marshallerProvider) { this.marshallerProvider = marshallerProvider; this.unmarshallerProvider = unmarshallerProvider; } @Override protected void initChannel(Channel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); pipeline.addLast(new MarshallingDecoder(unmarshallerProvider)); pipeline.addLast(new MarshallingEncoder(marshallerProvider)); pipeline.addLast(new ObjectHandler()); } public static final class ObjectHandler extends SimpleChannelInboundHandler<Serializable> { @Override public void channelRead0(ChannelHandlerContext channelHandlerContext, Serializable serializable) throws Exception { // Do something } } } ~~~ ### [](https://github.com/waylau/essential-netty-in-action/blob/master/CORE%20FUNCTIONS/Serializing%20data.md#protobuf-序列化)ProtoBuf 序列化 ProtoBuf 來自谷歌,并且開源了。它使編解碼數據更加緊湊和高效。它已經綁定各種編程語言,使它適合跨語言項目。 下表展示了 Netty 支持 ProtoBuf 的 ChannelHandler 實現。 Table 8.10 ProtoBuf codec | 名稱 | 描述 | | --- | --- | | ProtobufDecoder | 使用 ProtoBuf 來解碼消息 | | ProtobufEncoder | 使用 ProtoBuf 來編碼消息 | | ProtobufVarint32FrameDecoder | 在消息的整型長度域中,通過 "[Base 128 Varints](https://developers.google.com/protocol-buffers/docs/encoding)"將接收到的 ByteBuf 動態的分割 | 用法見下面 Listing 8.14 Using Google Protobuf ~~~ public class ProtoBufInitializer extends ChannelInitializer<Channel> { private final MessageLite lite; public ProtoBufInitializer(MessageLite lite) { this.lite = lite; } @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new ProtobufVarint32FrameDecoder()); pipeline.addLast(new ProtobufEncoder()); pipeline.addLast(new ProtobufDecoder(lite)); pipeline.addLast(new ObjectHandler()); } public static final class ObjectHandler extends SimpleChannelInboundHandler<Object> { @Override public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { // Do something with the object } } } ~~~ 1. 添加 ProtobufVarint32FrameDecoder 用來分割幀 2. 添加 ProtobufEncoder 用來處理消息的編碼 3. 添加 ProtobufDecoder 用來處理消息的解碼 4. 添加 ObjectHandler 用來處理解碼了的消息 本章在這最后一節中,我們探討了 Netty 支持的不同的序列化的專門的解碼器和編碼器。這些是標準 JDK 序列化 API,JBoss Marshalling 和谷歌ProtoBuf。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看