public Sample(BinaryReader reader) : this() { SourceStart = reader.ReadInt32BE(); SourceLength = reader.ReadUInt16BE()*2; LoopStart = reader.ReadInt32BE(); Data = new byte[SourceLength]; }
/// <summary> /// Reads a JPEG image's dimensions and returns a <see cref="Size"/> object. Much faster /// than using the built in <see cref="System.Drawing.Image"/> class which loads the entire image into memory /// Rewinds the stream after reading. /// </summary> /// <param name="stream"></param> /// <exception cref="InvalidDataException">Thrown when the Stream is not a JPEG image</exception> /// <returns></returns> public static Size GetJpegImageSize(Stream stream) { if (!ImageTypeHelper.IsJpeg(stream)) throw new InvalidDataException("This is not a JPEG stream"); try { var br = new BinaryReader(stream); // skip the 0xFFD8 marker br.BaseStream.Seek(2, SeekOrigin.Current); // keep reading packets until we find one that contains Size info while (true) { ushort b1 = 0x00; while (b1 != 0xFF) b1 = br.ReadByte(); while (b1 == 0xFF) b1 = br.ReadByte(); if (b1 >= 0xC0 && b1 <= 0xC3) { // this is the SOF (Start Of Frame) marker. skip 3 bytes br.BaseStream.Seek(3, SeekOrigin.Current); var h = br.ReadUInt16BE(); var w = br.ReadUInt16BE(); return new Size(w, h); } else { // this isn't the SOF marker, skip to the next marker br.BaseStream.Seek(br.ReadUInt16BE() - 2, SeekOrigin.Current); } } } catch (EndOfStreamException ex) { throw new Exception("Hit the end of the stream without finding the dimensions. This file may be corrupt.", ex); } finally { if (stream != null && stream.CanSeek) stream.Seek(0, SeekOrigin.Begin); } }
public static NetworkAddress DecodeNetworkAddress(BinaryReader reader) { return new NetworkAddress ( Services: reader.ReadUInt64(), IPv6Address: reader.ReadExactly(16).ToImmutableArray(), Port: reader.ReadUInt16BE() ); }
#pragma warning disable CS0219 public override string GetTitle(BinaryReader fileStream) { // using the official id3 tag documentation // http://id3.org/id3v2.3.0#ID3_tag_version_2.3.0 int read_count = 10; // read + validate header [10 bytes] // skipped for TagID >03 bytes byte version_major = fileStream.ReadByte(); // >01 bytes byte version_minor = fileStream.ReadByte(); // >01 bytes byte data_flags = fileStream.ReadByte(); // >01 bytes byte[] tag_size = fileStream.ReadBytes(4); // >04 bytes int tag_size_int = 0; for (int i = 0; i < 4; i++) tag_size_int |= tag_size[3 - i] << (i * 7); read_count += 10; #region ID3v2 if (version_major == 2) { while (read_count < tag_size_int + 10) { // frame header [06 bytes] int frame_id = fileStream.ReadInt24BE(); // >03 bytes int frame_size = fileStream.ReadInt24BE(); // >03 bytes read_count += 6; if (frame_id == v2_TT2) { string title; byte[] textBuffer = fileStream.ReadBytes(frame_size); if (textBuffer[0] == 0) title = Encoding.GetEncoding(28591).GetString(textBuffer, 1, frame_size - 1); else throw new FormatException("The id3 tag is damaged"); return title; } else { fileStream.ReadBytes(frame_size); read_count += frame_size; } } throw new FormatException("The id3 tag contains no title"); } #endregion #region ID3v3/4 else if (version_major == 3 || version_major == 4) { while (read_count < tag_size_int + 10) { // frame header [10 bytes] uint frame_id = fileStream.ReadUInt32BE(); // >04 bytes int frame_size = fileStream.ReadInt32BE(); // >04 bytes ushort frame_flags = fileStream.ReadUInt16BE(); // >02 bytes read_count += 10; // content if (frame_id == v3_TIT2) { string title; byte[] textBuffer = fileStream.ReadBytes(frame_size); // is a string, so the first byte is a indicator byte switch (textBuffer[0]) { case 0: title = Encoding.GetEncoding(28591).GetString(textBuffer, 1, frame_size - 1); break; case 1: title = Encoding.Unicode.GetString(textBuffer, 1, frame_size - 1); break; case 2: title = new UnicodeEncoding(true, false).GetString(textBuffer, 1, frame_size - 1); break; case 3: title = Encoding.UTF8.GetString(textBuffer, 1, frame_size - 1); break; default: throw new FormatException("The id3 tag is damaged"); } return title; } else if (frame_id == 0) break; else { fileStream.ReadBytes(frame_size); read_count += frame_size; } } throw new FormatException("The id3 tag contains no title"); } #endregion return null; }