Example usage for java.nio ByteBuffer slice

List of usage examples for java.nio ByteBuffer slice

Introduction

In this page you can find the example usage for java.nio ByteBuffer slice.

Prototype

public abstract ByteBuffer slice();

Source Link

Document

Returns a sliced buffer that shares its content with this buffer.

Usage

From source file:org.openteufel.file.mpq.MPQFileSector.java

protected MPQFileSector(boolean compressed, boolean imploded, int sizeUncompressed, ByteBuffer src,
        Long encryptionSeed) throws DataFormatException, IOException {
    this.encryptionSeed = encryptionSeed;
    this.sizeUncompressed = sizeUncompressed;

    if (sizeUncompressed == src.remaining()) {
        compression = Compression.Uncompressed;
    } else if (imploded) {
        compression = Compression.Imploded;
    } else if (compressed) {
        byte compressionByte = src.get();
        if (compressionByte == COMPRESSION_ZLIB) {
            compression = Compression.ZLib;
        } else if (compressionByte == COMPRESSION_BZIP2) {
            compression = Compression.BZip2;
        } else {/*from   w w w .  j a v  a 2s. co  m*/
            // Unkown compression
            compression = Compression.Uncompressed;
            /*
             * Count the compression byte to the data. Sectors are not
             * necessarily compressed, if the according flag is set. If the
             * compression byte is unknown, compressed sector are treated as
             * uncompressed.
             */
            src.position(src.position() - 1);
        }
    } else {
        compression = Compression.Uncompressed;
    }
    dataRaw = src.slice();
    dataRaw.order(src.order());
}

From source file:org.mozilla.gecko.gfx.GeckoSoftwareLayerClient.java

public void copyPixelsFromMultiTileLayer(Bitmap target) {
    Canvas c = new Canvas(target);
    ByteBuffer tileBuffer = mBuffer.slice();
    int bpp = CairoUtils.bitsPerPixelForCairoFormat(mFormat) / 8;

    for (int y = 0; y < mBufferSize.height; y += TILE_SIZE.height) {
        for (int x = 0; x < mBufferSize.width; x += TILE_SIZE.width) {
            // Calculate tile size
            IntSize tileSize = new IntSize(Math.min(mBufferSize.width - x, TILE_SIZE.width),
                    Math.min(mBufferSize.height - y, TILE_SIZE.height));

            // Create a Bitmap from this tile
            Bitmap tile = Bitmap.createBitmap(tileSize.width, tileSize.height,
                    CairoUtils.cairoFormatTobitmapConfig(mFormat));
            tile.copyPixelsFromBuffer(tileBuffer.asIntBuffer());

            // Copy the tile to the master Bitmap and recycle it
            c.drawBitmap(tile, x, y, null);
            tile.recycle();/*from  ww w  . j a  v a 2  s  .com*/

            // Progress the buffer to the next tile
            tileBuffer.position(tileSize.getArea() * bpp);
            tileBuffer = tileBuffer.slice();
        }
    }
}

From source file:org.apache.qpid.server.store.derby.DerbyMessageStore.java

private void storeMetaData(Connection conn, long messageId, StorableMessageMetaData metaData)
        throws SQLException {
    if (_logger.isDebugEnabled()) {
        _logger.debug("Adding metadata for message " + messageId);
    }/*from  w w w.ja  v  a  2s . c o  m*/

    PreparedStatement stmt = conn.prepareStatement(INSERT_INTO_META_DATA);
    try {
        stmt.setLong(1, messageId);

        final int bodySize = 1 + metaData.getStorableSize();
        byte[] underlying = new byte[bodySize];
        underlying[0] = (byte) metaData.getType().ordinal();
        java.nio.ByteBuffer buf = java.nio.ByteBuffer.wrap(underlying);
        buf.position(1);
        buf = buf.slice();

        metaData.writeToBuffer(0, buf);
        ByteArrayInputStream bis = new ByteArrayInputStream(underlying);
        try {
            stmt.setBinaryStream(2, bis, underlying.length);
            int result = stmt.executeUpdate();

            if (result == 0) {
                throw new RuntimeException("Unable to add meta data for message " + messageId);
            }
        } finally {
            try {
                bis.close();
            } catch (IOException e) {

                throw new SQLException(e);
            }
        }

    } finally {
        stmt.close();
    }

}

From source file:org.apache.qpid.server.store.derby.DerbyMessageStore.java

StorableMessageMetaData getMetaData(long messageId) throws SQLException {

    Connection conn = newAutoCommitConnection();
    try {/* w  w w.  j ava  2s.  co m*/
        PreparedStatement stmt = conn.prepareStatement(SELECT_FROM_META_DATA);
        try {
            stmt.setLong(1, messageId);
            ResultSet rs = stmt.executeQuery();
            try {

                if (rs.next()) {
                    Blob dataAsBlob = rs.getBlob(1);

                    byte[] dataAsBytes = dataAsBlob.getBytes(1, (int) dataAsBlob.length());
                    java.nio.ByteBuffer buf = java.nio.ByteBuffer.wrap(dataAsBytes);
                    buf.position(1);
                    buf = buf.slice();
                    MessageMetaDataType type = MessageMetaDataType.values()[dataAsBytes[0]];
                    StorableMessageMetaData metaData = type.getFactory().createMetaData(buf);

                    return metaData;
                } else {
                    throw new RuntimeException("Meta data not found for message with id " + messageId);
                }
            } finally {
                rs.close();
            }
        } finally {
            stmt.close();
        }
    } finally {
        conn.close();
    }
}

From source file:org.apache.qpid.server.store.derby.DerbyMessageStore.java

private void recoverMessages(MessageStoreRecoveryHandler recoveryHandler) throws SQLException {
    Connection conn = newAutoCommitConnection();
    try {/*from ww  w.  ja va  2s.c om*/
        MessageStoreRecoveryHandler.StoredMessageRecoveryHandler messageHandler = recoveryHandler.begin();

        Statement stmt = conn.createStatement();
        try {
            ResultSet rs = stmt.executeQuery(SELECT_ALL_FROM_META_DATA);
            try {

                long maxId = 0;

                while (rs.next()) {

                    long messageId = rs.getLong(1);
                    Blob dataAsBlob = rs.getBlob(2);

                    if (messageId > maxId) {
                        maxId = messageId;
                    }

                    byte[] dataAsBytes = dataAsBlob.getBytes(1, (int) dataAsBlob.length());
                    java.nio.ByteBuffer buf = java.nio.ByteBuffer.wrap(dataAsBytes);
                    buf.position(1);
                    buf = buf.slice();
                    MessageMetaDataType type = MessageMetaDataType.values()[dataAsBytes[0]];
                    StorableMessageMetaData metaData = type.getFactory().createMetaData(buf);
                    StoredDerbyMessage message = new StoredDerbyMessage(messageId, metaData, true);
                    messageHandler.message(message);
                }

                _messageId.set(maxId);

                messageHandler.completeMessageRecovery();
            } finally {
                rs.close();
            }
        } finally {
            stmt.close();
        }
    } finally {
        conn.close();
    }
}

From source file:org.apache.qpid.server.store.derby.DerbyMessageStore.java

private void addContent(Connection conn, long messageId, ByteBuffer src) {
    if (_logger.isDebugEnabled()) {
        _logger.debug("Adding content for message " + messageId);
    }//w w  w  . ja v  a 2s .  c o  m
    PreparedStatement stmt = null;

    try {
        src = src.slice();

        byte[] chunkData = new byte[src.limit()];
        src.duplicate().get(chunkData);

        stmt = conn.prepareStatement(INSERT_INTO_MESSAGE_CONTENT);
        stmt.setLong(1, messageId);

        ByteArrayInputStream bis = new ByteArrayInputStream(chunkData);
        stmt.setBinaryStream(2, bis, chunkData.length);
        stmt.executeUpdate();
    } catch (SQLException e) {
        closeConnection(conn);
        throw new RuntimeException("Error adding content for message " + messageId + ": " + e.getMessage(), e);
    } finally {
        closePreparedStatement(stmt);
    }

}

From source file:org.commoncrawl.io.internal.NIOHttpConnection.java

private void processChunkedContent() throws IOException {

    while (_inBuf.available() != 0 && _chunkState != ChunkState.STATE_DONE) {

        switch (_chunkState) {

        case STATE_AWAITING_CHUNK_HEADER: {

            _chunkCRLFReadState = _inBuf.readCRLFLine(_chunkLineBuffer, CHUNK_LINE_MAX, _chunkCRLFReadState);

            if (_chunkCRLFReadState == CRLFReadState.DONE) {
                // get the newly extracted line ... 
                String line = _chunkLineBuffer.toString();
                // now find first occurence of whitespace ... 
                int whiteSpaceIdx = line.indexOf(' ');
                if (whiteSpaceIdx != -1) {
                    line = line.substring(0, whiteSpaceIdx);
                }/*from w  ww  .j  ava2 s  .c  o m*/
                // now extract chunk length ... 
                try {
                    _chunkSize = Integer.parseInt(line, 16);
                } catch (NumberFormatException e) {
                    LOG.error("Invalid Chunk Size Encountered reading CHUNK HEADER:" + line);
                    throw new IOException("Invalid chunk size");
                }
                // reset chunk pos cursor ... 
                _chunkPos = 0;
                // reset chunk read state 
                _chunkCRLFReadState = CRLFReadState.NONE;
                // reset the buffer for the next potential line read ... 
                _chunkLineBuffer.setLength(0);

                // now interpret the chunk size value ... 
                if (_chunkSize > 0) {
                    _chunkState = ChunkState.STATE_READING_CHUNK;
                } else {
                    _chunkState = ChunkState.STATE_AWAITING_TRAILERS;
                }
            }
        }
            break;

        case STATE_READING_CHUNK: {

            // calculate amount we want to read in ... 
            int amountToRead = Math.min(_chunkSize - _chunkPos, _inBuf.available());
            // and track amount we wrote into chunk content buffer 
            int amountWritten = 0;

            while (amountToRead != 0) {

                // get a write buffer ... 
                ByteBuffer writeBuffer = _chunkContentBuffer.getWriteBuf();

                // get the next read buffer 
                ByteBuffer readBuffer = _inBuf.read();

                if (readBuffer == writeBuffer) {
                    throw new RuntimeException("BAD NEWS!!!");
                }

                //TODO: There is an opportunity here to skip buffer copy altogether and add read buffer directly to write buffer list 
                //            Need to look into this. 

                // if buffer size is > amountToRead ... 
                if (readBuffer.remaining() > writeBuffer.remaining() || readBuffer.remaining() > amountToRead) {

                    // slice the read buffer ... 
                    ByteBuffer sliced = readBuffer.slice();
                    // calculate slice amount 
                    int sliceAmount = Math.min(writeBuffer.remaining(), amountToRead);

                    // and increment original ... 
                    readBuffer.position(readBuffer.position() + sliceAmount);
                    // and limit sliced buffer scope ... 
                    sliced.limit(sliced.position() + sliceAmount);
                    // reduce amountToRead 
                    amountToRead -= sliceAmount;
                    // and increment chunk pos 
                    _chunkPos += sliceAmount;
                    // track amount written ... 
                    amountWritten += sliced.remaining();
                    // append it ... 
                    writeBuffer.put(sliced);
                    // and put back the read buffer 
                    _inBuf.putBack(readBuffer);
                }
                // otherwise... append whole buffer to write buffer 
                else {
                    // reduce amountToRead 
                    amountToRead -= readBuffer.remaining();
                    // and increment chunk pos 
                    _chunkPos += readBuffer.remaining();
                    // track amount written 
                    amountWritten += readBuffer.remaining();
                    // append as much as possible into the write buffer ... 
                    writeBuffer.put(readBuffer);
                }
            }

            // if we wrote some data to the content buffer ... 
            if (amountWritten != 0) {
                // update bytes downloaded ...
                _downloadedContentLength += amountWritten;

                if (getListener() != null) {
                    // inform listener of content availability 
                    getListener().HttpContentAvailable(this, _chunkContentBuffer);
                }
            }

            // now if we read in a chunks worth of data ... advance state ... 
            if (_chunkPos == _chunkSize) {
                _chunkState = ChunkState.STATE_AWAITING_CHUNK_EOL;
            }
        }
            break;

        case STATE_AWAITING_CHUNK_EOL: {

            if (_inBuf.available() >= 2) {
                ByteBuffer readBuffer = _inBuf.read();

                if (readBuffer.get() != '\r') {
                    LOG.error("Missing CR from Chunk Data Terminator");
                    throw new IOException("missing CR");
                }
                // now if read buffer is expended ... release it and get another one ... 
                if (readBuffer.remaining() == 0) {
                    readBuffer = _inBuf.read();
                }

                if (readBuffer.get() != '\n') {
                    LOG.error("Missing LFfrom Chunk Data Terminator");
                    throw new IOException("missing LF");
                }
                // put back the read buffer 
                _inBuf.putBack(readBuffer);
                // and transition to the next state ... 
                _chunkState = ChunkState.STATE_AWAITING_CHUNK_HEADER;
            } else {
                // break out and wait for more data 
                return;
            }
        }
            break;

        case STATE_AWAITING_TRAILERS: {

            _chunkCRLFReadState = _inBuf.readCRLFLine(_chunkLineBuffer, CHUNK_LINE_MAX, _chunkCRLFReadState);

            if (_chunkCRLFReadState == CRLFReadState.DONE) {
                // transition to a done state ... 
                _chunkState = ChunkState.STATE_DONE;
                // clear out intermediate crlf state
                _chunkCRLFReadState = CRLFReadState.NONE;
                _chunkLineBuffer.setLength(0);
            } else {
                break;
            }
        }
        // fall through if chunk state is done ... 

        case STATE_DONE: {
            // clear out existing input buffer ...
            _inBuf.reset();
            // flush chunk buffer ...
            _chunkContentBuffer.flush();
            // and swap it with the real content buffer ... 
            _inBuf = _chunkContentBuffer;
            // reset chunk state ... 
            _chunkContentBuffer = null;
            // reset chunked flag ... 
            _chunked = false;
            // set HTTP DONE state ... 
            setState(State.DONE, null);
        }
            break;
        }
    }
}

From source file:org.commoncrawl.io.internal.NIOHttpConnection.java

public void Writeable(NIOClientSocket theSocket) throws IOException {

    if (!theSocket.isOpen()) {
        return;/*w  ww.  j av a2 s . c o  m*/
    }

    int amountWritten = 0;

    try {

        boolean contentEOF = false;

        amountWritten = 0;

        if (_outBuf.available() == 0 && _dataSource != null) {
            // read some more data from the data source 
            contentEOF = _dataSource.read(_outBuf);
        }

        ByteBuffer bufferToWrite = _outBuf.read();

        if (bufferToWrite != null) {

            try {

                int amountToWrite = bufferToWrite.remaining();

                // if upload rate limiter is not null ... 
                if (_uploadRateLimiter != null) {
                    // apply rate limit policy to outbound data ... 
                    amountToWrite = _uploadRateLimiter.checkRateLimit(amountToWrite);
                }

                if (amountToWrite != 0) {
                    // if amount to write is less than remaining ... 
                    if (amountToWrite < bufferToWrite.remaining()) {
                        //slice the buffer ... 
                        ByteBuffer slicedBuffer = bufferToWrite.slice();
                        // limit to amount to write ... 
                        slicedBuffer.limit(amountToWrite);
                        // and write to socket ... 
                        amountWritten = _socket.write(slicedBuffer);
                        if (amountWritten >= 0) {
                            // advance source buffer manually...
                            bufferToWrite.position(bufferToWrite.position() + amountWritten);
                        }
                    } else {
                        amountWritten = _socket.write(bufferToWrite);
                    }

                    if (_uploadRateLimiter != null) {
                        _uploadRateLimiter.updateStats(amountWritten);

                        // debug output ... 
                        BandwidthUtils.BandwidthStats stats = new BandwidthUtils.BandwidthStats();
                        // collect stats 
                        _uploadRateLimiter.getStats(stats);
                        // dump stats ... 
                        // System.out.println("Connection: "+ this+"Upload Speed:" + stats.scaledBitsPerSecond + " " + stats.scaledBitsUnits + " TotalWritten:" + (_cumilativeWritten + amountWritten) );
                        // LOG.info("Connection:" + getId()+" BytesOut:" + amountWritten +" Upload Speed:" + stats.scaledBitsPerSecond + " " + stats.scaledBitsUnits + " TotalWritten:" + (_cumilativeWritten + amountWritten));
                    }
                }
            } catch (IOException exception) {
                // LOG.error(CCStringUtils.stringifyException(e));
                throw exception;
            }
            _totalWritten += amountWritten;
            _cumilativeWritten += amountWritten;

            // System.out.println("NIOHttpConnection->wrote:" + amountWritten + "Bytes TotalWritten:" + _cumilativeWritten);

            if (bufferToWrite.remaining() > 0) {
                _outBuf.putBack(bufferToWrite);
            }
        }

        if (_totalWritten > 0 && !_outBuf.isDataAvailable() && (_dataSource == null || contentEOF)) {

            _lastReadOrWriteTime = System.currentTimeMillis();

            // transition from sending to receiving ... 
            if (_state == State.SENDING_REQUEST) {
                // set up an initial last read time value here ... 
                setState(State.RECEIVING_HEADERS, null);
                _selector.registerForRead(theSocket);
            }
        }
    } catch (IOException e) {
        LOG.error("Writeable for url:" + getURL() + " threw Exception:" + e.getMessage());

        setErrorType(ErrorType.IOEXCEPTION);
        setErrorDesc(StringUtils.stringifyException(e));
        setState(State.ERROR, e);

        throw e;
    }

    if (_state == State.SENDING_REQUEST) {
        _selector.registerForReadAndWrite(theSocket);
    } else if (_state.ordinal() >= State.RECEIVING_HEADERS.ordinal()
            && _state.ordinal() < State.DONE.ordinal()) {
        _selector.registerForRead(theSocket);
    }
}

From source file:org.commoncrawl.io.NIOHttpConnection.java

private void processChunkedContent() throws IOException {

    while (_inBuf.available() != 0 && _chunkState != ChunkState.STATE_DONE) {

        switch (_chunkState) {

        case STATE_AWAITING_CHUNK_HEADER: {

            _chunkCRLFReadState = _inBuf.readCRLFLine(_chunkLineBuffer, CHUNK_LINE_MAX, _chunkCRLFReadState);

            if (_chunkCRLFReadState == CRLFReadState.DONE) {
                // get the newly extracted line ...
                String line = _chunkLineBuffer.toString();
                // now find first occurence of whitespace ...
                int whiteSpaceIdx = line.indexOf(' ');
                if (whiteSpaceIdx != -1) {
                    line = line.substring(0, whiteSpaceIdx);
                }//from w w w  .  j  a  v a  2  s.  c o  m
                // now extract chunk length ...
                try {
                    _chunkSize = Integer.parseInt(line, 16);
                } catch (NumberFormatException e) {
                    LOG.error("Connection:[" + getId()
                            + "] Invalid Chunk Size Encountered reading CHUNK HEADER:" + line);
                    throw new IOException("Invalid chunk size");
                }
                // reset chunk pos cursor ...
                _chunkPos = 0;
                // reset chunk read state
                _chunkCRLFReadState = CRLFReadState.NONE;
                // reset the buffer for the next potential line read ...
                _chunkLineBuffer.setLength(0);

                // now interpret the chunk size value ...
                if (_chunkSize > 0) {
                    _chunkState = ChunkState.STATE_READING_CHUNK;
                } else {
                    _chunkState = ChunkState.STATE_AWAITING_TRAILERS;
                }
            }
        }
            break;

        case STATE_READING_CHUNK: {

            // calculate amount we want to read in ...
            int amountToRead = Math.min(_chunkSize - _chunkPos, _inBuf.available());
            // and track amount we wrote into chunk content buffer
            int amountWritten = 0;

            while (amountToRead != 0) {

                // get a write buffer ...
                ByteBuffer writeBuffer = _chunkContentBuffer.getWriteBuf();

                // get the next read buffer
                ByteBuffer readBuffer = _inBuf.read();

                if (readBuffer == writeBuffer) {
                    throw new RuntimeException("BAD NEWS!!!");
                }

                // TODO: There is an opportunity here to skip buffer copy altogether
                // and add read buffer directly to write buffer list
                // Need to look into this.

                // if buffer size is > amountToRead ...
                if (readBuffer.remaining() > writeBuffer.remaining() || readBuffer.remaining() > amountToRead) {

                    // slice the read buffer ...
                    ByteBuffer sliced = readBuffer.slice();
                    // calculate slice amount
                    int sliceAmount = Math.min(writeBuffer.remaining(), amountToRead);

                    // and increment original ...
                    readBuffer.position(readBuffer.position() + sliceAmount);
                    // and limit sliced buffer scope ...
                    sliced.limit(sliced.position() + sliceAmount);
                    // reduce amountToRead
                    amountToRead -= sliceAmount;
                    // and increment chunk pos
                    _chunkPos += sliceAmount;
                    // track amount written ...
                    amountWritten += sliced.remaining();
                    // append it ...
                    writeBuffer.put(sliced);
                    // and put back the read buffer
                    _inBuf.putBack(readBuffer);
                }
                // otherwise... append whole buffer to write buffer
                else {
                    // reduce amountToRead
                    amountToRead -= readBuffer.remaining();
                    // and increment chunk pos
                    _chunkPos += readBuffer.remaining();
                    // track amount written
                    amountWritten += readBuffer.remaining();
                    // append as much as possible into the write buffer ...
                    writeBuffer.put(readBuffer);
                }
            }

            // if we wrote some data to the content buffer ...
            if (amountWritten != 0) {
                // update bytes downloaded ...
                _downloadedContentLength += amountWritten;

                if (getListener() != null) {
                    // inform listener of content availability
                    getListener().HttpContentAvailable(this, _chunkContentBuffer);
                }
            }

            // now if we read in a chunks worth of data ... advance state ...
            if (_chunkPos == _chunkSize) {
                _chunkState = ChunkState.STATE_AWAITING_CHUNK_EOL;
            }
        }
            break;

        case STATE_AWAITING_CHUNK_EOL: {

            if (_inBuf.available() >= 2) {
                ByteBuffer readBuffer = _inBuf.read();

                if (readBuffer.get() != '\r') {
                    LOG.error("Connection:[" + getId() + "] Missing CR from Chunk Data Terminator");
                    throw new IOException("missing CR");
                }
                // now if read buffer is expended ... release it and get another one
                // ...
                if (readBuffer.remaining() == 0) {
                    readBuffer = _inBuf.read();
                }

                if (readBuffer.get() != '\n') {
                    LOG.error("Connection:[" + getId() + "] Missing LFfrom Chunk Data Terminator");
                    throw new IOException("missing LF");
                }
                // put back the read buffer
                _inBuf.putBack(readBuffer);
                // and transition to the next state ...
                _chunkState = ChunkState.STATE_AWAITING_CHUNK_HEADER;
            } else {
                // break out and wait for more data
                return;
            }
        }
            break;

        case STATE_AWAITING_TRAILERS: {

            _chunkCRLFReadState = _inBuf.readCRLFLine(_chunkLineBuffer, CHUNK_LINE_MAX, _chunkCRLFReadState);

            if (_chunkCRLFReadState == CRLFReadState.DONE) {
                // transition to a done state ...
                _chunkState = ChunkState.STATE_DONE;
                // clear out intermediate crlf state
                _chunkCRLFReadState = CRLFReadState.NONE;
                _chunkLineBuffer.setLength(0);
            } else {
                break;
            }
        }
        // fall through if chunk state is done ...

        case STATE_DONE: {
            // clear out existing input buffer ...
            _inBuf.reset();
            // flush chunk buffer ...
            _chunkContentBuffer.flush();
            // and swap it with the real content buffer ...
            _inBuf = _chunkContentBuffer;
            // reset chunk state ...
            _chunkContentBuffer = null;
            // reset chunked flag ...
            _chunked = false;
            // set HTTP DONE state ...
            setState(State.DONE, null);
        }
            break;
        }
    }
}

From source file:org.commoncrawl.io.NIOHttpConnection.java

public void Writeable(NIOClientSocket theSocket) throws IOException {

    if (!theSocket.isOpen()) {
        return;/*from  w ww  .  jav a 2s.  co  m*/
    }

    int amountWritten = 0;

    try {

        boolean contentEOF = false;

        amountWritten = 0;

        if (_outBuf.available() == 0 && _dataSource != null) {
            // read some more data from the data source
            contentEOF = _dataSource.read(_outBuf);
        }

        ByteBuffer bufferToWrite = _outBuf.read();

        if (bufferToWrite != null) {

            try {

                int amountToWrite = bufferToWrite.remaining();

                // if upload rate limiter is not null ...
                if (_uploadRateLimiter != null) {
                    // apply rate limit policy to outbound data ...
                    amountToWrite = _uploadRateLimiter.checkRateLimit(amountToWrite);
                }

                if (amountToWrite != 0) {
                    // if amount to write is less than remaining ...
                    if (amountToWrite < bufferToWrite.remaining()) {
                        // slice the buffer ...
                        ByteBuffer slicedBuffer = bufferToWrite.slice();
                        // limit to amount to write ...
                        slicedBuffer.limit(amountToWrite);
                        // and write to socket ...
                        amountWritten = _socket.write(slicedBuffer);
                        if (amountWritten >= 0) {
                            // advance source buffer manually...
                            bufferToWrite.position(bufferToWrite.position() + amountWritten);
                        }
                    } else {
                        amountWritten = _socket.write(bufferToWrite);
                    }

                    if (_uploadRateLimiter != null) {
                        _uploadRateLimiter.updateStats(amountWritten);

                        // debug output ...
                        BandwidthUtils.BandwidthStats stats = new BandwidthUtils.BandwidthStats();
                        // collect stats
                        _uploadRateLimiter.getStats(stats);
                        // dump stats ...
                        // System.out.println("Connection: "+ this+"Upload Speed:" +
                        // stats.scaledBitsPerSecond + " " + stats.scaledBitsUnits +
                        // " TotalWritten:" + (_cumilativeWritten + amountWritten) );
                        LOG.info("Connection:[" + getId() + "] BytesOut:" + amountWritten + " Upload Speed:"
                                + stats.scaledBitsPerSecond + " " + stats.scaledBitsUnits + " TotalWritten:"
                                + (_totalWritten + amountWritten));
                    }
                }
            } catch (IOException exception) {
                // LOG.error(CCStringUtils.stringifyException(e));
                throw exception;
            }
            _totalWritten += amountWritten;
            _cumilativeWritten += amountWritten;

            // System.out.println("NIOHttpConnection->wrote:" + amountWritten +
            // "Bytes TotalWritten:" + _cumilativeWritten);

            if (bufferToWrite.remaining() > 0) {
                _outBuf.putBack(bufferToWrite);
            }
        }

        if (_totalWritten > 0 && !_outBuf.isDataAvailable() && (_dataSource == null || contentEOF)) {

            _lastReadOrWriteTime = System.currentTimeMillis();

            // transition from sending to receiving ...
            if (_state == State.SENDING_REQUEST) {
                // set up an initial last read time value here ...
                setState(State.RECEIVING_HEADERS, null);
                _selector.registerForRead(theSocket);
            }
        }
    } catch (IOException e) {
        LOG.error("Connection:[" + getId() + "] Writeable for url:" + getURL() + " threw Exception:"
                + e.getMessage());

        setErrorType(ErrorType.IOEXCEPTION);
        setErrorDesc(StringUtils.stringifyException(e));
        setState(State.ERROR, e);

        throw e;
    }

    if (_state == State.SENDING_REQUEST) {
        _selector.registerForReadAndWrite(theSocket);
    } else if (_state.ordinal() >= State.RECEIVING_HEADERS.ordinal()
            && _state.ordinal() < State.DONE.ordinal()) {
        _selector.registerForRead(theSocket);
    }
}