@@ -669,4 +669,101 @@ public static byte[] SubArray(this byte[] bytes, int startIndex, int length)
669669 }
670670
671671 #endregion
672+
673+ #region Primitive Type Read/Write
674+
675+ /// <summary>
676+ /// 将一个字节写入指定的缓冲区,并更新偏移量。
677+ /// </summary>
678+ /// <param name="buffer">要写入的缓冲区。</param>
679+ /// <param name="value">要写入的值。</param>
680+ /// <param name="offset">要写入值的缓冲区中的偏移量。</param>
681+ /// <exception cref="ArgumentNullException">当 <paramref name="buffer"/> 为 null 时抛出。</exception>
682+ /// <exception cref="ArgumentOutOfRangeException">当 <paramref name="offset"/> 为负数或缓冲区空间不足时抛出。</exception>
683+ public static void WriteByteValue ( this byte [ ] buffer , byte value , ref int offset )
684+ {
685+ ValidateBounds ( buffer , offset , 1 ) ;
686+ buffer [ offset ] = value ;
687+ offset += 1 ;
688+ }
689+
690+ /// <summary>
691+ /// 从字节数组中读取一个字节,并将偏移量前移。
692+ /// </summary>
693+ /// <param name="buffer">要读取的字节数组。</param>
694+ /// <param name="offset">引用偏移量。</param>
695+ /// <returns>返回读取的字节。</returns>
696+ /// <exception cref="ArgumentNullException">当 <paramref name="buffer"/> 为 null 时抛出。</exception>
697+ /// <exception cref="ArgumentOutOfRangeException">当 <paramref name="offset"/> 为负数或读取位置超出缓冲区边界时抛出。</exception>
698+ public static byte ReadByteValue ( this byte [ ] buffer , ref int offset )
699+ {
700+ ValidateBounds ( buffer , offset , 1 ) ;
701+ var value = buffer [ offset ] ;
702+ offset += 1 ;
703+ return value ;
704+ }
705+
706+ /// <summary>
707+ /// 将一个带符号字节写入指定的缓冲区,并更新偏移量。
708+ /// </summary>
709+ /// <param name="buffer">要写入的缓冲区。</param>
710+ /// <param name="value">要写入的值。</param>
711+ /// <param name="offset">要写入值的缓冲区中的偏移量。</param>
712+ /// <exception cref="ArgumentNullException">当 <paramref name="buffer"/> 为 null 时抛出。</exception>
713+ /// <exception cref="ArgumentOutOfRangeException">当 <paramref name="offset"/> 为负数或缓冲区空间不足时抛出。</exception>
714+ public static void WriteSByteValue ( this byte [ ] buffer , sbyte value , ref int offset )
715+ {
716+ ValidateBounds ( buffer , offset , 1 ) ;
717+ buffer [ offset ] = ( byte ) value ;
718+ offset += 1 ;
719+ }
720+
721+ /// <summary>
722+ /// 从字节数组中读取一个带符号字节,并将偏移量前移。
723+ /// </summary>
724+ /// <param name="buffer">要读取的字节数组。</param>
725+ /// <param name="offset">引用偏移量。</param>
726+ /// <returns>返回读取的带符号字节。</returns>
727+ /// <exception cref="ArgumentNullException">当 <paramref name="buffer"/> 为 null 时抛出。</exception>
728+ /// <exception cref="ArgumentOutOfRangeException">当 <paramref name="offset"/> 为负数或读取位置超出缓冲区边界时抛出。</exception>
729+ public static sbyte ReadSByteValue ( this byte [ ] buffer , ref int offset )
730+ {
731+ ValidateBounds ( buffer , offset , 1 ) ;
732+ var value = ( sbyte ) buffer [ offset ] ;
733+ offset += 1 ;
734+ return value ;
735+ }
736+
737+ /// <summary>
738+ /// 将一个布尔值写入指定的缓冲区,并更新偏移量。
739+ /// </summary>
740+ /// <param name="buffer">要写入的缓冲区。</param>
741+ /// <param name="value">要写入的值。</param>
742+ /// <param name="offset">要写入值的缓冲区中的偏移量。</param>
743+ /// <exception cref="ArgumentNullException">当 <paramref name="buffer"/> 为 null 时抛出。</exception>
744+ /// <exception cref="ArgumentOutOfRangeException">当 <paramref name="offset"/> 为负数或缓冲区空间不足时抛出。</exception>
745+ public static void WriteBoolValue ( this byte [ ] buffer , bool value , ref int offset )
746+ {
747+ ValidateBounds ( buffer , offset , 1 ) ;
748+ buffer [ offset ] = value ? ( byte ) 1 : ( byte ) 0 ;
749+ offset += 1 ;
750+ }
751+
752+ /// <summary>
753+ /// 从字节数组中读取一个布尔值,并将偏移量前移。
754+ /// </summary>
755+ /// <param name="buffer">要读取的字节数组。</param>
756+ /// <param name="offset">引用偏移量。</param>
757+ /// <returns>返回读取的布尔值。</returns>
758+ /// <exception cref="ArgumentNullException">当 <paramref name="buffer"/> 为 null 时抛出。</exception>
759+ /// <exception cref="ArgumentOutOfRangeException">当 <paramref name="offset"/> 为负数或读取位置超出缓冲区边界时抛出。</exception>
760+ public static bool ReadBoolValue ( this byte [ ] buffer , ref int offset )
761+ {
762+ ValidateBounds ( buffer , offset , 1 ) ;
763+ var value = buffer [ offset ] != 0 ;
764+ offset += 1 ;
765+ return value ;
766+ }
767+
768+ #endregion
672769}
0 commit comments