示例#1
0
文件: Tag.cs 项目: akinomyoga/afh
        public static void WriteToStream(Tag value, StreamAccessor accessor, out System.Action <long> SetSize)
        {
            long           pos_size;
            Stream         stream    = new MemoryStream();
            StreamAccessor accessor2 = new StreamAccessor(stream);
            StreamAccessor accessor3 = null;
            long           start     = 0L;

            if (value.has_ext)
            {
                value.WriteExtHeader(accessor2, out accessor3);
                start = stream.Position;
            }
            foreach (Frame frame in value.frames.Values)
            {
                accessor2.WriteAs <Frame>(frame);
            }
            if (value.has_ext)
            {
                long length = stream.Position - start;
                accessor3.Write(ID3Utils.CalculateCRC32(stream, start, length), EncodingType.U4BE);
                accessor3.Stream.Close();
            }
            bool unsync = ID3Utils.ID3RequiredUnsync(stream);

            if (unsync)
            {
                Stream stream2 = ID3Utils.ID3Unsynchronize(stream);
                stream.Close();
                stream = stream2;
            }
            value.WriteTagHeader(accessor, unsync, out pos_size);
            SetSize = delegate(long size){
                if (size > StreamAccessor.MaxUInt28)
                {
                    throw new System.ArgumentOutOfRangeException("Tag の情報量が多すぎて保存できません。Tag の内容は 256MB より小さくなる様にして下さい");
                }
                accessor.PushPosition(pos_size);
                accessor.Write((uint)size, EncodingType.UInt28BE);
                accessor.PopPosition();
            };
            accessor.WriteStream(stream);
        }
示例#2
0
文件: Tag.cs 项目: akinomyoga/afh
        public static Tag ReadFromStream(StreamAccessor accessor, out int tagsize)
        {
            Tag tag = new Tag();

            bool unsync;
            int  size;

            try{
                size    = tag.ReadTagHeader(accessor, out unsync);
                tagsize = size + 10;
            }catch (System.Exception e) {
                __dll__.log.WriteError(e, "TagHeader の読込に失敗しました。");
                tagsize = 0;
                return(null);
            }

            System.IO.Stream str = accessor.ReadSubStream((long)size);
            StreamAccessor   acc_str;

            if (unsync)
            {
                System.IO.Stream stream = ID3Utils.ID3ResolveUnsync(str);
                str.Close();
                acc_str = new StreamAccessor(stream);
            }
            else
            {
                acc_str = new StreamAccessor(str);
            }

            if (tag.has_ext)
            {
                uint crc32;
                int  padding;
                try{
                    tag.ReadExtHeader(acc_str, out crc32, out padding);
                }catch (Exception exception2) {
                    exception = exception2;
                    __dll__.log.WriteError(exception, "ExtendedHeader の読込に失敗しました。");
                    return(null);
                }
                if (tag.hascrc)
                {
                    long crc_start = acc_str.Stream.Position;
                    long crc_len   = (acc_str.Stream.Length - crc_start) - padding;
                    if (crc_len < 0L)
                    {
                        __dll__.log.WriteError(string.Format("Extended Header-Padding Size の値が不正です。大きすぎます:{0}", padding));
                        return(null);
                    }
                    if (crc32 != ID3Utils.CalculateCRC32(acc_str.Stream, crc_start, crc_len))
                    {
                        __dll__.log.WriteError("ファイルの CRC32 値が一致しませんでした。");
                        return(null);
                    }
                }
            }
            tag.frames = new afh.Collections.DictionaryP <string, Frame>();
            while (true)
            {
                Frame val = acc_str.Read <Frame>(EncodingType.NoSpecified);
                if (val == Frame.EndOfFrames)
                {
                    acc_str.Stream.Close();
                    return(tag);
                }
                tag.frames.Add(val.FrameId, val);
            }
        }