<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                有時候傳輸的入站或出站數據不夠,通常這種情況也需要處理,例如拋出一個異常。這可能是你錯誤的輸入或處理大的資源或其他的異常導致。我們來寫一個實現,如果輸入字節超出限制長度就拋出TooLongFrameException,這樣的功能一般用來防止資源耗盡。看下圖: 在圖10.4最大幀大小被設置為3個字節。 [![](https://box.kancloud.cn/2015-08-19_55d478d63ec01.jpg)](https://github.com/waylau/essential-netty-in-action/blob/master/images/Figure%2010.4%20Decoding%20via%20FrameChunkDecoder.jpg) Figure 10.4 Decoding via FrameChunkDecoder 上圖顯示幀的大小被限制為3字節,若輸入的字節超過3字節,則超過的字節被丟棄并拋出 TooLongFrameException。在 ChannelPipeline 中的其他ChannelHandler 實現可以處理 TooLongFrameException 或者忽略異常。處理異常在 ChannelHandler.exceptionCaught() 方法中完成,ChannelHandler 提供了一些具體的實現,看下面代碼: ~~~ public class FrameChunkDecoder extends ByteToMessageDecoder { //1 private final int maxFrameSize; public FrameChunkDecoder(int maxFrameSize) { this.maxFrameSize = maxFrameSize; } @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { int readableBytes = in.readableBytes(); //2 if (readableBytes > maxFrameSize) { // discard the bytes //3 in.clear(); throw new TooLongFrameException(); } ByteBuf buf = in.readBytes(readableBytes); //4 out.add(buf); //5 } } ~~~ 1. 繼承 ByteToMessageDecoder 用于解碼入站字節到消息 2. 指定最大需要的幀產生的體積 3. 如果幀太大就丟棄并拋出一個 TooLongFrameException 異常 4. 同時從 ByteBuf 讀到新幀 5. 添加幀到解碼消息 List 示例如下: Listing 10.6 Testing FixedLengthFrameDecoder ~~~ public class FrameChunkDecoderTest { @Test //1 public void testFramesDecoded() { ByteBuf buf = Unpooled.buffer(); //2 for (int i = 0; i < 9; i++) { buf.writeByte(i); } ByteBuf input = buf.duplicate(); EmbeddedChannel channel = new EmbeddedChannel(new FrameChunkDecoder(3)); //3 Assert.assertTrue(channel.writeInbound(input.readBytes(2))); //4 try { channel.writeInbound(input.readBytes(4)); //5 Assert.fail(); //6 } catch (TooLongFrameException e) { // expected } Assert.assertTrue(channel.writeInbound(input.readBytes(3))); //7 Assert.assertTrue(channel.finish()); //8 ByteBuf read = (ByteBuf) channel.readInbound(); Assert.assertEquals(buf.readSlice(2), read); //9 read.release(); read = (ByteBuf) channel.readInbound(); Assert.assertEquals(buf.skipBytes(4).readSlice(3), read); read.release(); buf.release(); } } ~~~ 1. 使用 @Test 注解 2. 新建 ByteBuf 寫入 9 個字節 3. 新建 EmbeddedChannel 并安裝一個 FixedLengthFrameDecoder 用于測試 4. 寫入 2 個字節并預測生產的新幀(消息) 5. 寫一幀大于幀的最大容量 (3) 并檢查一個 TooLongFrameException 異常 6. 如果異常沒有被捕獲,測試將失敗。注意如果類實現 exceptionCaught() 并且處理了異常 exception,那么這里就不會捕捉異常 7. 寫剩余的 2 個字節預測一個幀 8. 標記 channel 完成 9. 讀到的產生的消息并且驗證值。注意 assertEquals(Object,Object)測試使用 equals() 是否相當,不是對象的引用是否相當 即使我們使用 EmbeddedChannel 和 ByteToMessageDecoder。 應該指出的是,同樣的可以做每個 ChannelHandler 的實現,將拋出一個異常。 乍一看,這看起來很類似于測試我們寫在清單10.2中,但它有一個有趣的轉折,即 TooLongFrameException 的處理。這里使用的 try/catch 塊是 EmbeddedChannel 的一種特殊的特性。如果其中一個“write*"編寫方法產生一個受控異常將被包裝在一個 RuntimeException。這使得測試更加容易,如果異常處理的一部分處理。
                  <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>

                              哎呀哎呀视频在线观看