private void writeFully(ByteBuffer buffer) throws IOException, EOFException {
        while (buffer.hasRemaining()) {
            checkForRead();
            int written = channel.write(buffer);
            if (written == 0) {
                blockForWrite();
            } else if (written < 0) {
                throw new EOFException();
            }
        }
    }