ExcelToolKit.BinaryFormat.XlsStream.ReadStream C# (CSharp) Method

ReadStream() public method

Reads stream data from file
public ReadStream ( ) : byte[]
return byte[]
        public byte[] ReadStream() {
            uint sector=m_startSector, prevSector=0;
            int sectorSize=m_isMini?m_hdr.MiniSectorSize:m_hdr.SectorSize;
            XlsFat fat=m_isMini?m_minifat:m_fat;
            long offset=0;
            if (m_isMini&&m_rootDir!=null) {
                offset=(m_rootDir.RootEntry.StreamFirstSector+1)*m_hdr.SectorSize;
            }

            var buff=new byte[sectorSize];
            byte[] ret;

            using (var ms=new MemoryStream(sectorSize*8)) {
                lock (m_fileStream) {
                    do {
                        if (prevSector==0||(sector-prevSector)!=1) {
                            uint adjustedSector=m_isMini?sector:sector+1;
                            //standard sector is + 1 because header is first
                            m_fileStream.Seek(adjustedSector*sectorSize+offset, SeekOrigin.Begin);
                        }

                        if (prevSector!=0&&prevSector==sector)
                            throw new InvalidOperationException("The excel file may be corrupt. We appear to be stuck");

                        prevSector=sector;
                        m_fileStream.Read(buff, 0, sectorSize);
                        ms.Write(buff, 0, sectorSize);

                        sector=fat.GetNextSector(sector);

                        if (sector==0)
                            throw new InvalidOperationException("Next sector cannot be 0. Possibly corrupt excel file");
                    } while (sector!=(uint)FATMARKERS.FAT_EndOfChain);
                }

                ret=ms.ToArray();
            }

            return ret;
        }
    }

Usage Example

Beispiel #1
0
        /// <summary>
        /// Creates Root Directory catalog from XlsHeader
        /// </summary>
        /// <param name="hdr">XlsHeader object</param>
        public XlsRootDirectory(XlsHeader hdr) {
            var stream=new XlsStream(hdr, hdr.RootDirectoryEntryStart, false, null);
            var array=stream.ReadStream();
            var entries=new List<XlsDirectoryEntry>();
            for (int i=0; i<array.Length; i+=XlsDirectoryEntry.Length) {
                var tmp = new byte[XlsDirectoryEntry.Length];
                Buffer.BlockCopy(array, i, tmp, 0, tmp.Length);
                entries.Add(new XlsDirectoryEntry(tmp, hdr));
            }
            m_entries=entries;
            foreach (var entry in entries){
                if (m_root==null&&entry.EntryType==STGTY.STGTY_ROOT){
                    m_root = entry;
                }

                if (entry.ChildSid!=(uint)FATMARKERS.FAT_FreeSpace){
                    entry.Child = entries[(int) entry.ChildSid];
                }

                if (entry.LeftSiblingSid!=(uint)FATMARKERS.FAT_FreeSpace){
                    entry.LeftSibling = entries[(int) entry.LeftSiblingSid];
                }

                if (entry.RightSiblingSid!=(uint)FATMARKERS.FAT_FreeSpace){
                    entry.RightSibling = entries[(int) entry.RightSiblingSid];
                }
            }
            stream.CalculateMiniFat(this);
        }
All Usage Examples Of ExcelToolKit.BinaryFormat.XlsStream::ReadStream