Public feed #378519

Untitled

public text v1 · immutable
Raw Edit
·published 2007-03-02 15:49 UTC
rendered paste body

        public virtual void importImgfs(FileInfo imgFile)
        {
            FileStream osf = null;
            FileStream img = null;

            Console.WriteLine("...Importing ImageFS");

            if (OsFile == null)
                return;

            try
            {
                long position = guessBestPositionStart();
                long end = SupportClass.FileLength(OsFile);
                long size = end - position;

                osf = SupportClass.RandomAccessFileSupport.CreateRandomAccessFile(OsFile, "rw");
                img = new FileStream(imgFile.FullName, FileMode.Open, FileAccess.Read);

                long addCount = getImgfsStartBlock();
                byte[] endByte = { 255, 255, 251, 255 };
                byte[] bytes2 = new byte[512];
                byte[] byteFF = new byte[520];

                for (int i = 0; i < 520; i++)
                    byteFF[i] = 255;

                long actualPositionStart = position - getImgfsShiftBlock() * 520L;
                for (long fillpos = actualPositionStart; fillpos < position; fillpos += 520L)
                {
                    osf.Seek(fillpos, SeekOrigin.Begin);
                    osf.Write(byteFF, 0, byteFF.Length);
                }

                long ccc = (getContentEnd(imgFile, position, imgFile.Length, 512) * 520L) / 512L + guessBestPositionStart();

                for (long j = 0L; j < size; j += 520L)
                {
                    long currentPos = position + j;
                    if (currentPos < ccc)
                    {
                        img.Read(bytes2, 0, bytes2.Length);
                        osf.Seek(currentPos, SeekOrigin.Begin);

                        osf.Write(bytes2, 0, bytes2.Length);
                        byte[] temp = SupportClass.ToByteArray( decimalToBlkNum(addCount) );
                        osf.Write(temp, 0, temp.Length);
                        osf.Write(endByte, 0, endByte.Length);

                        if (j % 0x1fbd00L == 0L) 
                            Console.Write("."); 

                        addCount++;
                    }
                    else 
                        osf.Write(byteFF, 0, byteFF.Length); 
                }

                Console.WriteLine("...done! (size: {0})", Convert.ToInt32(size));
            }
            catch (Exception e)
            {
                Console.Write("...Error: {0}", e.Message);
            }

            if (osf != null)
                osf.Close();
            if (img != null)
                img.Close();
        }
        private long getContentEnd(FileInfo imgFile, long position, long end, int testLength)
        {
            FileStream img = SupportClass.RandomAccessFileSupport.CreateRandomAccessFile(imgFile, "r"); ;
            byte[] bytes = { 255, 255, 255, 255, 255, 255, 255, 255,
                            255, 255, 255, 255, 255, 255, 255, 255,};
            int skip = bytes.Length;
            int remainTestLength = testLength - skip;
            byte[] testByte = new byte[skip];
            byte[] byte2 = new byte[remainTestLength];
            byte[] testByte2 = new byte[remainTestLength];

            for (int i = 0; i < remainTestLength; i++) 
                byte2[i] = 255; 

            for (long j = testLength; j < end; j += testLength)
            {
                img.Seek(j, SeekOrigin.Begin );

                Console.WriteLine(j);
                img.Read(testByte, 0, testByte.Length);                
                if ( bytes.Equals(testByte) )
                {
                    img.Read(testByte2, 0, testByte2.Length);
                    if (byte2.Equals(testByte2))
                    {
                        long k = 0L;

                        do
                        {
                            img.Read(testByte, 0, testByte.Length);
                            img.Read(testByte2, 0, testByte2.Length);

                            k += testLength;
                
                            if (k >= end)
                                return j;
                        }
                        while (k < end && bytes.Equals(testByte) && 
                            byte2.Equals( testByte2) );
                    }
                }
            }
            return end;
        }