Example #1
0
 /// <summary>Write a ByteBuffer to a WritableByteChannel, handling short writes.</summary>
 /// <param name="bc">The WritableByteChannel to write to</param>
 /// <param name="buf">The input buffer</param>
 /// <exception cref="System.IO.IOException">On I/O error</exception>
 public static void WriteFully(WritableByteChannel bc, ByteBuffer buf)
 {
     do
     {
         bc.Write(buf);
     }while (buf.Remaining() > 0);
 }
Example #2
0
        /// <summary>
        /// Write all remaining bytes in buffer to the given channel.
        /// If the channel is selectable then it must be configured blocking.
        /// </summary>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private static void writeFullyImpl(WritableByteChannel ch, java.nio.ByteBuffer bb) throws java.io.IOException
        private static void WriteFullyImpl(WritableByteChannel ch, ByteBuffer bb)
        {
            while (bb.Remaining() > 0)
            {
                int n = ch.Write(bb);
                if (n <= 0)
                {
                    throw new RuntimeException("no bytes written");
                }
            }
        }
        internal virtual long CustomShuffleTransfer(WritableByteChannel target, long position
                                                    )
        {
            long actualCount = this.count - position;

            if (actualCount < 0 || position < 0)
            {
                throw new ArgumentException("position out of range: " + position + " (expected: 0 - "
                                            + (this.count - 1) + ')');
            }
            if (actualCount == 0)
            {
                return(0L);
            }
            long       trans = actualCount;
            int        readSize;
            ByteBuffer byteBuffer = ByteBuffer.Allocate(this.shuffleBufferSize);

            while (trans > 0L && (readSize = fileChannel.Read(byteBuffer, this.position + position
                                                              )) > 0)
            {
                //adjust counters and buffer limit
                if (readSize < trans)
                {
                    trans    -= readSize;
                    position += readSize;
                    byteBuffer.Flip();
                }
                else
                {
                    //We can read more than we need if the actualCount is not multiple
                    //of the byteBuffer size and file is big enough. In that case we cannot
                    //use flip method but we need to set buffer limit manually to trans.
                    byteBuffer.Limit((int)trans);
                    byteBuffer.Position(0);
                    position += trans;
                    trans     = 0;
                }
                //write data to the target
                while (byteBuffer.HasRemaining())
                {
                    target.Write(byteBuffer);
                }
                byteBuffer.Clear();
            }
            return(actualCount - trans);
        }
Example #4
0
 /// <exception cref="System.IO.IOException"/>
 internal override int PerformIO(ByteBuffer buf)
 {
     return(channel.Write(buf));
 }