/** * Writes bytes to the socket. * * @param buf byte buffer containing the bytes * @param offset offset into the buffer * @param length number of bytes to read * @param isEnd if the write is at a close. * @exception throws ClientDisconnectException if the connection is dropped */ @Override public void write(byte[] buf, int offset, int length, boolean isEnd) throws IOException { if (_s == null) { return; } try { _needsFlush = true; while (length > 0) { _writeBuffer.clear(); int sublen = Math.min(length, _writeBuffer.remaining()); _writeBuffer.put(buf, offset, sublen); _writeBuffer.flip(); _s.write(_writeBuffer); length -= sublen; offset += sublen; } _totalWriteBytes += length; } catch (IOException e) { IOException exn = ClientDisconnectException.create(this + ":" + e, e); try { close(); } catch (IOException e1) { } throw exn; } }
/** Writes an nio buffer to the socket. */ @Override public void write(Buffer buffer, boolean isEnd) throws IOException { if (_s == null) { buffer.free(); return; } try { _needsFlush = true; if (buffer.isDirect()) { _totalWriteBytes += buffer.length(); _s.write(buffer.direct()); return; } _totalWriteBytes += buffer.length(); while (buffer.length() > 0) { _writeBuffer.clear(); buffer.read(_writeBuffer); _writeBuffer.flip(); _s.write(_writeBuffer); } } catch (IOException e) { IOException exn = ClientDisconnectException.create(this + ":" + e, e); try { close(); } catch (IOException e1) { } throw exn; } finally { buffer.free(); } }