ByteBufでのJavaネットワークプログラミング--Netty

  JDKがByteBufferのダイナミックない膨張、および複雑なAPIやその他の理由の使用を提供するので、網状はByteBufを提供します。API操作がより便利Bytebuf、膨張が動的ゼロコピーのためByteBufの様々な、かつ効率的な機構を実現するために設けられてもよいです。
  
  ByteBuf操作
  
  三つの重要な特性を持っていByteBuf:位置を書き込ん容量容量、readerIndex読ん位置、writerIndex readerIndex 2つの変数とポインタがシーケンシャルリードをサポートしてに書き込み操作をweiterIndex提供
  
  バッファの図はどのように2を示しています:3つの領域に分割ポインタ
  
  IMG
  
  コードの例:
  
  ;インポートio.netty.buffer.ByteBuf
  
  インポートio.netty.buffer.Unpooled;
  
  インポートjava.util.Arrays;
  
  publicクラスByteBufDemo {
  
  公共の静的な無効メイン(文字列[]引数){
  
  //非ByteBuf、10バイトのサイズのプールを作成
  
  ByteBuf BUF = Unpooled.buffer(10)
  
  のSystem.out.println( "としてオリジナルByteBuf:" + buf.toString())。
  
  System.out.println( "範囲の1.ByteBuf内容:" Arrays.toStringの+(buf.array())+ "\ N-");
  
  //書き込み部2 SUMMARY
  
  バイト[]バイト= {1,2 ,. 3 ,. 4 ,. 5};
  
  buf.writeBytes(バイト)
  
  のSystem.out.println( "バイトのように書く:" + Arrays.toStringの(バイト));
  
  のSystem.out .println( "コンテンツByteBufの部分を書いた後で、" + buf.toString());
  
  のSystem.out.println( "範囲内2.ByteBuf含量:" + Arrays.toString(buf.array() )+ " \ N- ");
  
  //コンテンツの一部を読み取る3.
  
  バイトB1 = buf.readByte();
  
  バイトB2 = buf.readByte();
  
  のSystem.out.println("バイト読み取るために:「+ Arrays.toStringを(バイト新しい新[] {B1、B2}));
  
  のSystem.out.println( "コンテンツByteBufの部分を読んだ後である:" + buf.toString());
  
  のSystem.out.println範囲3.ByteBufで(「コンテンツ\ N- ");: "+ Arrays.toString(buf.array())+の"
  
  // 4.廃棄読み取り内容
  
  ; buf.discardReadBytes()
  
  読み込みのSystem.out.println(の内容を破棄した後に"次のようにByteBuf: "+ buf.toString())。
  
  System.out.println( "範囲の4.ByteBuf内容:" + Arrays.toStringの(buf.array())+ "\ N-");
  
  //クリア5.読み取りおよび書き込みポインタ
  
  buf.clear();
  
  システム。 out.printlnを( "リードを空としてByteBufポインタを書き込む:" + buf.toString());
  
  のSystem.out.println( "範囲内5.ByteBuf含量:" + Arrays.toString(buf.array() )+ "\ N-");
  
  コンテンツの// 6片は、再び書き込まれたコンテンツの最初の部分よりも小さい
  
  バイト[] = {bytes2 1,2 ,. 3};
  
  buf.writeBytes(bytes2)
  
  のSystem.out.println(「書き込み着信バイトである: "+ Arrays.toString(bytes2))の;
  
  のSystem.out.println("コンテンツByteBufの部分を書いた後で、 "+ buf.toString());
  
  (するSystem.out.println" 6.ByteBufでコンテンツ: "+ Arrays.toStringの(buf.array())+" \ N- ");
  
  //意志ByteBuf 7.クリア
  
  buf.setZero(0、buf.capacity());
  
  System.out.printlnは( "ByteBufをクリアした後である:" + BUF。toString())。
  
  System.out.println( "範囲内7.ByteBuf含量:" + Arrays.toStringの(buf.array())+ "\ N-");
  
  //書き込み部8は、コンテンツが再度の容量超える
  
  バイト[] bytes3 {1 = 1,2 ,. 3 ,. 4 ,. 5 ,. 6 ,. 7 ,. 8 ,. 9、10 ,. 11};
  
  buf.writeBytes(bytes3)
  
  のSystem.out.println( "バイトのように書く:" + Arrays.toString (バイト));
  
  のSystem.out.println( "コンテンツByteBufの部分を書いた後で、" + buf.toString());
  
  のSystem.out.println( "範囲内8.ByteBufコンテンツ:" + Arrays.toString( buf.array())+ "\ N-");
  
  //ロードデータにIDataView
  
  このデータセットは、//検出の変更はないスパイクまたはトレーニングのために使用されている。
  
  IDataViewのDataView www.chaoyul.com mlContext.Data.LoadFromTextFile = < ProductSalesData>(パス:DatasetPath、hasHeader :真、separatorCharです:www.sangyuLpt.com '')。
  
  //予測を作成するためのデータ変換を適用します。
  
  IDataView transformedData = tansformedModel.Transform(データビュー)。
  
  VaRの予測= mlcontext.Data.CreateEnumerable <ProductSalesPrediction> www.baiyytwg.com(transformedData、reuseRowObject:偽);
  
  Console.WriteLineを(www.huichengtxgw.com "アラート\ tScore \ TP-値");
  
  foreachの(予測でのvar P)
  
  {
  
  IF(p.Prediction [www.jintianxuesha.com] == 1)
  
  {
  
  Console.BackgroundColor = ConsoleColor.DarkYellow。
  
  Console.ForegroundColor = ConsoleColor.Black。
  
  }
  
  Console.WriteLineを( "{0} \ T {1:0.00} \ www.qjljdgt.cn {2:0.00} www.xgjrfwsc.cn"、p.Prediction [0]、p.Prediction [1]、P。予測[2])。
  
  Console.ResetColor();
  
  ButeBuf动态扩容
  
  容量默认值:256字节、最大值:Integer.MAX_VALUEの(2G)
  
  AbstractByteBufAllocator.calculateNewCapacity:writeXXX方法はAbstractByteBuf.ensureWritable0()メソッドをチェック容量算出方法で呼び出したとき
  
  の最小容量要件、計算の対応する二組:以下4メガバイト:先頭から64のバイトは、1ずつインクリメントされます回、newCapacity例えば新しい要件満たすように計算された最小容量まで:データの10のバイトを書き込むために継続、電流の大きさ256、250が書き込まれているが、必要な最小容量要件が261で、新しい容量が64x2x2x2 = 512で
  
  新しい:4メガバイト以上新しい最小必要容量=容量/ 4 + 4兆兆兆X4例:現在のサイズが3 MB、MB 3で書かれている、5メガバイトの容量の必要な最小サイズ2 MBを記述し続け、新しい容量8メガバイト(超えませんMAX)
  
  源4メガバイト:AbstractByteBufAllocator.CALCULATE_THRESHOLD固定閾値
  
  ByteBufが実装
  
  IMGを
  
  使用中でも、メモリ管理が設けられているByteBufAllocatorディスペンサーを介して適用
  
  PooledByteBufオブジェクト、メモリ再利用
  
  PooledByteBufAllocatorインスタンスメンテナンス:PooledThreadCache変数を複数の分類MemoryRegionCスレッド キャッシュメモリ、MemoryRegionCache内部リンクリスト、チャックを保持キューなどの痛み配列。:メモリ参照メモリアプローチを維持PoolChuckメモリPooledByteBufAllocator.ioBufferコーミング動作プロセスチャックにメモリBUFポイントを再利用することである
  
  IMG
  
  ゼロコピーメカニズムを
  
  網状ゼロコピーメカニズムは、アプリケーション層を実現することであり、JVM、オペレーティングシステムに関連する過度のメモリメカニズムの基礎となります。
  
  CompositeByteBuf、論理ByteBufに組み合わせる複数ByteBuf各ByteBuf間コピーを回避
  
  IMG
  
  wrapedBuffer()メソッド、ByteBufバイト[]アレイパッケージ化オブジェクト
  
  IMG
  
  スライス()メソッドは、オブジェクトが複数に切断されByteBuf ByteBufオブジェクト
  
  IMGの
  
  コード例:
  
  パブリッククラスZeroCopyTest {
  
  パブリック静的無効メイン(文字列[] args){
  
  ; ByteBuf Unpooled.bufferバッファ1 =(7)
  
  (7)buffer1.writeByte;
  
  ByteBuf Unpooled.bufferバッファ2 =(7)、
  
  バッファ2を.writeByte(13である);
  
  CompositeByteBuf compositeByteBuf Unpooled.compositeBuffer =();
  
  CompositeByteBuf newBuf = compositeByteBuf.addComponents(trueに、バッファ1、バッファ2)
  
  のSystem.out.println( "CompositeByteBuf:" + newBuf)。
  
  バイト[]バイト= {1、2、3}。
  
  ByteBuf wrappedBuffer = Unpooled.wrappedBuffer(バイト)。
  
  System.out.println( "wrappedBuffer:" + wrappedBuffer.getByte(2))。
  
  バイト[2] = 7。
  
  System.out.println( "wrappedBuffer:" + wrappedBuffer.getByte(2))。
  
  ByteBuf BUF = Unpooled.wrappedBuffer( "網状" .getBytes())。
  
  ByteBufスライス= buf.slice(1、2)。
  
  slice.unwrap();
  
  System.out.println( "スライス:" +スライス)。
  
  }

おすすめ

転載: www.cnblogs.com/dakunqq/p/11628481.html