/// <summary> /// 从流中读入一个缓冲数组 /// </summary> /// <param name="stream">要读取的流</param> /// <param name="length">读取的字节长度</param> /// <param name="lengthRequired">是否要求指定的字节数,如果读取不到,则抛出异常</param> /// <returns>缓冲数组</returns> public static byte[] ReadBuffer(this Stream stream, int length, bool lengthRequired = true) { var count = 0; var buffer = stream.ReadBuffer(length, out count); if (count < length && lengthRequired) { throw new EndOfStreamException(); } return(buffer); }
/// <summary> /// 从流中读入一个 <see cref="T:System.Int16"/> /// </summary> /// <param name="stream">要读取的流</param> /// <returns>读取的 <see cref="T:System.Int16"/></returns> public static short ReadInt16(this System.IO.Stream stream) { return(BitConverter.ToInt16(stream.ReadBuffer(2), 0)); }
/// <summary> /// 从流中读入一个 <see cref="T:System.Int64"/> /// </summary> /// <param name="stream">要读取的流</param> /// <returns>读取的 <see cref="T:System.Int64"/></returns> public static double ReadDouble(this Stream stream) { return(BitConverter.ToDouble(stream.ReadBuffer(sizeof(double)), 0)); }
/// <summary> /// 从流中读入一个 <see cref="T:System.Int64"/> /// </summary> /// <param name="stream">要读取的流</param> /// <returns>读取的 <see cref="T:System.Int64"/></returns> public static long ReadInt64(this Stream stream) { return(BitConverter.ToInt64(stream.ReadBuffer(8), 0)); }
/// <summary> /// 从流中读入一个 <see cref="T:System.Int32"/> /// </summary> /// <param name="stream">要读取的流</param> /// <returns>读取的 <see cref="T:System.Int32"/></returns> public static int ReadInt32(this Stream stream) { return(BitConverter.ToInt32(stream.ReadBuffer(4), 0)); }