Example #1
0
        /// <summary>
        /// Download a resource from the specified URI.
        /// </summary>
        public byte[] DownloadData(URL address)
        {
            URLConnection connection = null;
            InputStream inputStream = null;
            try
            {
                connection = OpenConnection(address);
                inputStream = connection.InputStream;
                var memStream = new ByteArrayOutputStream();
                var buffer = new byte[BUFFER_SIZE];
                int len;

                while ((len = inputStream.Read(buffer, 0, BUFFER_SIZE)) > 0)
                {
                    memStream.Write(buffer, 0, len);
                }

                return memStream.ToByteArray();
            }
            finally
            {
                if (inputStream != null)
                    inputStream.Close();
                var httpConnection = connection as HttpURLConnection;
                if (httpConnection != null)
                    httpConnection.Disconnect();
            }
        }
Example #2
0
        /// <summary>Escape unprintable characters optionally URI-reserved characters</summary>
        /// <param name="s">The Java String to encode (may contain any character)</param>
        /// <param name="escapeReservedChars">true to escape URI reserved characters</param>
        /// <param name="encodeNonAscii">encode any non-ASCII characters</param>
        /// <returns>a URI-encoded string</returns>
        private static string Escape(string s, bool escapeReservedChars, bool encodeNonAscii
                                     )
        {
            if (s == null)
            {
                return(null);
            }
            ByteArrayOutputStream os = new ByteArrayOutputStream(s.Length);

            byte[] bytes;
            try
            {
                bytes = Sharpen.Runtime.GetBytesForString(s, Constants.CHARACTER_ENCODING);
            }
            catch (UnsupportedEncodingException e)
            {
                throw new RuntimeException(e);
            }
            // cannot happen
            for (int i = 0; i < bytes.Length; ++i)
            {
                int b = bytes[i] & unchecked ((int)(0xFF));
                if (b <= 32 || (encodeNonAscii && b > 127) || b == '%' || (escapeReservedChars &&
                                                                           reservedChars.Get(b)))
                {
                    os.Write('%');
                    byte[] tmp = Constants.EncodeASCII(string.Format("{0:x2}", Sharpen.Extensions.ValueOf
                                                                         (b)));
                    os.Write(tmp[0]);
                    os.Write(tmp[1]);
                }
                else
                {
                    os.Write(b);
                }
            }
            byte[] buf = os.ToByteArray();
            return(RawParseUtils.Decode(buf, 0, buf.Length));
        }
Example #3
0
        public void TestWriteReadProperties()
        {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            // Write them out
            NPOIFSFileSystem outFS = new NPOIFSFileSystem();

            doc.ReadProperties();
            doc.WriteProperties(outFS);
            outFS.WriteFileSystem(baos);

            // Create a new version
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.ToByteArray());
            OPOIFSFileSystem     inFS = new OPOIFSFileSystem(bais);

            // Check they're still there
            doc.SetDirectoryNode(inFS.Root);
            doc.ReadProperties();

            // Delegate test
            TestReadProperties();
        }
Example #4
0
        /**
         * Reads GIF image from stream
         *
         * @param is containing GIF file.
         * @return read status code (0 = no errors)
         */
        public int read(InputStream inputStream, int contentLength)
        {
            long startTime = DateTime.Now.Millisecond;

            if (inputStream != null)
            {
                try
                {
                    int capacity = (contentLength > 0) ? (contentLength + 4096) : 4096;
                    ByteArrayOutputStream buffer = new ByteArrayOutputStream(capacity);
                    int    nRead;
                    byte[] data = new byte[16384];
                    while ((nRead = inputStream.Read(data, 0, data.Length)) != -1)
                    {
                        buffer.Write(data, 0, nRead);
                    }
                    buffer.Flush();

                    read(buffer.ToByteArray());
                }
                catch (IOException e)
                {
                }
            }
            else
            {
                status = STATUS_OPEN_ERROR;
            }

            try
            {
                inputStream.Close();
            }
            catch (Exception e)
            {
            }

            return(status);
        }
Example #5
0
        public virtual void TestOneBlockAndHalf_Copy()
        {
            TemporaryBuffer b = new TemporaryBuffer.LocalFile();

            byte[] test = new TestRng(Sharpen.Extensions.GetTestName()).NextBytes(TemporaryBuffer.Block
                                                                                  .SZ * 3 / 2);
            try
            {
                ByteArrayInputStream @in = new ByteArrayInputStream(test);
                b.Write(@in.Read());
                b.Copy(@in);
                b.Close();
                NUnit.Framework.Assert.AreEqual(test.Length, b.Length());
                {
                    byte[] r = b.ToByteArray();
                    NUnit.Framework.Assert.IsNotNull(r);
                    NUnit.Framework.Assert.AreEqual(test.Length, r.Length);
                    for (int i = 0; i < test.Length; i++)
                    {
                        Assert.AreEqual(test[i], r[i]);
                    }
                }
                {
                    ByteArrayOutputStream o = new ByteArrayOutputStream();
                    b.WriteTo(o, null);
                    o.Close();
                    byte[] r = o.ToByteArray();
                    NUnit.Framework.Assert.AreEqual(test.Length, r.Length);
                    for (int i = 0; i < test.Length; i++)
                    {
                        Assert.AreEqual(test[i], r[i]);
                    }
                }
            }
            finally
            {
                b.Destroy();
            }
        }
Example #6
0
        public virtual void TestNoManifest()
        {
            FilePath dir = new FilePath(Runtime.GetProperty("test.build.dir", "target/test-dir"
                                                            ), typeof(TestJarFinder).FullName + "-testNoManifest");

            Delete(dir);
            dir.Mkdirs();
            FilePath   propsFile = new FilePath(dir, "props.properties");
            TextWriter writer    = new FileWriter(propsFile);

            new Properties().Store(writer, string.Empty);
            writer.Close();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            JarOutputStream       zos  = new JarOutputStream(baos);

            JarFinder.JarDir(dir, string.Empty, zos);
            JarInputStream jis = new JarInputStream(new ByteArrayInputStream(baos.ToByteArray
                                                                                 ()));

            NUnit.Framework.Assert.IsNotNull(jis.GetManifest());
            jis.Close();
        }
Example #7
0
        /// <summary>Format this builder's state as an annotated tag object.</summary>
        /// <remarks>Format this builder's state as an annotated tag object.</remarks>
        /// <returns>
        /// this object in the canonical annotated tag format, suitable for
        /// storage in a repository.
        /// </returns>
        public virtual byte[] Build()
        {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            OutputStreamWriter    w  = new OutputStreamWriter(os, Constants.CHARSET);

            try
            {
                w.Write("object ");
                GetObjectId().CopyTo(w);
                w.Write('\n');
                w.Write("type ");
                w.Write(Constants.TypeString(GetObjectType()));
                w.Write("\n");
                w.Write("tag ");
                w.Write(GetTag());
                w.Write("\n");
                if (GetTagger() != null)
                {
                    w.Write("tagger ");
                    w.Write(GetTagger().ToExternalString());
                    w.Write('\n');
                }
                w.Write('\n');
                if (GetMessage() != null)
                {
                    w.Write(GetMessage());
                }
                w.Close();
            }
            catch (IOException err)
            {
                // This should never occur, the only way to get it above is
                // for the ByteArrayOutputStream to throw, but it doesn't.
                //
                throw new RuntimeException(err);
            }
            return(os.ToByteArray());
        }
Example #8
0
        public virtual void TestInCoreLimit_SwitchBeforeAppendByte()
        {
            TemporaryBuffer b = new TemporaryBuffer.LocalFile();

            byte[] test = new TestRng(Sharpen.Extensions.GetTestName()).NextBytes(TemporaryBuffer
                                                                                  .DEFAULT_IN_CORE_LIMIT * 3);
            try
            {
                b.Write(test, 0, test.Length - 1);
                b.Write(test[test.Length - 1]);
                b.Close();
                NUnit.Framework.Assert.AreEqual(test.Length, b.Length());
                {
                    byte[] r = b.ToByteArray();
                    NUnit.Framework.Assert.IsNotNull(r);
                    NUnit.Framework.Assert.AreEqual(test.Length, r.Length);
                    for (int i = 0; i < test.Length; i++)
                    {
                        Assert.AreEqual(test[i], r[i]);
                    }
                }
                {
                    ByteArrayOutputStream o = new ByteArrayOutputStream();
                    b.WriteTo(o, null);
                    o.Close();
                    byte[] r = o.ToByteArray();
                    NUnit.Framework.Assert.AreEqual(test.Length, r.Length);
                    for (int i = 0; i < test.Length; i++)
                    {
                        Assert.AreEqual(test[i], r[i]);
                    }
                }
            }
            finally
            {
                b.Destroy();
            }
        }
Example #9
0
        /// <exception cref="System.IO.IOException"/>
        private void TestRead(int bufLen)
        {
            // compress empty stream
            bytesOut = new ByteArrayOutputStream();
            if (bufLen > 0)
            {
                bytesOut.Write(((byte[])ByteBuffer.Allocate(bufLen).PutInt(1024).Array()), 0, bufLen
                               );
            }
            BlockCompressorStream blockCompressorStream = new BlockCompressorStream(bytesOut,
                                                                                    new FakeCompressor(), 1024, 0);

            // close without any write
            blockCompressorStream.Close();
            // check compressed output
            buf = bytesOut.ToByteArray();
            Assert.Equal("empty file compressed output size is not " + (bufLen
                                                                        + 4), bufLen + 4, buf.Length);
            // use compressed output as input for decompression
            bytesIn = new ByteArrayInputStream(buf);
            // get decompression stream
            BlockDecompressorStream blockDecompressorStream = new BlockDecompressorStream(bytesIn
                                                                                          , new FakeDecompressor(), 1024);

            try
            {
                Assert.Equal("return value is not -1", -1, blockDecompressorStream
                             .Read());
            }
            catch (IOException e)
            {
                NUnit.Framework.Assert.Fail("unexpected IOException : " + e);
            }
            finally
            {
                blockDecompressorStream.Close();
            }
        }
Example #10
0
        /// <exception cref="System.Exception"/>
        public virtual void TestCopyFromHostWithRetry()
        {
            InMemoryMapOutput <Text, Text> immo = Org.Mockito.Mockito.Mock <InMemoryMapOutput>(
                );

            ss = Org.Mockito.Mockito.Mock <ShuffleSchedulerImpl>();
            Fetcher <Text, Text> underTest = new TestFetcher.FakeFetcher <Text, Text>(jobWithRetry
                                                                                      , id, ss, mm, r, metrics, except, key, connection, true);
            string replyHash = SecureShuffleUtils.GenerateHash(Sharpen.Runtime.GetBytesForString
                                                                   (encHash), key);

            Org.Mockito.Mockito.When(connection.GetResponseCode()).ThenReturn(200);
            Org.Mockito.Mockito.When(connection.GetHeaderField(SecureShuffleUtils.HttpHeaderReplyUrlHash
                                                               )).ThenReturn(replyHash);
            ShuffleHeader         header = new ShuffleHeader(map1ID.ToString(), 10, 10, 1);
            ByteArrayOutputStream bout   = new ByteArrayOutputStream();

            header.Write(new DataOutputStream(bout));
            ByteArrayInputStream @in = new ByteArrayInputStream(bout.ToByteArray());

            Org.Mockito.Mockito.When(connection.GetInputStream()).ThenReturn(@in);
            Org.Mockito.Mockito.When(connection.GetHeaderField(ShuffleHeader.HttpHeaderName))
            .ThenReturn(ShuffleHeader.DefaultHttpHeaderName);
            Org.Mockito.Mockito.When(connection.GetHeaderField(ShuffleHeader.HttpHeaderVersion
                                                               )).ThenReturn(ShuffleHeader.DefaultHttpHeaderVersion);
            Org.Mockito.Mockito.When(mm.Reserve(Matchers.Any <TaskAttemptID>(), Matchers.AnyLong
                                                    (), Matchers.AnyInt())).ThenReturn(immo);
            long retryTime = Time.MonotonicNow();

            Org.Mockito.Mockito.DoAnswer(new _Answer_375(retryTime)).When(immo).Shuffle(Matchers.Any
                                                                                        <MapHost>(), Matchers.Any <InputStream>(), Matchers.AnyLong(), Matchers.AnyLong()
                                                                                        , Matchers.Any <ShuffleClientMetrics>(), Matchers.Any <Reporter>());
            // Emulate host down for 3 seconds.
            underTest.CopyFromHost(host);
            Org.Mockito.Mockito.Verify(ss, Org.Mockito.Mockito.Never()).CopyFailed(Matchers.Any
                                                                                   <TaskAttemptID>(), Matchers.Any <MapHost>(), Matchers.AnyBoolean(), Matchers.AnyBoolean
                                                                                       ());
        }
        private byte[] getByteArrayFromfile(String filePath)
        {
            byte[] bytes = null;
            try
            {
                Java.IO.File          file = new Java.IO.File(filePath);
                FileInputStream       fis  = new FileInputStream(file);
                ByteArrayOutputStream bos  = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];

                for (int readNum; (readNum = fis.Read(buf)) != -1;)
                {
                    bos.Write(buf, 0, readNum);
                }

                bytes = bos.ToByteArray();
            }
            catch
            {
                return(bytes);
            }
            return(bytes);
        }
Example #12
0
        public static byte[] read(System.IO.Stream stream)
        {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            try
            {
                byte[] buffer = new byte[1024];
                int length;

                while ((length = stream.Read(buffer, 0, 1024)) >= 0)
                {
                    byteArrayOutputStream.Write(buffer, 0, length);
                }
                stream.Close();
                return byteArrayOutputStream.ToByteArray();
            }
            finally
            {
                byteArrayOutputStream.Flush();
                byteArrayOutputStream.Close();

            }
        }
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.TypeLoadException"/>
        internal static ConcatVectorBenchmark.SerializationReport ProtoSerializationBenchmark(ConcatVectorBenchmark.ConcatVectorConstructionRecord[] records)
        {
            ConcatVector[]        vectors = MakeVectors(records);
            ByteArrayOutputStream bArr    = new ByteArrayOutputStream();
            long before = Runtime.CurrentTimeMillis();

            for (int i = 0; i < vectors.Length; i++)
            {
                vectors[i].WriteToStream(bArr);
            }
            bArr.Close();
            byte[] bytes = bArr.ToByteArray();
            ByteArrayInputStream bArrIn = new ByteArrayInputStream(bytes);

            for (int i_1 = 0; i_1 < vectors.Length; i_1++)
            {
                ConcatVector.ReadFromStream(bArrIn);
            }
            ConcatVectorBenchmark.SerializationReport sr = new ConcatVectorBenchmark.SerializationReport();
            sr.time = Runtime.CurrentTimeMillis() - before;
            sr.size = bytes.Length;
            return(sr);
        }
Example #14
0
        public virtual void TestUTF8withBOM()
        {
            ByteArrayOutputStream bos1 = new ByteArrayOutputStream();

            bos1.Write(unchecked ((int)(0xEF)));
            bos1.Write(unchecked ((int)(0xBB)));
            bos1.Write(unchecked ((int)(0xBF)));
            bos1.Write(Sharpen.Runtime.GetBytesForString(CONTENT1, "UTF-8"));
            FilePath        file   = CreateFile(bos1.ToByteArray());
            FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);

            config.Load();
            NUnit.Framework.Assert.AreEqual(ALICE, config.GetString(USER, null, NAME));
            config.SetString(USER, null, NAME, BOB);
            config.Save();
            ByteArrayOutputStream bos2 = new ByteArrayOutputStream();

            bos2.Write(unchecked ((int)(0xEF)));
            bos2.Write(unchecked ((int)(0xBB)));
            bos2.Write(unchecked ((int)(0xBF)));
            bos2.Write(Sharpen.Runtime.GetBytesForString(CONTENT2, "UTF-8"));
            Assert.AssertArrayEquals(bos2.ToByteArray(), IOUtil.ReadFully(file));
        }
        public virtual void TestProtoTable(ConcatVector[][][] factor3)
        {
            ConcatVectorTable     concatVectorTable     = ConvertArrayToVectorTable((ConcatVector[][][])factor3);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            concatVectorTable.WriteToStream(byteArrayOutputStream);
            byteArrayOutputStream.Close();
            byte[] bytes = byteArrayOutputStream.ToByteArray();
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            ConcatVectorTable    recovered            = ConcatVectorTable.ReadFromStream(byteArrayInputStream);

            for (int i = 0; i < factor3.Length; i++)
            {
                for (int j = 0; j < factor3[0].Length; j++)
                {
                    for (int k = 0; k < factor3[0][0].Length; k++)
                    {
                        NUnit.Framework.Assert.IsTrue(factor3[i][j][k].ValueEquals(recovered.GetAssignmentValue(new int[] { i, j, k }).Get(), 1.0e-5));
                    }
                }
            }
            NUnit.Framework.Assert.IsTrue(concatVectorTable.ValueEquals(recovered, 1.0e-5));
        }
        public virtual void TestParse_explicit_bad_encoded2()
        {
            ByteArrayOutputStream b = new ByteArrayOutputStream();

            b.Write(Sharpen.Runtime.GetBytesForString("object 9788669ad918b6fcce64af8882fc9a81cb6aba67\n"
                                                      , "UTF-8"));
            b.Write(Sharpen.Runtime.GetBytesForString("type tree\n", "UTF-8"));
            b.Write(Sharpen.Runtime.GetBytesForString("tag v1.2.3.4.5\n", "UTF-8"));
            b.Write(Sharpen.Runtime.GetBytesForString("tagger F\u00f6r fattare <*****@*****.**> 1218123387 +0700\n"
                                                      , "UTF-8"));
            b.Write(Sharpen.Runtime.GetBytesForString("encoding ISO-8859-1\n", "UTF-8"));
            b.Write(Sharpen.Runtime.GetBytesForString("\n", "UTF-8"));
            b.Write(Sharpen.Runtime.GetBytesForString("\u304d\u308c\u3044\n", "UTF-8"));
            b.Write(Sharpen.Runtime.GetBytesForString("\n", "UTF-8"));
            b.Write(Sharpen.Runtime.GetBytesForString("Hi\n", "UTF-8"));
            RevTag c;

            c = new RevTag(Id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
            c.ParseCanonical(new RevWalk(db), b.ToByteArray());
            NUnit.Framework.Assert.AreEqual("F\u00f6r fattare", c.GetTaggerIdent().GetName());
            NUnit.Framework.Assert.AreEqual("\u304d\u308c\u3044", c.GetShortMessage());
            NUnit.Framework.Assert.AreEqual("\u304d\u308c\u3044\n\nHi\n", c.GetFullMessage());
        }
Example #17
0
        /// <exception cref="System.Exception"/>
        public virtual void TestReadWrite()
        {
            MultiFileSplit split = new MultiFileSplit(new JobConf(), new Path[] { new Path("/test/path/1"
                                                                                           ), new Path("/test/path/2") }, new long[] { 100, 200 });
            ByteArrayOutputStream bos = null;

            byte[] result = null;
            try
            {
                bos = new ByteArrayOutputStream();
                split.Write(new DataOutputStream(bos));
                result = bos.ToByteArray();
            }
            finally
            {
                IOUtils.CloseStream(bos);
            }
            MultiFileSplit       readSplit = new MultiFileSplit();
            ByteArrayInputStream bis       = null;

            try
            {
                bis = new ByteArrayInputStream(result);
                readSplit.ReadFields(new DataInputStream(bis));
            }
            finally
            {
                IOUtils.CloseStream(bis);
            }
            NUnit.Framework.Assert.IsTrue(split.GetLength() != 0);
            NUnit.Framework.Assert.AreEqual(split.GetLength(), readSplit.GetLength());
            NUnit.Framework.Assert.IsTrue(Arrays.Equals(split.GetPaths(), readSplit.GetPaths(
                                                            )));
            NUnit.Framework.Assert.IsTrue(Arrays.Equals(split.GetLengths(), readSplit.GetLengths
                                                            ()));
            System.Console.Out.WriteLine(split.ToString());
        }
Example #18
0
        public virtual void TestCopyFromHostWait()
        {
            Fetcher <Text, Text> underTest = new TestFetcher.FakeFetcher <Text, Text>(job, id,
                                                                                      ss, mm, r, metrics, except, key, connection);
            string replyHash = SecureShuffleUtils.GenerateHash(Sharpen.Runtime.GetBytesForString
                                                                   (encHash), key);

            Org.Mockito.Mockito.When(connection.GetResponseCode()).ThenReturn(200);
            Org.Mockito.Mockito.When(connection.GetHeaderField(SecureShuffleUtils.HttpHeaderReplyUrlHash
                                                               )).ThenReturn(replyHash);
            ShuffleHeader         header = new ShuffleHeader(map1ID.ToString(), 10, 10, 1);
            ByteArrayOutputStream bout   = new ByteArrayOutputStream();

            header.Write(new DataOutputStream(bout));
            ByteArrayInputStream @in = new ByteArrayInputStream(bout.ToByteArray());

            Org.Mockito.Mockito.When(connection.GetInputStream()).ThenReturn(@in);
            Org.Mockito.Mockito.When(connection.GetHeaderField(ShuffleHeader.HttpHeaderName))
            .ThenReturn(ShuffleHeader.DefaultHttpHeaderName);
            Org.Mockito.Mockito.When(connection.GetHeaderField(ShuffleHeader.HttpHeaderVersion
                                                               )).ThenReturn(ShuffleHeader.DefaultHttpHeaderVersion);
            //Defaults to null, which is what we want to test
            Org.Mockito.Mockito.When(mm.Reserve(Matchers.Any <TaskAttemptID>(), Matchers.AnyLong
                                                    (), Matchers.AnyInt())).ThenReturn(null);
            underTest.CopyFromHost(host);
            Org.Mockito.Mockito.Verify(connection).AddRequestProperty(SecureShuffleUtils.HttpHeaderUrlHash
                                                                      , encHash);
            Org.Mockito.Mockito.Verify(allErrs, Org.Mockito.Mockito.Never()).Increment(1);
            Org.Mockito.Mockito.Verify(ss, Org.Mockito.Mockito.Never()).CopyFailed(map1ID, host
                                                                                   , true, false);
            Org.Mockito.Mockito.Verify(ss, Org.Mockito.Mockito.Never()).CopyFailed(map2ID, host
                                                                                   , true, false);
            Org.Mockito.Mockito.Verify(ss).PutBackKnownMapOutput(Matchers.Any <MapHost>(), Matchers.Eq
                                                                     (map1ID));
            Org.Mockito.Mockito.Verify(ss).PutBackKnownMapOutput(Matchers.Any <MapHost>(), Matchers.Eq
                                                                     (map2ID));
        }
Example #19
0
        /// <summary>
        /// Tests a tuple writable with more than 64 values and the values set written
        /// spread far apart.
        /// </summary>
        /// <exception cref="System.Exception"/>
        public virtual void TestSparseWideWritable()
        {
            Writable[]    manyWrits = MakeRandomWritables(131);
            TupleWritable sTuple    = new TupleWritable(manyWrits);

            for (int i = 0; i < manyWrits.Length; i++)
            {
                if (i % 65 == 0)
                {
                    sTuple.SetWritten(i);
                }
            }
            ByteArrayOutputStream @out = new ByteArrayOutputStream();

            sTuple.Write(new DataOutputStream(@out));
            ByteArrayInputStream @in    = new ByteArrayInputStream(@out.ToByteArray());
            TupleWritable        dTuple = new TupleWritable();

            dTuple.ReadFields(new DataInputStream(@in));
            NUnit.Framework.Assert.IsTrue("Failed to write/read tuple", sTuple.Equals(dTuple)
                                          );
            NUnit.Framework.Assert.AreEqual("All tuple data has not been read from the stream"
                                            , -1, @in.Read());
        }
Example #20
0
 public virtual byte[] ConvertToBytes(IList <Tree> input)
 {
     try
     {
         ByteArrayOutputStream bos         = new ByteArrayOutputStream();
         GZIPOutputStream      gos         = new GZIPOutputStream(bos);
         ObjectOutputStream    oos         = new ObjectOutputStream(gos);
         IList <Tree>          transformed = CollectionUtils.TransformAsList(input, treeBasicCategories);
         IList <Tree>          filtered    = CollectionUtils.FilterAsList(transformed, treeFilter);
         oos.WriteObject(filtered.Count);
         foreach (Tree tree in filtered)
         {
             oos.WriteObject(tree.ToString());
         }
         oos.Close();
         gos.Close();
         bos.Close();
         return(bos.ToByteArray());
     }
     catch (IOException e)
     {
         throw new RuntimeIOException(e);
     }
 }
Example #21
0
        public static byte[] Generate(int heapAddr, SystemVersion version, int sourceAddr)
        {
            try
            {
                var payloadSize = 131072;
                var codegenAddr = 25165824;

                var output = new ByteArrayOutputStream();

                _switchToCore1(version, output);
                _copyCodebinToCodegen(codegenAddr, sourceAddr, payloadSize, version, output);
                _popr24Tor31(version.Constants.OsFatal(), version.Constants.Exit(), version.Constants.OsDynLoadAcquire(),
                             version.Constants.OsDynLoadFindExport(), version.Constants.OsSnprintf(), sourceAddr, 8, heapAddr,
                             version, output);

                Util.WriteU32(codegenAddr, output);
                Util.WriteU32(0, output);
                _copyCodebinToCodegen(codegenAddr, sourceAddr, payloadSize, version, output);

                _popr24Tor31(version.Constants.OsFatal(), version.Constants.Exit(), version.Constants.OsDynLoadAcquire(),
                             version.Constants.OsDynLoadFindExport(), version.Constants.OsSnprintf(), sourceAddr, 8, heapAddr,
                             version, output);

                Util.WriteU32(codegenAddr, output);

                output.Close();

                return(output.ToByteArray());
            }
            catch (Exception e)
            {
                Log.Debug("Offliine", e.Message);
            }

            return(null);
        }
Example #22
0
 public static byte[] getBytesFromFile(File f)
 {
     if (f == null)
     {
         return(null);
     }
     try
     {
         FileInputStream       stream    = new FileInputStream(f);
         ByteArrayOutputStream outStream = new ByteArrayOutputStream(1000);
         byte[] b = new byte[1000];
         for (int n; (n = stream.Read(b)) != -1;)
         {
             outStream.Write(b, 0, n);
         }
         stream.Close();
         outStream.Close();
         return(outStream.ToByteArray());
     }
     catch (IOException e)
     {
     }
     return(null);
 }
Example #23
0
 /// <summary>Read a single byte from the stream.</summary>
 /// <exception cref="System.IO.IOException"/>
 public override int Read()
 {
     if (pos < buffer.Length)
     {
         return(buffer[pos++]);
     }
     if (!fileReader.HasNext())
     {
         return(-1);
     }
     writer.Write(fileReader.Next(), encoder);
     encoder.Flush();
     if (!fileReader.HasNext())
     {
         // Write a new line after the last Avro record.
         output.Write(Runtime.GetBytesForString(Runtime.GetProperty("line.separator"
                                                                    ), Charsets.Utf8));
         output.Flush();
     }
     pos    = 0;
     buffer = output.ToByteArray();
     output.Reset();
     return(Read());
 }
Example #24
0
 private void DownloadKmlFile(string mUrl)
 {
     try
     {
         Stream @is = new URL(mUrl).OpenStream();
         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
         int    nRead;
         byte[] data = new byte[16384];
         while ((nRead = @is.Read(data, 0, data.Length)) != -1)
         {
             buffer.Write(data, 0, nRead);
         }
         buffer.Flush();
         try
         {
             using (var stream = new MemoryStream(buffer.ToByteArray()))
             {
                 KmlLayer kmlLayer = new KmlLayer(mMap, stream, this);//,
                 //new MarkerManager(mMap),
                 //new PolygonManager(mMap),
                 //new PolylineManager(mMap),
                 //new GroundOverlayManager(mMap),
                 //getImagesCache());
                 addKmlToMap(kmlLayer);
             }
         }
         catch (XmlPullParserException e)
         {
             e.PrintStackTrace();
         }
     }
     catch (Java.IO.IOException e)
     {
         e.PrintStackTrace();
     }
 }
Example #25
0
        public void TestCreateNewProperties()
        {
            POIDocument doc = new HSSFWorkbook();

            // New document won't have them
            Assert.IsNull(doc.SummaryInformation);
            Assert.IsNull(doc.DocumentSummaryInformation);

            // Add them in
            doc.CreateInformationProperties();
            Assert.IsNotNull(doc.SummaryInformation);
            Assert.IsNotNull(doc.DocumentSummaryInformation);

            // Write out and back in again, no change
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            doc.Write(baos);
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.ToByteArray());

            doc = new HSSFWorkbook(bais);

            Assert.IsNotNull(doc.SummaryInformation);
            Assert.IsNotNull(doc.DocumentSummaryInformation);
        }
Example #26
0
        /// <summary>Tests compatibility with pre-0.21 versions of TupleWritable</summary>
        /// <exception cref="System.Exception"/>
        public virtual void TestPreVersion21Compatibility()
        {
            Writable[] manyWrits = MakeRandomWritables(64);
            TestTupleWritable.PreVersion21TupleWritable oldTuple = new TestTupleWritable.PreVersion21TupleWritable
                                                                       (manyWrits);
            for (int i = 0; i < manyWrits.Length; i++)
            {
                if (i % 3 == 0)
                {
                    oldTuple.SetWritten(i);
                }
            }
            ByteArrayOutputStream @out = new ByteArrayOutputStream();

            oldTuple.Write(new DataOutputStream(@out));
            ByteArrayInputStream @in    = new ByteArrayInputStream(@out.ToByteArray());
            TupleWritable        dTuple = new TupleWritable();

            dTuple.ReadFields(new DataInputStream(@in));
            NUnit.Framework.Assert.IsTrue("Tuple writable is unable to read pre-0.21 versions of TupleWritable"
                                          , oldTuple.IsCompatible(dTuple));
            NUnit.Framework.Assert.AreEqual("All tuple data has not been read from the stream"
                                            , -1, @in.Read());
        }
        public byte[] imageToByteArray(Java.Net.URI imageIn, byte[] bytes)
        {
            Java.IO.File    file = new Java.IO.File(imageIn);
            FileInputStream fis  = new FileInputStream(file);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            byte[] buf = new byte[1024];

            try
            {
                for (int readNum; (readNum = fis.Read(buf)) != -1;)
                {
                    bos.Write(buf, 0, readNum);
                }
            }
            catch (Exception)
            {
                throw new Exception();
            }

            bytes = bos.ToByteArray();
            return(bytes);
        }
 /// <exception cref="System.IO.IOException"></exception>
 internal byte[] ToArray()
 {
     try
     {
         if (length >= 0)
         {
             byte[] r = new byte[(int)length];
             IOUtil.ReadFully(@in, r, 0, r.Length);
             return(r);
         }
         ByteArrayOutputStream r_1 = new ByteArrayOutputStream();
         byte[] buf = new byte[2048];
         int    n;
         while ((n = @in.Read(buf)) >= 0)
         {
             r_1.Write(buf, 0, n);
         }
         return(r_1.ToByteArray());
     }
     finally
     {
         @in.Close();
     }
 }
        public virtual void TestReadWriteV3()
        {
            FilePath file = PathOf("gitgit.index.v3");
            DirCache dc   = new DirCache(file, FS.DETECTED);

            dc.Read();
            NUnit.Framework.Assert.AreEqual(10, dc.GetEntryCount());
            AssertV3TreeEntry(0, "dir1/file1.txt", false, false, dc);
            AssertV3TreeEntry(1, "dir2/file2.txt", true, false, dc);
            AssertV3TreeEntry(2, "dir3/file3.txt", false, false, dc);
            AssertV3TreeEntry(3, "dir3/file3a.txt", true, false, dc);
            AssertV3TreeEntry(4, "dir4/file4.txt", true, false, dc);
            AssertV3TreeEntry(5, "dir4/file4a.txt", false, false, dc);
            AssertV3TreeEntry(6, "file.txt", true, false, dc);
            AssertV3TreeEntry(7, "newdir1/newfile1.txt", false, true, dc);
            AssertV3TreeEntry(8, "newdir1/newfile2.txt", false, true, dc);
            AssertV3TreeEntry(9, "newfile.txt", false, true, dc);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            dc.WriteTo(bos);
            byte[] indexBytes    = bos.ToByteArray();
            byte[] expectedBytes = IOUtil.ReadFully(file);
            NUnit.Framework.Assert.IsTrue(Arrays.Equals(expectedBytes, indexBytes));
        }
Example #30
0
        /*
         * makes array of bytes with History events
         */
        /// <exception cref="System.Exception"/>
        private byte[] GetEvents()
        {
            ByteArrayOutputStream output   = new ByteArrayOutputStream();
            FSDataOutputStream    fsOutput = new FSDataOutputStream(output, new FileSystem.Statistics
                                                                        ("scheme"));
            EventWriter writer = new EventWriter(fsOutput);

            writer.Write(GetJobPriorityChangedEvent());
            writer.Write(GetJobStatusChangedEvent());
            writer.Write(GetTaskUpdatedEvent());
            writer.Write(GetReduceAttemptKilledEvent());
            writer.Write(GetJobKilledEvent());
            writer.Write(GetSetupAttemptStartedEvent());
            writer.Write(GetTaskAttemptFinishedEvent());
            writer.Write(GetSetupAttemptFieledEvent());
            writer.Write(GetSetupAttemptKilledEvent());
            writer.Write(GetCleanupAttemptStartedEvent());
            writer.Write(GetCleanupAttemptFinishedEvent());
            writer.Write(GetCleanupAttemptFiledEvent());
            writer.Write(GetCleanupAttemptKilledEvent());
            writer.Flush();
            writer.Close();
            return(output.ToByteArray());
        }
Example #31
0
        public void ZipBombCreateAndHandle()
        {
            // #50090 / #56865
            ZipFile zipFile = ZipHelper.OpenZipFile(OpenXml4NetTestDataSamples.GetSampleFile("sample.xlsx"));

            Assert.IsNotNull(zipFile);

            ByteArrayOutputStream bos    = new ByteArrayOutputStream(2500000);
            ZipOutputStream       append = new ZipOutputStream(bos);
            // first, copy contents from existing war
            IEnumerator entries = zipFile.GetEnumerator();

            while (entries.MoveNext())
            {
                ZipEntry e2 = (ZipEntry)entries.Current;
                ZipEntry e  = new ZipEntry(e2.Name);

                e.DateTime = (e2.DateTime);
                e.Comment  = (e2.Comment);
                e.Size     = (e2.Size);

                append.PutNextEntry(e);
                if (!e.IsDirectory)
                {
                    Stream is1 = zipFile.GetInputStream(e);
                    if (e.Name.Equals("[Content_Types].xml"))
                    {
                        ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
                        IOUtils.Copy(is1, bos2);
                        long size = bos2.Length - "</Types>".Length;
                        append.Write(bos2.ToByteArray(), 0, (int)size);
                        byte[] spam = new byte[0x7FFF];
                        for (int i = 0; i < spam.Length; i++)
                        {
                            spam[i] = (byte)' ';
                        }
                        // 0x7FFF0000 is the maximum for 32-bit zips, but less still works
                        while (size < 0x7FFF0000)
                        {
                            append.Write(spam, 0, spam.Length);
                            size += spam.Length;
                        }
                        append.Write(Encoding.ASCII.GetBytes("</Types>"), 0, 8);
                        size  += 8;
                        e.Size = (size);
                    }
                    else
                    {
                        IOUtils.Copy(is1, append);
                    }
                }
                append.CloseEntry();
            }

            append.Close();
            zipFile.Close();

            byte[] buf = bos.ToByteArray();
            bos = null;

            IWorkbook wb = WorkbookFactory.Create(new ByteArrayInputStream(buf));

            wb.GetSheetAt(0);
            wb.Close();
            zipFile.Close();
        }
Example #32
0
 public byte[] ToByteArray()
 {
     return(stream.ToByteArray());
 }
Example #33
0
		public virtual void Write(float value) {

			if (disposed)
				throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");

            var byteArrayOutputStream = new ByteArrayOutputStream();
            var dataOutputStream = new DataOutputStream(byteArrayOutputStream);
            dataOutputStream.WriteFloat(value);
            byteArrayOutputStream.Flush();

		    WriteSwapped(byteArrayOutputStream.ToByteArray(), 4);

            byteArrayOutputStream.Close();
            dataOutputStream.Close();
		}
Example #34
0
        static void GetPhoto(Context ctx, ref AndroidContact destination, Android.Net.Uri photoUri)
        {
            ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
            const int bufSize = 0x100;
            byte[] buf = new byte[bufSize];
            int n;
            using (Stream fileStream = ctx.ContentResolver.OpenInputStream(photoUri))
            {
                while ((n = fileStream.Read(buf, 0, bufSize)) > 0)
                    byteBuffer.Write(buf, 0, n);
            }

            destination.Photo = byteBuffer.ToByteArray();
        }
		/**
     	* Write a file to internal storage.  Used to set up our dummy "cloud server".
     	*
     	* @param resId     the resource ID of the file to write to internal storage
     	* @param extension the file extension (ex. .png, .mp3)
     	*/
		void WriteFileToInternalStorage (int resId, String extension)
		{
			using (System.IO.Stream ins = Context.Resources.OpenRawResource (resId)) {
				var outputStream = new ByteArrayOutputStream ();
				int size;
				byte[] buffer = new byte [1024];
				while ((size = ins.Read (buffer, 0, 1024)) > 0) {
					outputStream.Write (buffer, 0, size);
				}
				buffer = outputStream.ToByteArray ();
				var filename = Context.Resources.GetResourceEntryName (resId) + extension;
				using (System.IO.Stream fos = Context.OpenFileOutput (filename, FileCreationMode.Private)) {
					fos.Write (buffer, 0, buffer.Length);
				}
			}
		}
Example #36
0
        /**
         * Reads GIF image from stream
         *
         * @param is containing GIF file.
         * @return read status code (0 = no errors)
         */
        public int read(InputStream inputStream, int contentLength)
        {
            long startTime = DateTime.Now.Millisecond;
            if (inputStream != null)
            {
                try
                {
                    int capacity = (contentLength > 0) ? (contentLength + 4096) : 4096;
                    ByteArrayOutputStream buffer = new ByteArrayOutputStream(capacity);
                    int nRead;
                    byte[] data = new byte[16384];
                    while ((nRead = inputStream.Read(data, 0, data.Length)) != -1)
                    {
                        buffer.Write(data, 0, nRead);
                    }
                    buffer.Flush();

                    read(buffer.ToByteArray());
                }
                catch (IOException e)
                {
                }
            }
            else {
                status = STATUS_OPEN_ERROR;
            }

            try
            {
                inputStream.Close();
            }
            catch (Exception e)
            {
            }

            return status;
        }