示例#1
0
        public static long write(object nd, FileDescriptor fd, ByteBuffer[] bufs, int offset, int length)
        {
#if FIRST_PASS
            return(0);
#else
            ByteBuffer[] altBufs             = null;
            List <ArraySegment <byte> > list = new List <ArraySegment <byte> >(length);
            for (int i = 0; i < length; i++)
            {
                ByteBuffer bb = bufs[i + offset];
                if (!bb.hasArray())
                {
                    if (altBufs == null)
                    {
                        altBufs = new ByteBuffer[bufs.Length];
                    }
                    ByteBuffer abb = ByteBuffer.allocate(bb.remaining());
                    int        pos = bb.position();
                    abb.put(bb);
                    bb.position(pos);
                    abb.flip();
                    bb = altBufs[i + offset] = abb;
                }
                list.Add(new ArraySegment <byte>(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining()));
            }
            int count;
            try
            {
                count = fd.getSocket().Send(list);
            }
            catch (System.Net.Sockets.SocketException x)
            {
                if (x.ErrorCode == global::java.net.SocketUtil.WSAEWOULDBLOCK)
                {
                    count = 0;
                }
                else
                {
                    throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
                }
            }
            catch (ObjectDisposedException)
            {
                throw new global::java.net.SocketException("Socket is closed");
            }
            int total = count;
            for (int i = 0; total > 0 && i < length; i++)
            {
                ByteBuffer bb       = bufs[i + offset];
                int        consumed = Math.Min(total, bb.remaining());
                bb.position(bb.position() + consumed);
                total -= consumed;
            }
            return(count);
#endif
        }
示例#2
0
 /// <exception cref="System.IO.IOException"></exception>
 private void flushBytes(bool flushUnderlyingStream)
 {
     lock (@lock)
     {
         checkStatus();
         int position = bytes.position();
         if (position > 0)
         {
             bytes.flip();
             @out.write(((byte[])bytes.array()), bytes.arrayOffset(), position);
             bytes.clear();
         }
         if (flushUnderlyingStream)
         {
             @out.flush();
         }
     }
 }
示例#3
0
        public static void BlockCopy(ByteBuffer src, int srcPos, ByteBuffer dst, int dstPos, int length)
        {
            if (src.hasArray() && dst.hasArray())
            {
                java.lang.System.arraycopy(src.array(),
                                           src.arrayOffset() + srcPos,
                                           dst.array(),
                                           dst.arrayOffset() + dstPos,
                                           length);
            }
            else
            {
                if (src.limit() - srcPos < length || dst.limit() - dstPos < length)
                {
                    throw new java.lang.IndexOutOfBoundsException();
                }

                for (int i = 0; i < length; i++)
                {
                    // TODO: ByteBuffer.put is polymorphic, and might be slow here
                    dst.put(dstPos++, src.get(srcPos++));
                }
            }
        }
示例#4
0
 public override int read(char[] buffer, int offset, int length)
 {
     lock (@lock)
     {
         if (!isOpen())
         {
             throw new System.IO.IOException("InputStreamReader is closed");
         }
         java.util.Arrays.checkOffsetAndCount(buffer.Length, offset, length);
         if (length == 0)
         {
             return(0);
         }
         java.nio.CharBuffer          @out   = java.nio.CharBuffer.wrap(buffer, offset, length);
         java.nio.charset.CoderResult result = java.nio.charset.CoderResult.UNDERFLOW;
         // bytes.remaining() indicates number of bytes in buffer
         // when 1-st time entered, it'll be equal to zero
         bool needInput = !bytes.hasRemaining();
         while (@out.hasRemaining())
         {
             // fill the buffer if needed
             if (needInput)
             {
                 try
                 {
                     if (@in.available() == 0 && @out.position() > offset)
                     {
                         // we could return the result without blocking read
                         break;
                     }
                 }
                 catch (System.IO.IOException)
                 {
                 }
                 // available didn't work so just try the read
                 int desiredByteCount = bytes.capacity() - bytes.limit();
                 int off             = bytes.arrayOffset() + bytes.limit();
                 int actualByteCount = @in.read(((byte[])bytes.array()), off, desiredByteCount);
                 if (actualByteCount == -1)
                 {
                     endOfInput = true;
                     break;
                 }
                 else
                 {
                     if (actualByteCount == 0)
                     {
                         break;
                     }
                 }
                 bytes.limit(bytes.limit() + actualByteCount);
                 needInput = false;
             }
             // decode bytes
             result = decoder.decode(bytes, @out, false);
             if (result.isUnderflow())
             {
                 // compact the buffer if no space left
                 if (bytes.limit() == bytes.capacity())
                 {
                     bytes.compact();
                     bytes.limit(bytes.position());
                     bytes.position(0);
                 }
                 needInput = true;
             }
             else
             {
                 break;
             }
         }
         if (result == java.nio.charset.CoderResult.UNDERFLOW && endOfInput)
         {
             result = decoder.decode(bytes, @out, true);
             decoder.flush(@out);
             decoder.reset();
         }
         if (result.isMalformed() || result.isUnmappable())
         {
             result.throwException();
         }
         return(@out.position() - offset == 0 ? -1 : @out.position() - offset);
     }
 }