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 }
// allocate more spaces to the given ByteBuffer private java.nio.ByteBuffer allocateMore(java.nio.ByteBuffer output) { if (output.capacity() == 0) { return(java.nio.ByteBuffer.allocate(1)); } java.nio.ByteBuffer result = java.nio.ByteBuffer.allocate(output.capacity() * 2); output.flip(); result.put(output); return(result); }
/// <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(); } } }
/// <summary>This is a facade method for the encoding operation.</summary> /// <remarks> /// This is a facade method for the encoding operation. /// <p> /// This method encodes the remaining character sequence of the given /// character buffer into a new byte buffer. This method performs a complete /// encoding operation, resets at first, then encodes, and flushes at last. /// <p> /// This method should not be invoked if another encode operation is ongoing. /// </remarks> /// <param name="in">the input buffer.</param> /// <returns> /// a new <code>ByteBuffer</code> containing the bytes produced by /// this encoding operation. The buffer's limit will be the position /// of the last byte in the buffer, and the position will be zero. /// </returns> /// <exception cref="System.InvalidOperationException">if another encoding operation is ongoing. /// </exception> /// <exception cref="MalformedInputException"> /// if an illegal input character sequence for this charset is /// encountered, and the action for malformed error is /// <see cref="CodingErrorAction.REPORT">CodingErrorAction.REPORT</see> /// </exception> /// <exception cref="UnmappableCharacterException"> /// if a legal but unmappable input character sequence for this /// charset is encountered, and the action for unmappable /// character error is /// <see cref="CodingErrorAction.REPORT">CodingErrorAction.REPORT</see> /// . /// Unmappable means the Unicode character sequence at the input /// buffer's current position cannot be mapped to a equivalent /// byte sequence. /// </exception> /// <exception cref="CharacterCodingException">if other exception happened during the encode operation. /// </exception> /// <exception cref="java.nio.charset.CharacterCodingException"></exception> public java.nio.ByteBuffer encode(java.nio.CharBuffer @in) { if (@in.remaining() == 0) { return(java.nio.ByteBuffer.allocate(0)); } reset(); int length = (int)(@in.remaining() * _averageBytesPerChar); java.nio.ByteBuffer output = java.nio.ByteBuffer.allocate(length); java.nio.charset.CoderResult result = null; while (true) { result = encode(@in, output, false); if (result == java.nio.charset.CoderResult.UNDERFLOW) { break; } else { if (result == java.nio.charset.CoderResult.OVERFLOW) { output = allocateMore(output); continue; } } checkCoderResult(result); } result = encode(@in, output, true); checkCoderResult(result); while (true) { result = flush(output); if (result == java.nio.charset.CoderResult.UNDERFLOW) { output.flip(); break; } else { if (result == java.nio.charset.CoderResult.OVERFLOW) { output = allocateMore(output); continue; } } checkCoderResult(result); output.flip(); if (result.isMalformed()) { throw new java.nio.charset.MalformedInputException(result.length()); } else { if (result.isUnmappable()) { throw new java.nio.charset.UnmappableCharacterException(result.length()); } } break; } status = READY; finished = true; return(output); }