Пример #1
0
        //the number of bytes already returned
        //private long pos = 0;

        //the marked position
        //private long mark = -1;

        // flag if close shoud be propagated
        //private bool propagateClose = true;
        /// <summary>
        /// Creates a new <c>BoundedInputStream</c> that wraps the given input
        /// stream and limits it to a certain size.
        /// </summary>
        /// <param name="in1">The wrapped input stream</param>
        /// <param name="size">The maximum number of bytes to return</param>
        public BoundedInputStream(ByteArrayInputStream in1, long size)
        {
            IsPropagateClose = true;
            // Some badly designed methods - eg the servlet API - overload length
            // such that "-1" means stream finished
            this.max = size;
            this.in1 = in1;
        }
Пример #2
0
        public void TestReadAllFiles1()
        {
            //string dataDir = @"..\..\..\TestCases\HPSF\data\";
            POIDataSamples _samples = POIDataSamples.GetHPSFInstance();
            string[] files = _samples.GetFiles();

            try
            {
                for (int i = 0; i < files.Length; i++)
                {
                    if (files[i].EndsWith("1"))
                        continue;

                    Console.WriteLine("Reading file \"" + files[i] + "\"");

                    using (FileStream f = new FileStream(files[i], FileMode.Open, FileAccess.Read))
                    {
                        /* Read the POI filesystem's property Set streams: */
                        POIFile[] psf1 = Util.ReadPropertySets(f);

                        for (int j = 0; j < psf1.Length; j++)
                        {
                            Stream in1 =
                                new ByteArrayInputStream(psf1[j].GetBytes());
                            PropertySet a = PropertySetFactory.Create(in1);
                        }
                        f.Close();
                    }
                }
            }
            catch (Exception t)
            {
                String s = t.ToString();
                Assert.Fail(s);
            }
        }
Пример #3
0
        /// <summary>
        /// Loob XML faili (XDocument objekt), kasutades NPOI.dll-i
        /// </summary>
        /// <param name="dbytes">Exceli fail baitidena</param>
        /// <param name="symbol">Aktsiasümbol</param>
        /// <returns>XML faili sisu XDocument objektina</returns>
        public XDocument GetData(byte[] dbytes, string symbol)
        {
            try
            {

                ByteArrayInputStream bais = new ByteArrayInputStream(dbytes);
                XDocument xDoc = new XDocument();
                HSSFWorkbook hwb = new HSSFWorkbook(bais);
                ISheet sheet = hwb.GetSheetAt(0);
                AddDataNameRowVariableMappingsCurDoc(sheet.SheetName);
                //MessageBox.Show(IsNameRowMappingValid(sheet).ToString());

                IRow quartersDataRow = findQuartersDataRow(sheet);
                if (quartersDataRow != null)
                {
                    int[] quarterColumnIndexes = FindQuarterColumnIndexes(quartersDataRow);

                    if (quarterColumnIndexes.Count() > 0)
                    {

                        XElement xTable;
                        XElement xColumn;
                        XAttribute curAttribute;
                        ICell curCell;
                        string curData;
                        XElement xDatabase = new XElement("database");
                        for (int i = 0; i < quarterColumnIndexes.Count(); i++)
                        {
                            xTable = new XElement("table");
                            xColumn = new XElement("column", symbol);
                            curAttribute = new XAttribute("name", "is_symbol");
                            xColumn.Add(curAttribute);
                            xTable.Add(xColumn);
                            for (int j = 0; j < _dataNameRowVariableMappings.Count; j++)
                            {
                                curCell = sheet.GetRow(_dataNameRowVariableMappings.ElementAt(j).Value).GetCell(quarterColumnIndexes[i]);
                                curCell.SetCellType(CellType.STRING);
                                if (String.IsNullOrEmpty(curCell.StringCellValue))
                                {
                                    curData = "NULL";
                                }
                                else
                                {
                                    curData = curCell.StringCellValue;
                                }
                                xColumn = new XElement("column", curData);
                                curAttribute = new XAttribute("name", _dataNameRowVariableMappings.ElementAt(j).Key.Value);
                                xColumn.Add(curAttribute);
                                xTable.Add(xColumn);
                            }
                            xDatabase.Add(xTable);
                        }
                        xDoc.Add(xDatabase);
                        return xDoc;
                    }
                }
                return null;

            }
            catch (IOException e)
            {
                MessageBox.Show(e.Message);
                return null;
            }
        }
Пример #4
0
        public void TestDictionary()
        {
            FileStream copy = File.Create( @"\Test-HPSF.ole2");
            //copy.deleteOnExit();

            /* Write: */
            FileStream out1 = copy;
            POIFSFileSystem poiFs = new POIFSFileSystem();
            MutablePropertySet ps1 = new MutablePropertySet();
            MutableSection s = (MutableSection)ps1.Sections[0];
            Hashtable m = new Hashtable(3, 1.0f);
            m[1] = "String 1";
            m[2] = "String 2";
            m[3] = "String 3";
            s.Dictionary = (m);
            s.SetFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID1);
            int codepage = (int)Constants.CP_UNICODE;
            s.SetProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2,
                          codepage);
            poiFs.CreateDocument(ps1.GetStream(), "Test");
            poiFs.WriteFileSystem(out1);

            /* Read back: */
            POIFile[] psf = Util.ReadPropertySets(copy);
            Assert.AreEqual(1, psf.Length);
            byte[] bytes = psf[0].GetBytes();
            Stream in1 = new ByteArrayInputStream(bytes);
            PropertySet ps2 = PropertySetFactory.Create(in1);

            /* Check if the result is a DocumentSummaryInformation stream, as
             * specified. */
            Assert.IsTrue(ps2.IsDocumentSummaryInformation);

            /* Compare the property Set stream with the corresponding one
             * from the origin file and check whether they are equal. */
            Assert.IsTrue(ps1.Equals(ps2));

            out1.Close();
            copy.Close();
            File.Delete( @"\Test-HPSF.ole2");

        }
Пример #5
0
        /**
         * Performs the check described in {@link #TestReCreate()} for a single
         * POI filesystem.
         *
         * @param f the POI filesystem to check
         */
        private void TestRecreate(FileInfo f)
        {
            Console.WriteLine("Recreating file \"" + f.Name + "\"");
            
            /* Read the POI filesystem's property Set streams: */
            POIFile[] psf1 = Util.ReadPropertySets(_samples.GetFile(f.Name));

            /* Create a new POI filesystem containing the origin file's
             * property Set streams: */
            FileInfo fi = new FileInfo(f.Name);
            FileStream copy = File.Create(fi.Name);
            //copy.deleteOnExit();
            FileStream out1 = copy;
            POIFSFileSystem poiFs = new POIFSFileSystem();
            for (int i = 0; i < psf1.Length; i++)
            {
                Stream in1 =
                    new ByteArrayInputStream(psf1[i].GetBytes());
                PropertySet psIn = PropertySetFactory.Create(in1);
                MutablePropertySet psOut = new MutablePropertySet(psIn);
                MemoryStream psStream =
                    new MemoryStream();
                psOut.Write(psStream);
                psStream.Close();
                byte[] streamData = psStream.ToArray();
                poiFs.CreateDocument(new ByteArrayInputStream(streamData),
                                     psf1[i].GetName());
                poiFs.WriteFileSystem(out1);
            }

            /* Read the property Set streams from the POI filesystem just
             * Created. */
            POIFile[] psf2 = Util.ReadPropertySets(copy);
            for (int i = 0; i < psf2.Length; i++)
            {
                byte[] bytes1 = psf1[i].GetBytes();
                byte[] bytes2 = psf2[i].GetBytes();
                Stream in1 = new ByteArrayInputStream(bytes1);
                Stream in2 = new ByteArrayInputStream(bytes2);
                PropertySet ps1 = PropertySetFactory.Create(in1);
                PropertySet ps2 = PropertySetFactory.Create(in2);

                /* Compare the property Set stream with the corresponding one
                 * from the origin file and check whether they are equal. */
                
                Assert.AreEqual(ps1, ps2, "Equality for file " + f.Name);
            }
            out1.Close();
        }
        public void TestBadSectorAllocationTableSize_bug48085()
        {
            int BLOCK_SIZE = 512;
            POIFSBigBlockSize bigBlockSize = POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS;
            Assert.AreEqual(BLOCK_SIZE, bigBlockSize.GetBigBlockSize());

            // 512 bytes take from the start of bugzilla attachment 24444
            byte[] initData = HexRead.ReadFromString(

            "D0 CF 11 E0 A1 B1 1A E1 20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20 3E 20 03 20 FE FF 09 20" +
            "06 20 20 20 20 20 20 20 20 20 20 20 01 20 20 20  01 20 20 20 20 20 20 20 20 10 20 20 02 20 20 20" +
            "02 20 20 20 FE FF FF FF 20 20 20 20 20 20 20 20  "
            );
            // the rest of the block is 'FF'
            byte[] data = new byte[BLOCK_SIZE];
            Arrays.Fill(data, (byte)0xFF);
            Array.Copy(initData, 0, data, 0, initData.Length);

            // similar code to POIFSFileSystem.<init>:
            Stream stream = new ByteArrayInputStream(data);
            HeaderBlock hb;
            RawDataBlockList dataBlocks;
            try
            {
                hb = new HeaderBlock(stream);
                dataBlocks = new RawDataBlockList(stream, bigBlockSize);
            }
            catch (IOException e)
            {
                throw new RuntimeException(e);
            }
            try
            {
                new BlockAllocationTableReader(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS,
                      hb.BATCount, hb.BATArray, hb.XBATCount,
                        hb.XBATIndex, dataBlocks);
            }
            catch (IOException e)
            {
                // expected during successful test
                Assert.AreEqual("Block count 538976257 is too high. POI maximum is 65535.", e.Message);
            }
            catch (OutOfMemoryException e)
            {
                if (e.StackTrace.Contains("testBadSectorAllocationTableSize"))
                {
                    throw new AssertionException("Identified bug 48085");
                }
            }
        }
Пример #7
0
 public void TestCreatePropertySets()
 {
     Type[] expected = new Type[]
     {
         typeof(SummaryInformation),
         typeof(DocumentSummaryInformation),
         typeof(NoPropertySetStreamException),
         typeof(NoPropertySetStreamException),
         typeof(NoPropertySetStreamException)
     };
     for (int i = 0; i < expected.Length; i++)
     {
         Stream in1 = new ByteArrayInputStream(poiFiles[i].GetBytes());
         Object o;
         try
         {
             o = PropertySetFactory.Create(in1);
         }
         catch (NoPropertySetStreamException ex)
         {
             o = ex;
         }
         catch (MarkUnsupportedException ex)
         {
             o = ex;
         }
         in1.Close();
         Assert.AreEqual(expected[i], o.GetType());
     }
 }
Пример #8
0
        public void TestAreDocumentsIdentical()
        {
            POIFSFileSystem fs = new POIFSFileSystem();
            DirectoryEntry dirA = fs.CreateDirectory("DirA");
            DirectoryEntry dirB = fs.CreateDirectory("DirB");

            DocumentEntry entryA1 = dirA.CreateDocument("Entry1", new ByteArrayInputStream(dataSmallA));
            DocumentEntry entryA1b = dirA.CreateDocument("Entry1b", new ByteArrayInputStream(dataSmallA));
            DocumentEntry entryA2 = dirA.CreateDocument("Entry2", new ByteArrayInputStream(dataSmallB));
            DocumentEntry entryB1 = dirB.CreateDocument("Entry1", new ByteArrayInputStream(dataSmallA));


            // Names must match
            Assert.AreEqual(false, entryA1.Name.Equals(entryA1b.Name));
            Assert.AreEqual(false, EntryUtils.AreDocumentsIdentical(entryA1, entryA1b));

            // Contents must match
            Assert.AreEqual(false, EntryUtils.AreDocumentsIdentical(entryA1, entryA2));

            // Parents don't matter if contents + names are the same
            Assert.AreEqual(false, entryA1.Parent.Equals(entryB1.Parent));
            Assert.AreEqual(true, EntryUtils.AreDocumentsIdentical(entryA1, entryB1));


            // Can work with NPOIFS + POIFS
            //ByteArrayOutputStream tmpO = new ByteArrayOutputStream();
            MemoryStream tmpO = new MemoryStream();
            fs.WriteFileSystem(tmpO);
            ByteArrayInputStream tmpI = new ByteArrayInputStream(tmpO.ToArray());
            NPOIFSFileSystem nfs = new NPOIFSFileSystem(tmpI);

            DirectoryEntry dN1 = (DirectoryEntry)nfs.Root.GetEntry("DirA");
            DirectoryEntry dN2 = (DirectoryEntry)nfs.Root.GetEntry("DirB");
            DocumentEntry eNA1 = (DocumentEntry)dN1.GetEntry(entryA1.Name);
            DocumentEntry eNA2 = (DocumentEntry)dN1.GetEntry(entryA2.Name);
            DocumentEntry eNB1 = (DocumentEntry)dN2.GetEntry(entryB1.Name);

            Assert.AreEqual(false, EntryUtils.AreDocumentsIdentical(eNA1, eNA2));
            Assert.AreEqual(true, EntryUtils.AreDocumentsIdentical(eNA1, eNB1));

            Assert.AreEqual(false, EntryUtils.AreDocumentsIdentical(eNA1, entryA1b));
            Assert.AreEqual(false, EntryUtils.AreDocumentsIdentical(eNA1, entryA2));

            Assert.AreEqual(true, EntryUtils.AreDocumentsIdentical(eNA1, entryA1));
            Assert.AreEqual(true, EntryUtils.AreDocumentsIdentical(eNA1, entryB1));
        }
Пример #9
0
 /// <summary>
 /// Creates a new <c>BoundedInputStream</c> that wraps the given input
 /// stream and is unlimited.
 /// </summary>
 /// <param name="in1">The wrapped input stream</param>
 public BoundedInputStream(ByteArrayInputStream in1)
     : this(in1, -1)
 {
     ;
 }