/// <exception cref="System.IO.IOException"></exception>
 private void AssertNoCrLfHelper(string expect, string input)
 {
     byte[] inbytes = Sharpen.Runtime.GetBytesForString(input);
     byte[] expectBytes = Sharpen.Runtime.GetBytesForString(expect);
     for (int i = 0; i < 5; ++i)
     {
         byte[] buf = new byte[i];
         InputStream @in = new ByteArrayInputStream(inbytes);
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         OutputStream @out = new AutoCRLFOutputStream(bos);
         if (i > 0)
         {
             int n;
             while ((n = @in.Read(buf)) >= 0)
             {
                 @out.Write(buf, 0, n);
             }
         }
         else
         {
             int c;
             while ((c = @in.Read()) != -1)
             {
                 @out.Write(c);
             }
         }
         @out.Flush();
         @in.Close();
         @out.Close();
         byte[] actualBytes = bos.ToByteArray();
         NUnit.Framework.Assert.AreEqual(Encode(expectBytes), Encode(actualBytes), "bufsize="
              + i);
     }
 }
 public void TestCreatePropertySets()
 {
     Type[] expected = new Type[]
     {
         typeof(NoPropertySetStreamException),
         typeof(SummaryInformation),
         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(o.GetType(), expected[i]);
     }
 }
Esempio n. 3
0
 public static IList <Tree> ConvertToTrees(byte[] input)
 {
     try
     {
         IList <Tree>         output = new List <Tree>();
         ByteArrayInputStream bis    = new ByteArrayInputStream(input);
         GZIPInputStream      gis    = new GZIPInputStream(bis);
         ObjectInputStream    ois    = new ObjectInputStream(gis);
         int size = ErasureUtils.UncheckedCast <int>(ois.ReadObject());
         for (int i = 0; i < size; ++i)
         {
             string rawTree = ErasureUtils.UncheckedCast(ois.ReadObject());
             Tree   tree    = Tree.ValueOf(rawTree, trf);
             tree.SetSpans();
             output.Add(tree);
         }
         ois.Close();
         gis.Close();
         bis.Close();
         return(output);
     }
     catch (IOException e)
     {
         throw new RuntimeIOException(e);
     }
     catch (TypeLoadException e)
     {
         throw new Exception(e);
     }
 }
Esempio n. 4
0
                internal override void AssertCompression(string name, Compressor compressor, Decompressor
                                                         decompressor, byte[] originalRawData)
                {
                    byte[] buf = null;
                    ByteArrayInputStream    bytesIn = null;
                    BlockDecompressorStream blockDecompressorStream = null;
                    ByteArrayOutputStream   bytesOut = new ByteArrayOutputStream();

                    // close without write
                    try
                    {
                        compressor.Reset();
                        // decompressor.end();
                        BlockCompressorStream blockCompressorStream = new BlockCompressorStream(bytesOut,
                                                                                                compressor, 1024, 0);
                        blockCompressorStream.Close();
                        // check compressed output
                        buf = bytesOut.ToByteArray();
                        int emSize = this.emptySize[compressor.GetType()];
                        Assert.Equal(this.joiner.Join(name, "empty stream compressed output size != "
                                                      + emSize), emSize, buf.Length);
                        // use compressed output as input for decompression
                        bytesIn = new ByteArrayInputStream(buf);
                        // create decompression stream
                        blockDecompressorStream = new BlockDecompressorStream(bytesIn, decompressor, 1024
                                                                              );
                        // no byte is available because stream was closed
                        Assert.Equal(this.joiner.Join(name, " return value is not -1")
                                     , -1, blockDecompressorStream.Read());
                    }
                    catch (IOException e)
                    {
                        NUnit.Framework.Assert.Fail(this.joiner.Join(name, e.Message));
                    }
                    finally
                    {
                        if (blockDecompressorStream != null)
                        {
                            try
                            {
                                bytesOut.Close();
                                blockDecompressorStream.Close();
                                bytesIn.Close();
                                blockDecompressorStream.Close();
                            }
                            catch (IOException)
                            {
                            }
                        }
                    }
                }
Esempio n. 5
0
        public virtual void TestCompressorDecompressorEmptyStreamLogic()
        {
            ByteArrayInputStream  bytesIn  = null;
            ByteArrayOutputStream bytesOut = null;

            byte[] buf = null;
            BlockDecompressorStream blockDecompressorStream = null;

            try
            {
                // compress empty stream
                bytesOut = new ByteArrayOutputStream();
                BlockCompressorStream blockCompressorStream = new BlockCompressorStream(bytesOut,
                                                                                        new Lz4Compressor(), 1024, 0);
                // close without write
                blockCompressorStream.Close();
                // check compressed output
                buf = bytesOut.ToByteArray();
                Assert.Equal("empty stream compressed output size != 4", 4, buf
                             .Length);
                // use compressed output as input for decompression
                bytesIn = new ByteArrayInputStream(buf);
                // create decompression stream
                blockDecompressorStream = new BlockDecompressorStream(bytesIn, new Lz4Decompressor
                                                                          (), 1024);
                // no byte is available because stream was closed
                Assert.Equal("return value is not -1", -1, blockDecompressorStream
                             .Read());
            }
            catch (Exception e)
            {
                NUnit.Framework.Assert.Fail("testCompressorDecompressorEmptyStreamLogic ex error !!!"
                                            + e.Message);
            }
            finally
            {
                if (blockDecompressorStream != null)
                {
                    try
                    {
                        bytesIn.Close();
                        bytesOut.Close();
                        blockDecompressorStream.Close();
                    }
                    catch (IOException)
                    {
                    }
                }
            }
        }
Esempio n. 6
0
        /**
         * Read back a workbook that was written out to a memory buffer with
         * {@link #writeOut(Workbook)} or {@link #writeOutAndClose(Workbook)}.
         *
         * @param file the workbook file to read
         * @return the read back workbook
         * @throws IOException
         */
        public static XSSFWorkbook ReadBack(ByteArrayOutputStream out1)
        {
            InputStream is1 = new ByteArrayInputStream(out1.ToByteArray());

            out1.Close();
            try
            {
                return(new XSSFWorkbook(is1));
            }
            finally
            {
                is1.Close();
            }
        }
Esempio n. 7
0
        public virtual double ReadDouble()
        {
            FillBuffer(8);

            var byteArrayInputStream = new ByteArrayInputStream(ReadSwapped(8));
            var dataInputStream      = new DataInputStream(byteArrayInputStream);

            var result = dataInputStream.ReadDouble();

            byteArrayInputStream.Close();
            dataInputStream.Close();

            return(result);
        }
Esempio n. 8
0
        public virtual float ReadSingle()
        {
            FillBuffer(4);

            var byteArrayInputStream = new ByteArrayInputStream(ReadSwapped(4));
            var dataInputStream      = new DataInputStream(byteArrayInputStream);

            var result = dataInputStream.ReadFloat();

            byteArrayInputStream.Close();
            dataInputStream.Close();

            return(result);
        }
Esempio n. 9
0
        public virtual void TestSkipFully()
        {
            byte[] inArray           = new byte[] { 0, 1, 2, 3, 4 };
            ByteArrayInputStream @in = new ByteArrayInputStream(inArray);

            try
            {
                @in.Mark(inArray.Length);
                IOUtils.SkipFully(@in, 2);
                IOUtils.SkipFully(@in, 2);
                try
                {
                    IOUtils.SkipFully(@in, 2);
                    NUnit.Framework.Assert.Fail("expected to get a PrematureEOFException");
                }
                catch (EOFException e)
                {
                    Assert.Equal("Premature EOF from inputStream " + "after skipping 1 byte(s)."
                                 , e.Message);
                }
                @in.Reset();
                try
                {
                    IOUtils.SkipFully(@in, 20);
                    NUnit.Framework.Assert.Fail("expected to get a PrematureEOFException");
                }
                catch (EOFException e)
                {
                    Assert.Equal("Premature EOF from inputStream " + "after skipping 5 byte(s)."
                                 , e.Message);
                }
                @in.Reset();
                IOUtils.SkipFully(@in, 5);
                try
                {
                    IOUtils.SkipFully(@in, 10);
                    NUnit.Framework.Assert.Fail("expected to get a PrematureEOFException");
                }
                catch (EOFException e)
                {
                    Assert.Equal("Premature EOF from inputStream " + "after skipping 0 byte(s)."
                                 , e.Message);
                }
            }
            finally
            {
                @in.Close();
            }
        }
        public virtual void TestProtoBatchModifier(ModelBatch batch)
        {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            batch.WriteToStream(byteArrayOutputStream);
            byteArrayOutputStream.Close();
            byte[] bytes = byteArrayOutputStream.ToByteArray();
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            ModelBatch           recovered            = new ModelBatch(byteArrayInputStream, null);

            byteArrayInputStream.Close();
            NUnit.Framework.Assert.AreEqual(batch.Count, recovered.Count);
            for (int i = 0; i < batch.Count; i++)
            {
                NUnit.Framework.Assert.AreEqual("true", recovered[i].GetModelMetaDataByReference()["testing"]);
            }
        }
        public virtual void TestProtoBatch(ModelBatch batch)
        {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            batch.WriteToStream(byteArrayOutputStream);
            byteArrayOutputStream.Close();
            byte[] bytes = byteArrayOutputStream.ToByteArray();
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            ModelBatch           recovered            = new ModelBatch(byteArrayInputStream);

            byteArrayInputStream.Close();
            NUnit.Framework.Assert.AreEqual(batch.Count, recovered.Count);
            for (int i = 0; i < batch.Count; i++)
            {
                NUnit.Framework.Assert.IsTrue(batch[i].ValueEquals(recovered[i], 1.0e-5));
            }
        }
Esempio n. 12
0
        protected void InternalLoadContent(string content, string url, string contentType)
        {
            if (!IsHandleCreated)
            {
                WpfExtensions.DoEvents();
            }
            using (var sContentType = new nsACString(contentType))
                using (var sUtf8 = new nsACString("UTF8")) {
                    ByteArrayInputStream inputStream = null;
                    try {
                        inputStream = ByteArrayInputStream.Create(System.Text.Encoding.UTF8.GetBytes(content != null ? content : string.Empty));

                        var    docShell = Xpcom.QueryInterface <nsIDocShell> (this.WebBrowser);
                        nsIURI uri      = null;
                        if (!string.IsNullOrEmpty(url))
                        {
                            uri = IOService.CreateNsIUri(url);
                        }
                        nsIDocShellLoadInfo l = null;
                        if (true)
                        {
                            l = Xpcom.QueryInterface <nsIDocShellLoadInfo> (this.WebBrowser);

                            docShell.CreateLoadInfo(ref l);

                            l.SetLoadTypeAttribute(new IntPtr(16));
                        }

                        docShell.LoadStream(inputStream, uri, sContentType, sUtf8, l);
                        Marshal.ReleaseComObject(docShell);
                        if (l != null)
                        {
                            Marshal.ReleaseComObject(l);
                        }
                    } finally {
                        if (inputStream != null)
                        {
                            inputStream.Close();
                        }
                    }
                }
        }
        public virtual void TestProtoBatchWithoutFactors(ModelBatch batch)
        {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            batch.WriteToStreamWithoutFactors(byteArrayOutputStream);
            byteArrayOutputStream.Close();
            byte[] bytes = byteArrayOutputStream.ToByteArray();
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            ModelBatch           recovered            = new ModelBatch(byteArrayInputStream);

            byteArrayInputStream.Close();
            NUnit.Framework.Assert.AreEqual(batch.Count, recovered.Count);
            for (int i = 0; i < batch.Count; i++)
            {
                NUnit.Framework.Assert.AreEqual(0, recovered[i].factors.Count);
                NUnit.Framework.Assert.IsTrue(batch[i].GetModelMetaDataByReference().Equals(recovered[i].GetModelMetaDataByReference()));
                for (int j = 0; j < batch[i].GetVariableSizes().Length; j++)
                {
                    NUnit.Framework.Assert.IsTrue(batch[i].GetVariableMetaDataByReference(j).Equals(recovered[i].GetVariableMetaDataByReference(j)));
                }
            }
        }
Esempio n. 14
0
		public virtual float ReadSingle() {
			FillBuffer(4);

            var byteArrayInputStream = new ByteArrayInputStream(ReadSwapped(4));
            var dataInputStream = new DataInputStream(byteArrayInputStream);

            var result = dataInputStream.ReadFloat();

            byteArrayInputStream.Close();
            dataInputStream.Close();

            return result;
		}
Esempio n. 15
0
        public virtual void TestCorruptedIFile()
        {
            int  fetcher             = 7;
            Path onDiskMapOutputPath = new Path(name.GetMethodName() + "/foo");
            Path shuffledToDisk      = OnDiskMapOutput.GetTempPath(onDiskMapOutputPath, fetcher);

            fs = FileSystem.GetLocal(job).GetRaw();
            MapOutputFile mof = Org.Mockito.Mockito.Mock <MapOutputFile>();
            OnDiskMapOutput <Text, Text> odmo = new OnDiskMapOutput <Text, Text>(map1ID, id, mm
                                                                                 , 100L, job, mof, fetcher, true, fs, onDiskMapOutputPath);
            string                mapData = "MAPDATA12345678901234567890";
            ShuffleHeader         header  = new ShuffleHeader(map1ID.ToString(), 14, 10, 1);
            ByteArrayOutputStream bout    = new ByteArrayOutputStream();
            DataOutputStream      dos     = new DataOutputStream(bout);
            IFileOutputStream     ios     = new IFileOutputStream(dos);

            header.Write(dos);
            int headerSize = dos.Size();

            try
            {
                ios.Write(Sharpen.Runtime.GetBytesForString(mapData));
            }
            finally
            {
                ios.Close();
            }
            int dataSize = bout.Size() - headerSize;
            // Ensure that the OnDiskMapOutput shuffler can successfully read the data.
            MapHost host             = new MapHost("TestHost", "http://test/url");
            ByteArrayInputStream bin = new ByteArrayInputStream(bout.ToByteArray());

            try
            {
                // Read past the shuffle header.
                bin.Read(new byte[headerSize], 0, headerSize);
                odmo.Shuffle(host, bin, dataSize, dataSize, metrics, Reporter.Null);
            }
            finally
            {
                bin.Close();
            }
            // Now corrupt the IFile data.
            byte[] corrupted = bout.ToByteArray();
            corrupted[headerSize + (dataSize / 2)] = unchecked ((int)(0x0));
            try
            {
                bin = new ByteArrayInputStream(corrupted);
                // Read past the shuffle header.
                bin.Read(new byte[headerSize], 0, headerSize);
                odmo.Shuffle(host, bin, dataSize, dataSize, metrics, Reporter.Null);
                NUnit.Framework.Assert.Fail("OnDiskMapOutput.shuffle didn't detect the corrupted map partition file"
                                            );
            }
            catch (ChecksumException e)
            {
                Log.Info("The expected checksum exception was thrown.", e);
            }
            finally
            {
                bin.Close();
            }
            // Ensure that the shuffled file can be read.
            IFileInputStream iFin = new IFileInputStream(fs.Open(shuffledToDisk), dataSize, job
                                                         );

            try
            {
                iFin.Read(new byte[dataSize], 0, dataSize);
            }
            finally
            {
                iFin.Close();
            }
        }
        /// <summary>
        /// Restores this object from a stream (i.e., deserializes it).
        /// </summary>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void readObject(java.io.ObjectInputStream ois) throws java.io.IOException, ClassNotFoundException
        private void ReadObject(java.io.ObjectInputStream ois)
        {
            CertificateFactory cf;
            Dictionary <String, CertificateFactory> cfs = null;

            ois.DefaultReadObject();

            if (Type == null)
            {
                throw new NullPointerException("type can't be null");
            }

            // process any new-style certs in the stream (if present)
            int size = ois.ReadInt();

            if (size > 0)
            {
                // we know of 3 different cert types: X.509, PGP, SDSI, which
                // could all be present in the stream at the same time
                cfs        = new Dictionary <String, CertificateFactory>(3);
                this.Certs = new java.security.cert.Certificate[size];
            }

            for (int i = 0; i < size; i++)
            {
                // read the certificate type, and instantiate a certificate
                // factory of that type (reuse existing factory if possible)
                String certType = ois.ReadUTF();
                if (cfs.ContainsKey(certType))
                {
                    // reuse certificate factory
                    cf = cfs[certType];
                }
                else
                {
                    // create new certificate factory
                    try
                    {
                        cf = CertificateFactory.GetInstance(certType);
                    }
                    catch (CertificateException)
                    {
                        throw new ClassNotFoundException("Certificate factory for " + certType + " not found");
                    }
                    // store the certificate factory so we can reuse it later
                    cfs[certType] = cf;
                }
                // parse the certificate
                sbyte[] encoded = null;
                try
                {
                    encoded = new sbyte[ois.ReadInt()];
                }
                catch (OutOfMemoryError)
                {
                    throw new IOException("Certificate too big");
                }
                ois.ReadFully(encoded);
                ByteArrayInputStream bais = new ByteArrayInputStream(encoded);
                try
                {
                    this.Certs[i] = cf.GenerateCertificate(bais);
                }
                catch (CertificateException ce)
                {
                    throw new IOException(ce.Message);
                }
                bais.Close();
            }
        }
Esempio n. 17
0
		public virtual double ReadDouble() {
			FillBuffer(8);

            var byteArrayInputStream = new ByteArrayInputStream(ReadSwapped(8));
            var dataInputStream = new DataInputStream(byteArrayInputStream);

            var result = dataInputStream.ReadDouble();

            byteArrayInputStream.Close();
            dataInputStream.Close();

			return result;
		}