コード例 #1
0
            private static void Write(InputStream inputStream, OutputStream os, bool close = true)
            {
                var buffer = new byte[DefaultBufferSize];

                try
                {
                    while (inputStream.Available() > 0)
                    {
                        var n = inputStream.Read(buffer);
                        if (n > 0)
                        {
                            os.Write(buffer, 0, n);
                        }
                    }

                    if (!close)
                    {
                        return;
                    }

                    inputStream.Close();
                    os.Close();
                }
                catch (IOException e)
                {
                    throw new UncheckedIOException(e);
                }
            }
コード例 #2
0
        /// <exception cref="System.IO.IOException"></exception>
        public void Verify(string host, SSLSocket ssl)
        {
            if (host == null)
            {
                throw new ArgumentNullException("host to verify is null");
            }
            SSLSession session = ssl.GetSession();

            if (session == null)
            {
                // In our experience this only happens under IBM 1.4.x when
                // spurious (unrelated) certificates show up in the server'
                // chain.  Hopefully this will unearth the real problem:
                InputStream @in = ssl.GetInputStream();
                @in.Available();
                // If ssl.getInputStream().available() didn't cause an
                // exception, maybe at least now the session is available?
                session = ssl.GetSession();
                if (session == null)
                {
                    // If it's still null, probably a startHandshake() will
                    // unearth the real problem.
                    ssl.StartHandshake();
                    // Okay, if we still haven't managed to cause an exception,
                    // might as well go for the NPE.  Or maybe we're okay now?
                    session = ssl.GetSession();
                }
            }
            Certificate[]   certs = session.GetPeerCertificates();
            X509Certificate x509  = (X509Certificate)certs[0];

            Verify(host, x509);
        }
コード例 #3
0
 /// <summary>
 /// Create a compression input stream that reads
 /// the decompressed bytes from the given stream.
 /// </summary>
 /// <param name="in">The input stream to be compressed.</param>
 /// <exception cref="System.IO.IOException"/>
 protected internal CompressionInputStream(InputStream @in)
 {
     if (!(@in is Seekable) || !(@in is PositionedReadable))
     {
         this.maxAvailableData = @in.Available();
     }
     this.@in = @in;
 }
コード例 #4
0
        /// <exception cref="System.Exception"/>
        public virtual void TestAvailable()
        {
            OutputStream @out = GetOutputStream(defaultBufferSize);

            WriteData(@out);
            // Default buffer size
            InputStream @in = GetInputStream(defaultBufferSize);

            byte[] result = new byte[dataLen];
            int    n1     = ReadAll(@in, result, 0, dataLen / 3);

            Assert.Equal(@in.Available(), dataLen - n1);
            int n2 = ReadAll(@in, result, n1, dataLen - n1);

            Assert.Equal(@in.Available(), dataLen - n1 - n2);
            @in.Close();
        }
コード例 #5
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: @Override public int available() throws java.io.IOException
            public override int Available()
            {
                if (Closed)
                {
                    throw new IOException("Stream is closed");
                }
                return(@is.Available());                // TBD:
            }
コード例 #6
0
        /// <exception cref="System.IO.IOException"></exception>
        protected internal override void DoSkip()
        {
            int size = Encdec.Dec_uint16be(Sbuf, 2) & 0xFFFF;

            if (size < 33 || (4 + size) > RcvBufSize)
            {
                In.Skip(In.Available());
            }
            else
            {
                In.Skip(size - 32);
            }
        }
コード例 #7
0
ファイル: LuaFiles.cs プロジェクト: zenjan1/AutoLua
 public static byte[] ReadBytes(InputStream inputStream)
 {
     try
     {
         var bytes = new byte[inputStream.Available()];
         inputStream.Read(bytes);
         return(bytes);
     }
     catch (IOException e)
     {
         throw new UncheckedIOException(e);
     }
 }
コード例 #8
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public int read(java.nio.ByteBuffer dst) throws java.io.IOException
            public virtual int Read(ByteBuffer dst)
            {
                int len       = dst.Remaining();
                int totalRead = 0;
                int bytesRead = 0;

                lock (ReadLock)
                {
                    while (totalRead < len)
                    {
                        int bytesToRead = System.Math.Min((len - totalRead), TRANSFER_SIZE);
                        if (Buf.Length < bytesToRead)
                        {
                            Buf = new sbyte[bytesToRead];
                        }
                        if ((totalRead > 0) && !(@in.Available() > 0))
                        {
                            break;                             // block at most once
                        }
                        try
                        {
                            Begin();
                            bytesRead = @in.Read(Buf, 0, bytesToRead);
                        }
                        finally
                        {
                            End(bytesRead > 0);
                        }
                        if (bytesRead < 0)
                        {
                            break;
                        }
                        else
                        {
                            totalRead += bytesRead;
                        }
                        dst.Put(Buf, 0, bytesRead);
                    }
                    if ((bytesRead < 0) && (totalRead == 0))
                    {
                        return(-1);
                    }

                    return(totalRead);
                }
            }
コード例 #9
0
ファイル: LuaFiles.cs プロジェクト: zenjan1/AutoLua
 public static string Read(InputStream inputStream, string encoding)
 {
     try
     {
         var bytes = new byte[inputStream.Available()];
         inputStream.Read(bytes);
         return(new Java.Lang.String(bytes, encoding).ToString());
     }
     catch (IOException e)
     {
         throw new UncheckedIOException(e);
     }
     finally
     {
         CloseSilently(inputStream);
     }
 }
コード例 #10
0
        /// <exception cref="System.IO.IOException"></exception>
        public override int Available()
        {
            int a = 0;

            // not -1
            if (IsReadAllowed())
            {
                try
                {
                    a = wrappedStream.Available();
                }
                catch (IOException ex)
                {
                    // no checkEOF() here, available() can't trigger EOF
                    CheckAbort();
                    throw;
                }
            }
            return(a);
        }
コード例 #11
0
 /// <summary>
 /// load the music file from file path and music duration in millisecond
 /// </summary>
 /// <param name="stream">music file path</param>
 /// <param name="duration">music duration in millisecond</param>
 public void LoadFrom(InputStream stream, int duration)
 {
     try
     {
         MStream = stream;
         MTrackDurationInMilliSec = duration;
         try
         {
             MStreamLength = stream.Available();
         }
         catch (IOException e)
         {
             Methods.DisplayReportResultTrack(e);
         }
         IsNewLoad       = true;
         MSeekToPosition = -1;
         IsAutoProgress  = false;
         Invalidate();
     }
     catch (Exception e)
     {
         Methods.DisplayReportResultTrack(e);
     }
 }
コード例 #12
0
 /// <summary>
 /// load the music file from file path and music duration in millisecond
 /// </summary>
 /// <param name="stream">music file path</param>
 /// <param name="duration">music duration in millisecond</param>
 public void LoadFrom(InputStream stream, int duration)
 {
     try
     {
         MStream = stream;
         MTrackDurationInMilliSec = duration;
         try
         {
             MStreamLength = stream.Available();
         }
         catch (IOException e)
         {
             Console.WriteLine(e);
         }
         IsNewLoad       = true;
         MSeekToPosition = -1;
         IsAutoProgress  = false;
         Invalidate();
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
     }
 }
コード例 #13
0
        /// <summary>
        /// Reads bytes from this byte-input stream into the specified byte array,
        /// starting at the given offset.
        ///
        /// <para> This method implements the general contract of the corresponding
        /// <code><seealso cref="InputStream#read(byte[], int, int) read"/></code> method of
        /// the <code><seealso cref="InputStream"/></code> class.  As an additional
        /// convenience, it attempts to read as many bytes as possible by repeatedly
        /// invoking the <code>read</code> method of the underlying stream.  This
        /// iterated <code>read</code> continues until one of the following
        /// conditions becomes true: <ul>
        ///
        ///   <li> The specified number of bytes have been read,
        ///
        ///   <li> The <code>read</code> method of the underlying stream returns
        ///   <code>-1</code>, indicating end-of-file, or
        ///
        ///   <li> The <code>available</code> method of the underlying stream
        ///   returns zero, indicating that further input requests would block.
        ///
        /// </ul> If the first <code>read</code> on the underlying stream returns
        /// <code>-1</code> to indicate end-of-file then this method returns
        /// <code>-1</code>.  Otherwise this method returns the number of bytes
        /// actually read.
        ///
        /// </para>
        /// <para> Subclasses of this class are encouraged, but not required, to
        /// attempt to read as many bytes as possible in the same fashion.
        ///
        /// </para>
        /// </summary>
        /// <param name="b">     destination buffer. </param>
        /// <param name="off">   offset at which to start storing bytes. </param>
        /// <param name="len">   maximum number of bytes to read. </param>
        /// <returns>     the number of bytes read, or <code>-1</code> if the end of
        ///             the stream has been reached. </returns>
        /// <exception cref="IOException">  if this input stream has been closed by
        ///                          invoking its <seealso cref="#close()"/> method,
        ///                          or an I/O error occurs. </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public synchronized int read(byte b[] , int off, int len) throws IOException
        public override int Read(sbyte[] b, int off, int len)
        {
            lock (this)
            {
                BufIfOpen;                 // Check for closed stream
                if ((off | len | (off + len) | (b.Length - (off + len))) < 0)
                {
                    throw new IndexOutOfBoundsException();
                }
                else if (len == 0)
                {
                    return(0);
                }

                int n = 0;
                for (;;)
                {
                    int nread = Read1(b, off + n, len - n);
                    if (nread <= 0)
                    {
                        return((n == 0) ? nread : n);
                    }
                    n += nread;
                    if (n >= len)
                    {
                        return(n);
                    }
                    // if not closed but no bytes available, return
                    InputStream input = @in;
                    if (input != null && input.Available() <= 0)
                    {
                        return(n);
                    }
                }
            }
        }
コード例 #14
0
ファイル: LuaFiles.cs プロジェクト: zenjan1/AutoLua
 public static void Write(InputStream inputStream, OutputStream os, bool close)
 {
     byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
     try
     {
         while (inputStream.Available() > 0)
         {
             int n = inputStream.Read(buffer);
             if (n > 0)
             {
                 os.Write(buffer, 0, n);
             }
         }
         if (close)
         {
             inputStream.Close();
             os.Close();
         }
     }
     catch (IOException e)
     {
         throw new UncheckedIOException(e);
     }
 }
コード例 #15
0
 /// <exception cref="System.IO.IOException"></exception>
 public override int Available()
 {
     InitWrapper();
     return(wrapperStream.Available());
 }
コード例 #16
0
            /// <exception cref="System.IO.IOException"/>
            public override void Check(string[] host, SSLSocket ssl)
            {
                if (host == null)
                {
                    throw new ArgumentNullException("host to verify is null");
                }
                SSLSession session = ssl.GetSession();

                if (session == null)
                {
                    // In our experience this only happens under IBM 1.4.x when
                    // spurious (unrelated) certificates show up in the server'
                    // chain.  Hopefully this will unearth the real problem:
                    InputStream @in = ssl.GetInputStream();
                    @in.Available();

                    /*
                     * If you're looking at the 2 lines of code above because
                     * you're running into a problem, you probably have two
                     * options:
                     *
                     #1.  Clean up the certificate chain that your server
                     * is presenting (e.g. edit "/etc/apache2/server.crt"
                     * or wherever it is your server's certificate chain
                     * is defined).
                     *
                     * OR
                     *
                     #2.   Upgrade to an IBM 1.5.x or greater JVM, or switch
                     * to a non-IBM JVM.
                     */
                    // If ssl.getInputStream().available() didn't cause an
                    // exception, maybe at least now the session is available?
                    session = ssl.GetSession();
                    if (session == null)
                    {
                        // If it's still null, probably a startHandshake() will
                        // unearth the real problem.
                        ssl.StartHandshake();
                        // Okay, if we still haven't managed to cause an exception,
                        // might as well go for the NPE.  Or maybe we're okay now?
                        session = ssl.GetSession();
                    }
                }
                Certificate[] certs;
                try
                {
                    certs = session.GetPeerCertificates();
                }
                catch (SSLPeerUnverifiedException spue)
                {
                    InputStream @in = ssl.GetInputStream();
                    @in.Available();
                    // Didn't trigger anything interesting?  Okay, just throw
                    // original.
                    throw;
                }
                X509Certificate x509 = (X509Certificate)certs[0];

                Check(host, x509);
            }
コード例 #17
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public int available() throws IOException
            public override int Available()
            {
                return(@is.Available());
            }
コード例 #18
0
    public void TestBufferReaderWriter()
    {
        StreamingControlImpl <int> .BridgedStreams obj = new StreamingControlImpl <int> .BridgedStreams();

        InputStream <int>  input  = (InputStream <int>)obj;
        OutputStream <int> output = (OutputStream <int>)obj;

        output.Write(1);
        output.Write(2);
        output.Write(3);
        output.Write(4);
        output.Write(5);
        output.Write(6);
        Console.WriteLine("Value (" + output.ToString() + ")");
        int counter = 0;

        foreach (int value in input)
        {
            counter++;
        }
        NUnit.Framework.Assert.AreEqual(6, counter);
        output.Flush();
        Console.WriteLine("Value (" + output.ToString() + ")");
        counter = 0;
        foreach (int value in input)
        {
            counter++;
        }
        NUnit.Framework.Assert.AreEqual(0, counter);

        output.Write(1);
        output.Write(2);
        counter = 0;
        foreach (int value in input)
        {
            counter++;
        }
        Console.WriteLine("Value (" + output.ToString() + ")");
        NUnit.Framework.Assert.AreEqual(2, counter);

        input.Reset();
        NUnit.Framework.Assert.IsTrue(input.Available(), "First read content is available\n" + input.ToString());
        NUnit.Framework.Assert.AreEqual(1, input.Read(), "Read value is not one\n" + input.ToString());
        NUnit.Framework.Assert.IsTrue(input.Available(), "Second read content is available\n" + input.ToString());
        NUnit.Framework.Assert.AreEqual(2, input.Read(), "Read value is not second\n" + input.ToString());
        NUnit.Framework.Assert.IsFalse(input.Available(), "Third read and content is not available\n" + input.ToString());

        int[] buffer;
        input.Read(out buffer);
        NUnit.Framework.Assert.AreEqual(0, buffer.Length, "Length should nothing as all has been read");
        input.Reset();
        input.Read(out buffer);
        NUnit.Framework.Assert.AreEqual(2, buffer.Length, "Length should 2");

        input.Reset();
        input.Read();
        output.Write(new int[] { 5, 6 });
        Console.WriteLine("Value (" + output.ToString() + ")");
        NUnit.Framework.Assert.IsFalse(input.Available(), "Wrote content so should be at the end");

        input.Reset();
        input.Read(out buffer);
        NUnit.Framework.Assert.AreEqual(3, buffer.Length, "Length should three long");
        NUnit.Framework.Assert.AreEqual(1, buffer[0]);
        NUnit.Framework.Assert.AreEqual(5, buffer[1]);
        NUnit.Framework.Assert.AreEqual(6, buffer[2]);
    }