Ejemplo n.º 1
0
        private Int32 writePictures(BinaryWriter w, IList <PictureInfo> pictures, Boolean isLast)
        {
            var   result = 0;
            Int64 sizePos, dataPos, finalPos;
            Byte  blockType;

            foreach (var picture in pictures)
            {
                blockType = META_PICTURE;
                if (isLast)
                {
                    blockType = (Byte)(blockType & 0x80);
                }

                w.Write(blockType);
                sizePos = w.BaseStream.Position;
                w.Write(new Byte[] { 0, 0, 0 }); // Placeholder for 24-bit integer that will be rewritten at the end of the method

                dataPos = w.BaseStream.Position;
                vorbisTag.WritePicture(w, picture.PictureData, picture.NativeFormat, ImageUtils.GetMimeTypeFromImageFormat(picture.NativeFormat), picture.PicType.Equals(PictureInfo.PIC_TYPE.Unsupported) ? picture.NativePicCode : ID3v2.EncodeID3v2PictureType(picture.PicType), picture.Description);

                finalPos = w.BaseStream.Position;
                w.BaseStream.Seek(sizePos, SeekOrigin.Begin);
                w.Write(StreamUtils.EncodeBEUInt24((UInt32)(finalPos - dataPos)));
                w.BaseStream.Seek(finalPos, SeekOrigin.Begin);
                result++;
            }

            return(result);
        }
Ejemplo n.º 2
0
        private Int32 writeVorbisTag(BinaryWriter w, TagData tag, Boolean isLast)
        {
            Int32 result;
            Int64 sizePos, dataPos, finalPos;
            var   blockType = META_VORBIS_COMMENT;

            if (isLast)
            {
                blockType = (Byte)(blockType & 0x80);
            }

            w.Write(blockType);
            sizePos = w.BaseStream.Position;
            w.Write(new Byte[] { 0, 0, 0 }); // Placeholder for 24-bit integer that will be rewritten at the end of the method

            dataPos = w.BaseStream.Position;
            result  = vorbisTag.Write(w.BaseStream, tag);

            finalPos = w.BaseStream.Position;
            w.BaseStream.Seek(sizePos, SeekOrigin.Begin);
            w.Write(StreamUtils.EncodeBEUInt24((UInt32)(finalPos - dataPos)));
            w.BaseStream.Seek(finalPos, SeekOrigin.Begin);

            return(result);
        }
Ejemplo n.º 3
0
        public void StreamUtils_BE24UintConverters()
        {
            uint intValue = 0x00873529;

            Assert.AreEqual((uint)0x00FFFFFF, StreamUtils.DecodeBEUInt24(new byte[3] {
                0xFF, 0xFF, 0xFF
            }));

            byte[] byteValue = StreamUtils.EncodeBEUInt24(intValue);
            Assert.AreEqual(intValue, StreamUtils.DecodeBEUInt24(byteValue));
        }
Ejemplo n.º 4
0
        private WriteResult writeVorbisCommentBlock(BinaryWriter w, TagData tag)
        {
            long sizePos, dataPos, finalPos;

            w.Write(META_VORBIS_COMMENT);
            sizePos = w.BaseStream.Position;
            w.Write(new byte[] { 0, 0, 0 }); // Placeholder for 24-bit integer that will be rewritten at the end of the method

            dataPos = w.BaseStream.Position;
            int writtenFields = vorbisTag.Write(w.BaseStream, tag);

            finalPos = w.BaseStream.Position;
            w.BaseStream.Seek(sizePos, SeekOrigin.Begin);
            w.Write(StreamUtils.EncodeBEUInt24((uint)(finalPos - dataPos)));
            w.BaseStream.Seek(finalPos, SeekOrigin.Begin);

            return(new WriteResult(WriteMode.REPLACE, writtenFields));
        }
Ejemplo n.º 5
0
        private int writePictureBlock(BinaryWriter w, PictureInfo picture)
        {
            long sizePos, dataPos, finalPos;

            w.Write(META_PICTURE);

            sizePos = w.BaseStream.Position;
            w.Write(new byte[] { 0, 0, 0 }); // Placeholder for 24-bit integer that will be rewritten at the end of the method

            dataPos = w.BaseStream.Position;
            vorbisTag.WritePicture(w, picture.PictureData, picture.NativeFormat, ImageUtils.GetMimeTypeFromImageFormat(picture.NativeFormat), picture.PicType.Equals(PictureInfo.PIC_TYPE.Unsupported) ? picture.NativePicCode : ID3v2.EncodeID3v2PictureType(picture.PicType), picture.Description);

            finalPos = w.BaseStream.Position;
            w.BaseStream.Seek(sizePos, SeekOrigin.Begin);
            w.Write(StreamUtils.EncodeBEUInt24((uint)(finalPos - dataPos)));
            w.BaseStream.Seek(finalPos, SeekOrigin.Begin);

            return(1);
        }
Ejemplo n.º 6
0
        private WriteResult writePaddingBlock(BinaryWriter w, long cumulativeDelta)
        {
            long paddingSizeToWrite = TrackUtils.ComputePaddingSize(initialPaddingOffset, initialPaddingSize, -cumulativeDelta);

            if (paddingSizeToWrite > 0)
            {
                w.Write(META_PADDING);
                w.Write(StreamUtils.EncodeBEUInt24((uint)paddingSizeToWrite));
                for (int i = 0; i < paddingSizeToWrite; i++)
                {
                    w.Write((byte)0);
                }
                return(new WriteResult(WriteMode.REPLACE, 1));
            }
            else
            {
                return(new WriteResult(WriteMode.REPLACE, 0));
            }
        }
Ejemplo n.º 7
0
        private int writePictures(BinaryWriter w, IList <PictureInfo> pictures, bool isLast)
        {
            int  result = 0;
            long sizePos, dataPos, finalPos;
            byte blockType;

            foreach (PictureInfo picture in pictures)
            {
                // Picture has either to be supported, or to come from the right tag standard
                bool doWritePicture = !picture.PicType.Equals(PictureInfo.PIC_TYPE.Unsupported);
                if (!doWritePicture)
                {
                    doWritePicture = (MetaDataIOFactory.TAG_NATIVE == picture.TagType);
                }
                // It also has not to be marked for deletion
                doWritePicture = doWritePicture && (!picture.MarkedForDeletion);

                if (doWritePicture)
                {
                    blockType = META_PICTURE;
                    if (isLast)
                    {
                        blockType = (byte)(blockType & 0x80);
                    }

                    w.Write(blockType);
                    sizePos = w.BaseStream.Position;
                    w.Write(new byte[] { 0, 0, 0 }); // Placeholder for 24-bit integer that will be rewritten at the end of the method

                    dataPos = w.BaseStream.Position;
                    vorbisTag.WritePicture(w, picture.PictureData, picture.NativeFormat, ImageUtils.GetMimeTypeFromImageFormat(picture.NativeFormat), picture.PicType.Equals(PictureInfo.PIC_TYPE.Unsupported) ? picture.NativePicCode : ID3v2.EncodeID3v2PictureType(picture.PicType), picture.Description);

                    finalPos = w.BaseStream.Position;
                    w.BaseStream.Seek(sizePos, SeekOrigin.Begin);
                    w.Write(StreamUtils.EncodeBEUInt24((uint)(finalPos - dataPos)));
                    w.BaseStream.Seek(finalPos, SeekOrigin.Begin);
                    result++;
                }
            }

            return(result);
        }
Ejemplo n.º 8
0
        public void StreamUtils_Exceptions()
        {
            Assert.IsFalse(StreamUtils.ArrEqualsArr(new byte[1], new byte[2]));
            Assert.IsFalse(StreamUtils.StringEqualsArr(".", new char[2]));

            try
            {
                StreamUtils.DecodeBEUInt16(new byte[1]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeUInt16(new byte[1]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeInt16(new byte[1]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeBEInt16(new byte[1]);
                Assert.Fail();
            }
            catch { }


            try
            {
                StreamUtils.DecodeBEInt24(new byte[2]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeBEUInt24(new byte[2]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.EncodeBEUInt24(0x01FFFFFF);
                Assert.Fail();
            }
            catch { }


            try
            {
                StreamUtils.DecodeBEUInt32(new byte[3]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeUInt32(new byte[3]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeBEInt32(new byte[3]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeInt32(new byte[3]);
                Assert.Fail();
            }
            catch { }


            try
            {
                StreamUtils.DecodeUInt64(new byte[7]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeBEInt64(new byte[7]);
                Assert.Fail();
            }
            catch { }


            try
            {
                StreamUtils.DecodeSynchSafeInt(new byte[6]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeSynchSafeInt32(new byte[6]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.EncodeSynchSafeInt(1, 0);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.EncodeSynchSafeInt(1, 6);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.ReadBits(new BinaryReader(new MemoryStream()), 0, 0);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.ReadBits(new BinaryReader(new MemoryStream()), 0, 33);
                Assert.Fail();
            }
            catch { }
        }