Exemplo n.º 1
0
        private bool ReadData(Stream stream, bool readData)
        {
            if ((_header = ID3v2Header.FromStream(stream)) != null)
            {
                byte[] buffer = new byte[_header.DataLength];
                if (stream.Read(buffer, 0, buffer.Length) < buffer.Length)
                {
                    return(false);
                }

                if (_header.IsUnsync && _header.Version != ID3Version.ID3v2_4)
                {
                    buffer = UnSyncBuffer(buffer);
                }
                _content = buffer;
                MemoryStream contentStream = new MemoryStream(buffer);

                switch (_header.Version)
                {
                case ID3Version.ID3v2_2:
                    Parse2(contentStream);
                    break;

                case ID3Version.ID3v2_3:
                    Parse3(contentStream);
                    break;

                case ID3Version.ID3v2_4:
                    Parse4(contentStream);
                    break;

                default:
                    throw new ID3Exception("Invalid Version: [2.{0};{1}]", _header.RawVersion[0], _header.RawVersion[1]);
                }

                if (readData)
                {
                    ReadFrames(contentStream);
                }

                contentStream.Dispose();

                return(true);
            }
            else
            {
                Debug.WriteLine("ID3v2::ReadData: No ID3v2 Header found.");
                return(false);
            }
        }
Exemplo n.º 2
0
        public static ID3v2Header FromStream(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (!stream.CanRead)
            {
                throw new ArgumentException("stream not readable");
            }
            if (!stream.CanSeek)
            {
                throw new ArgumentException("stream not seekable");
            }

            BinaryReader reader = new BinaryReader(stream);
            ID3v2Header  header;

            byte[] buffer = new byte[HeaderLength];
            int    read   = stream.Read(buffer, 0, buffer.Length);

            if (read < 10)
            {
                throw new EndOfStreamException();
            }

            if (buffer[0] == 0x49 && //I
                buffer[1] == 0x44 && //D
                buffer[2] == 0x33)
            {
                header = new ID3v2Header();

                header.Version    = (ID3Version)buffer[3];
                header.RawVersion = new byte[] { buffer[3], buffer[4] };
                header.Flags      = (ID3v2HeaderFlags)buffer[5];

                /*header.DataLength = buffer[6] * (1 << 21);
                 * header.DataLength += buffer[7] * (1 << 14);
                 * header.DataLength += buffer[8] * (1 << 7);
                 * header.DataLength += buffer[9];
                 */
                header.DataLength = ID3Utils.ReadInt32(buffer, 6, true);

                return(header);
            }

            stream.Position -= read;
            return(null);
        }
Exemplo n.º 3
0
        public static ID3v2Header FromStream(Stream stream)
        {
            if (stream == null) throw new ArgumentNullException("stream");
            if (!stream.CanRead) throw new ArgumentException("stream not readable");
            if (!stream.CanSeek) throw new ArgumentException("stream not seekable");

            BinaryReader reader = new BinaryReader(stream);
            ID3v2Header header;

            byte[] buffer = new byte[HeaderLength];
            int read = stream.Read(buffer, 0, buffer.Length);
            if (read < 10)
                throw new EndOfStreamException();

            if (buffer[0] == 0x49 && //I
               buffer[1] == 0x44 && //D
               buffer[2] == 0x33)
            {
                header = new ID3v2Header();

                header.Version = (ID3Version)buffer[3];
                header.RawVersion = new byte[] { buffer[3], buffer[4] };
                header.Flags = (ID3v2HeaderFlags)buffer[5];

                /*header.DataLength = buffer[6] * (1 << 21);
                header.DataLength += buffer[7] * (1 << 14);
                header.DataLength += buffer[8] * (1 << 7);
                header.DataLength += buffer[9];
                */
                header.DataLength = ID3Utils.ReadInt32(buffer, 6, true);

                return header;
            }

            stream.Position -= read;
            return null;
        }