Example usage for java.nio ByteBuffer hasRemaining

List of usage examples for java.nio ByteBuffer hasRemaining

Introduction

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

Prototype

public final boolean hasRemaining() 

Source Link

Document

Indicates if there are elements remaining in this buffer, that is if position < limit .

Usage

From source file:org.apache.carbondata.core.indexstore.blockletindex.BlockDataMap.java

protected int getTotalBlocklets() {
    ByteBuffer byteBuffer = ByteBuffer.wrap(getBlockletRowCountForEachBlock());
    int sum = 0;/*from w ww  .j a va2s .  c  o  m*/
    while (byteBuffer.hasRemaining()) {
        sum += byteBuffer.getShort();
    }
    return sum;
}

From source file:com.github.ambry.utils.UtilsTest.java

@Test
public void testSerializeString() {
    String randomString = getRandomString(10);
    ByteBuffer outputBuffer = ByteBuffer.allocate(4 + randomString.getBytes().length);
    Utils.serializeString(outputBuffer, randomString, StandardCharsets.US_ASCII);
    outputBuffer.flip();/*from ww w  . j  ava  2 s  . c  o  m*/
    int length = outputBuffer.getInt();
    assertEquals("Input and output string lengths don't match", randomString.getBytes().length, length);
    byte[] output = new byte[length];
    outputBuffer.get(output);
    assertFalse("Output buffer shouldn't have any remaining, but has " + outputBuffer.remaining() + " bytes",
            outputBuffer.hasRemaining());
    String outputString = new String(output);
    assertEquals("Input and output strings don't match", randomString, outputString);

    randomString = getRandomString(10) + "";
    outputBuffer = ByteBuffer.allocate(4 + randomString.getBytes().length);
    Utils.serializeString(outputBuffer, randomString, StandardCharsets.US_ASCII);
    outputBuffer.flip();
    length = outputBuffer.getInt();
    assertEquals("Input and output string lengths don't match ", (randomString.getBytes().length - 1), length);
    output = new byte[length];
    outputBuffer.get(output);
    assertFalse("Output buffer shouldn't have any remaining, but has " + outputBuffer.remaining() + " bytes",
            outputBuffer.hasRemaining());
    outputString = new String(output);
    randomString = randomString.substring(0, randomString.length() - 1) + "?";
    assertEquals("Input and output strings don't match", randomString, outputString);

    randomString = "";
    outputBuffer = ByteBuffer.allocate(4);
    Utils.serializeString(outputBuffer, randomString, StandardCharsets.US_ASCII);
    outputBuffer.flip();
    length = outputBuffer.getInt();
    assertEquals("Input and output string lengths don't match", 0, length);
    output = new byte[length];
    outputBuffer.get(output);
    assertFalse("Output buffer shouldn't have any remaining, but has " + outputBuffer.remaining() + " bytes",
            outputBuffer.hasRemaining());
    outputString = new String(output);
    assertEquals("Output string \"" + outputString + "\" expected to be empty", outputString, "");

    randomString = getRandomString(10);
    outputBuffer = ByteBuffer.allocate(4 + randomString.getBytes().length - 1);
    try {
        Utils.serializeString(outputBuffer, randomString, StandardCharsets.US_ASCII);
        Assert.fail("Serialization should have failed due to insufficient space");
    } catch (RuntimeException e) {
    }
}

From source file:org.apache.druid.hll.HyperLogLogCollectorTest.java

private ByteBuffer makeCollectorBuffer(int offset, byte[] initialBytes, int remainingBytes) {
    short numNonZero = 0;
    for (byte initialByte : initialBytes) {
        numNonZero += computeNumNonZero(initialByte);
    }/*from   w  w  w  . j  a  va 2s  .  co  m*/

    final short numNonZeroInRemaining = computeNumNonZero((byte) remainingBytes);
    numNonZero += (short) ((HyperLogLogCollector.NUM_BYTES_FOR_BUCKETS - initialBytes.length)
            * numNonZeroInRemaining);

    ByteBuffer biggerOffset = ByteBuffer.allocate(HyperLogLogCollector.getLatestNumBytesForDenseStorage());
    biggerOffset.put(VersionOneHyperLogLogCollector.VERSION);
    biggerOffset.put((byte) offset);
    biggerOffset.putShort(numNonZero);
    biggerOffset.put((byte) 0);
    biggerOffset.putShort((short) 0);
    biggerOffset.put(initialBytes);
    while (biggerOffset.hasRemaining()) {
        biggerOffset.put((byte) remainingBytes);
    }
    biggerOffset.clear();
    return biggerOffset.asReadOnlyBuffer();
}

From source file:org.carbondata.core.util.CarbonUtil.java

/**
 * @param listOfNodeInfo/*  w  w w . ja v  a  2s.  com*/
 * @param filesLocation
 * @param measureCount
 * @param mdKeySize
 * @param fileSize
 * @return
 */
private static List<BlockletInfo> getBlockletDetails(List<BlockletInfo> listOfNodeInfo, String filesLocation,
        int measureCount, int mdKeySize, long fileSize) {
    long offset = fileSize - CarbonCommonConstants.LONG_SIZE_IN_BYTE;
    FileHolder fileHolder = FileFactory.getFileHolder(FileFactory.getFileType(filesLocation));
    offset = fileHolder.readDouble(filesLocation, offset);
    int totalMetaDataLength = (int) (fileSize - CarbonCommonConstants.LONG_SIZE_IN_BYTE - offset);
    ByteBuffer buffer = ByteBuffer.wrap(fileHolder.readByteArray(filesLocation, offset, totalMetaDataLength));
    buffer.rewind();
    while (buffer.hasRemaining()) {
        int[] msrLength = new int[measureCount];
        long[] msrOffset = new long[measureCount];
        BlockletInfo info = new BlockletInfo();
        byte[] startKey = new byte[mdKeySize];
        byte[] endKey = new byte[mdKeySize];
        info.setFileName(filesLocation);
        info.setNumberOfKeys(buffer.getInt());
        info.setKeyLength(buffer.getInt());
        info.setKeyOffset(buffer.getLong());
        buffer.get(startKey);
        buffer.get(endKey);
        info.setStartKey(startKey);
        info.setEndKey(endKey);
        for (int i = 0; i < measureCount; i++) {
            msrLength[i] = buffer.getInt();
            msrOffset[i] = buffer.getLong();
        }
        info.setMeasureLength(msrLength);
        info.setMeasureOffset(msrOffset);
        listOfNodeInfo.add(info);
    }
    fileHolder.finish();
    return listOfNodeInfo;
}

From source file:org.apache.carbondata.processing.store.writer.AbstractFactDataWriter.java

/**
 * Below method will be used to update the no dictionary start and end key
 *
 * @param key key to be updated/*  ww  w.j  ava 2s .com*/
 * @return return no dictionary key
 */
protected byte[] updateNoDictionaryStartAndEndKey(byte[] key) {
    if (key.length == 0) {
        return key;
    }
    // add key to byte buffer remove the length part of the data
    ByteBuffer buffer = ByteBuffer.wrap(key, 2, key.length - 2);
    // create a output buffer without length
    ByteBuffer output = ByteBuffer.allocate(key.length - 2);
    short numberOfByteToStorLength = 2;
    // as length part is removed, so each no dictionary value index
    // needs to be reshuffled by 2 bytes
    for (int i = 0; i < dataWriterVo.getNoDictionaryCount(); i++) {
        output.putShort((short) (buffer.getShort() - numberOfByteToStorLength));
    }
    // copy the data part
    while (buffer.hasRemaining()) {
        output.put(buffer.get());
    }
    output.rewind();
    return output.array();
}

From source file:com.springrts.springls.Client.java

/**
 * Tries to send the data from the sendQueue.
 * @return true if all data has been flushed; false otherwise.
 *//*from   ww w  .java2s.  c o m*/
public boolean tryToFlushData() {

    if (!alive || halfDead) {
        // disregard any other scheduled writes:
        while (sendQueue.size() != 0) {
            sendQueue.remove();
        }
        return true; // no more data left to be flushed, so return true
    }

    ByteBuffer buf;
    while ((buf = sendQueue.peek()) != null) {
        try {
            sockChan.write(buf);

            if (buf.hasRemaining()) {
                // This happens when send buffer is full and no more data
                // can be written to it.
                // Lets just skip it without removing the packet
                // from the send queue (we will retry sending it later).
                break;
            }
            // remove element from queue (it was sent entirely)
            sendQueue.remove();
        } catch (ClosedChannelException ccex) {
            // no point sending the rest to the closed channel
            if (alive) {
                context.getClients().killClientDelayed(this, "Quit: socket channel closed exception");
            }
            break;
        } catch (IOException ioex) {
            if (alive) {
                context.getClients().killClientDelayed(this, "Quit: socket channel closed exception");
            }
            break;
        }
    }

    return sendQueue.size() == 0;
}

From source file:com.esri.geoevent.solutions.adapter.cap.CAPInboundAdapter.java

@Override
public void receive(ByteBuffer buffer, String channelId) {
    //System.out.println("Processing...");
    String data;/*from ww  w  . ja  v  a  2s .  co  m*/
    while (buffer.hasRemaining()) {
        buffer.mark();

        try {
            byte[] bytearray = new byte[buffer.remaining()];
            buffer.get(bytearray);
            data = new String(bytearray);

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new InputSource(new StringReader(data)));
            NodeList alerts = doc.getElementsByTagName("alert");
            System.out.println();
            System.out.println(new Date().toString() + ": Processing " + alerts.getLength() + " alerts.");
            int procAlerts = 0;
            for (int a = 0; a < alerts.getLength(); a++) {
                Element alert = (Element) alerts.item(a);

                NodeList nodeList = alert.getElementsByTagName("identifier");
                Element line = (Element) nodeList.item(0);

                String identifier = getCharacterDataFromElement(line);
                if (MAP.containsKey(identifier)) {
                    System.out.println(
                            " Alert: " + identifier + " was processed previously. Skipping to next alert.");
                    continue;
                }
                //System.out.println("   Alert "+ a + ": " + identifier + ". Processing now.");
                MAP.put(identifier, identifier);
                procAlerts++;

                GeoEvent alertMsg = parseAlert(alert, identifier);
                if (alertMsg != null) {
                    geoEventListener.receive(alertMsg);
                    System.out.println(" Alert " + a + ": " + identifier);
                    System.out.println(" " + alertMsg.toString());

                    NodeList codes = alert.getElementsByTagName("code");
                    for (int c = 0; c < codes.getLength(); c++) {
                        Element code = (Element) codes.item(c);
                        GeoEvent codeMsg = parseAlertCode(code, identifier);
                        if (codeMsg != null) {
                            geoEventListener.receive(codeMsg);
                            System.out.println("  Code: " + codeMsg.toString());
                        }
                    }

                    NodeList infos = alert.getElementsByTagName("info");
                    for (int i = 0; i < infos.getLength(); i++) {
                        Element info = (Element) infos.item(i);
                        String infoID = identifier + "_" + i;
                        GeoEvent infoMsg = parseAlertInfo(info, identifier, infoID);
                        if (infoMsg != null) {
                            geoEventListener.receive(infoMsg);
                            System.out.println("  Info " + i + ": " + infoID);
                            System.out.println("  " + infoMsg.toString());

                            NodeList categories = info.getElementsByTagName("category");
                            for (int cat = 0; cat < categories.getLength(); cat++) {
                                Element category = (Element) categories.item(cat);
                                GeoEvent catMsg = parseInfoCategory(category, identifier, infoID);
                                if (catMsg != null) {
                                    geoEventListener.receive(catMsg);
                                    System.out.println("   Category: " + catMsg.toString());
                                }
                            }
                            NodeList eventCodes = info.getElementsByTagName("eventCode");
                            for (int e = 0; e < eventCodes.getLength(); e++) {
                                Element eventCode = (Element) eventCodes.item(e);
                                GeoEvent eMsg = parseInfoEventCode(eventCode, identifier, infoID);
                                if (eMsg != null) {
                                    geoEventListener.receive(eMsg);
                                    System.out.println("   Event code: " + eMsg.toString());
                                }
                            }
                            NodeList responseTypes = info.getElementsByTagName("responseType");
                            for (int rt = 0; rt < responseTypes.getLength(); rt++) {
                                Element responseType = (Element) responseTypes.item(rt);
                                GeoEvent rtMsg = parseInfoResponseType(responseType, identifier, infoID);
                                if (rtMsg != null) {
                                    geoEventListener.receive(rtMsg);
                                    System.out.println("   Response type: " + rtMsg.toString());
                                }
                            }
                            NodeList parameters = info.getElementsByTagName("parameter");
                            for (int p = 0; p < parameters.getLength(); p++) {
                                Element parameter = (Element) parameters.item(p);
                                GeoEvent pMsg = parseInfoParameter(parameter, identifier, infoID);
                                if (pMsg != null) {
                                    geoEventListener.receive(pMsg);
                                    System.out.println("   Parameter: " + pMsg.toString());
                                }
                            }
                            NodeList resources = info.getElementsByTagName("resource");
                            for (int r = 0; r < resources.getLength(); r++) {
                                Element resource = (Element) resources.item(r);
                                GeoEvent rMsg = parseInfoResource(resource, identifier, infoID);
                                if (rMsg != null) {
                                    geoEventListener.receive(rMsg);
                                    System.out.println("   Resource " + r + ": ");
                                    System.out.println("   " + rMsg.toString());
                                }
                            }
                            NodeList areas = info.getElementsByTagName("area");
                            for (int ar = 0; ar < areas.getLength(); ar++) {
                                Element area = (Element) areas.item(ar);
                                String areaID = infoID + "_" + ar;
                                GeoEvent areaMsg = parseInfoArea(area, identifier, infoID, areaID);
                                if (areaMsg != null) {
                                    geoEventListener.receive(areaMsg);
                                    System.out.println("   Area " + ar + ": ");
                                    System.out.println("    " + areaMsg.toString());

                                    NodeList polygons = info.getElementsByTagName("polygon");
                                    for (int pg = 0; pg < polygons.getLength(); pg++) {
                                        Element polygon = (Element) polygons.item(pg);
                                        System.out.println("     Polygon " + pg + ": ");
                                        GeoEvent areaGeomMsg = parseInfoAreaGeom(polygon, null, null,
                                                identifier, infoID, areaID);
                                        if (areaGeomMsg != null) {
                                            geoEventListener.receive(areaGeomMsg);
                                            System.out.println("      " + areaGeomMsg.toString());
                                        } else {
                                            System.out.println("      " + getCharacterDataFromElement(polygon));
                                        }
                                    }

                                    NodeList circles = info.getElementsByTagName("circle");
                                    for (int c = 0; c < circles.getLength(); c++) {
                                        Element circle = (Element) circles.item(c);
                                        System.out.println("     Circle " + c + ": ");
                                        GeoEvent areaGeomMsg = parseInfoAreaGeom(null, circle, null, identifier,
                                                infoID, areaID);
                                        if (areaGeomMsg != null) {
                                            geoEventListener.receive(areaGeomMsg);
                                            System.out.println("      " + areaGeomMsg.toString());
                                        } else {
                                            System.out.println("      " + getCharacterDataFromElement(circle));
                                        }
                                    }

                                    NodeList geocodes = info.getElementsByTagName("geocode");
                                    for (int g = 0; g < geocodes.getLength(); g++) {
                                        Element geocode = (Element) geocodes.item(g);
                                        GeoEvent areaGeomMsg = parseInfoAreaGeom(null, null, geocode,
                                                identifier, infoID, areaID);
                                        if (areaGeomMsg != null) {
                                            geoEventListener.receive(areaGeomMsg);
                                            System.out.println("     Geocode " + g + ": ");
                                            System.out.println("      " + areaGeomMsg.toString());
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //System.out.println("Processed " + procAlerts + " of " + alerts.getLength() + " alerts.");

        } catch (Exception e) {
            String msg = e.getMessage();
            System.out.println(msg);
            e.printStackTrace();
        }

        return;
    }
}

From source file:org.cloudata.core.commitlog.pipe.Bulk.java

public OperationResult read(SocketChannel ch) throws IOException {
    if (bufferList.isEmpty()) {
        throw new IOException("Pipe is closed");
    }/*from w w w. j  a v  a 2  s  . com*/

    while (true) {
        ByteBuffer buf = bufferList.get(currentReadBufIndex);
        int numRead = 0;
        try {
            if ((numRead = testProxy.readFromChannel(ch, buf)) < 0) {
                throw new IOException("Disconnect from previous");
            }
        } catch (UndeclaredThrowableException e) {
            ProxyExceptionHelper.handleException(e, LOG);
        }

        LOG.debug("num read : " + numRead);
        totalNumRead += numRead;

        if (!readingHeaderDone) {
            readingHeaderDone = readHeader(buf);
        }

        if (totalNumRead >= headerAndPayloadSize) {
            if (totalNumRead > headerAndPayloadSize) {
                LOG.warn("buffer over read. totalNumRead : " + totalNumRead + ", but expected size : "
                        + headerAndPayloadSize);
            }

            LOG.debug("total num read : " + totalNumRead + ", expected : " + headerAndPayloadSize);

            return OperationResult.completed;
        }

        if (buf.hasRemaining()) {
            LOG.debug("total num read : " + totalNumRead + ", expected : " + headerAndPayloadSize);

            return OperationResult.partially;
        }

        currentReadBufIndex++;
        LOG.debug("buffer commit, index[" + currentReadBufIndex + "]");

        if (bufferList.size() <= currentReadBufIndex) {
            addNewBuffersToList();
            LOG.debug("bulk [" + this + "] append new buffer. bufferList.size : " + bufferList.size());
        }
    }
}

From source file:org.bimserver.collada.ColladaSerializer.java

private void setGeometry(PrintWriter out, IfcProduct ifcProductObject, String material)
        throws RenderEngineException, SerializerException {
    // Mostly just skips IfcOpeningElements which one would probably not want to end up in the Collada file.
    if (ifcProductObject instanceof IfcFeatureElementSubtraction)
        return;/*  w  w  w . j a va2s .co m*/
    //
    GeometryInfo geometryInfo = ifcProductObject.getGeometry();
    if (geometryInfo != null && geometryInfo.getTransformation() != null) {
        GeometryData geometryData = geometryInfo.getData();
        ByteBuffer indicesBuffer = ByteBuffer.wrap(geometryData.getIndices());
        indicesBuffer.order(ByteOrder.LITTLE_ENDIAN);
        // TODO: In Blender (3d modeling tool) and Three.js, normals are ignored in favor of vertex order. The incoming geometry seems to be in order 0 1 2 when it needs to be in 1 0 2. Need more test cases.
        // Failing order: (0, 1050, 2800), (0, 1050, 3100), (3580, 1050, 3100)
        // Successful order: (0, 1050, 3100), (0, 1050, 2800), (3580, 1050, 3100)
        List<Integer> list = new ArrayList<Integer>();
        while (indicesBuffer.hasRemaining())
            list.add(indicesBuffer.getInt());
        indicesBuffer.rewind();
        for (int i = 0; i < list.size(); i += 3) {
            Integer first = list.get(i);
            Integer next = list.get(i + 1);
            list.set(i, next);
            list.set(i + 1, first);
        }
        // Positions the X or the Y or the Z of (X, Y, Z).
        ByteBuffer positionsBuffer = ByteBuffer.wrap(geometryData.getVertices());
        positionsBuffer.order(ByteOrder.LITTLE_ENDIAN);
        // Do pass to find highest Z for considered objects.
        while (positionsBuffer.hasRemaining()) {
            float x = positionsBuffer.getFloat();
            float y = positionsBuffer.getFloat();
            float z = positionsBuffer.getFloat();
            // X
            if (x > highestObserved.x())
                highestObserved.x(x);
            else if (x < lowestObserved.x())
                lowestObserved.x(x);
            // Y
            if (y > highestObserved.y())
                highestObserved.y(y);
            else if (y < lowestObserved.y())
                lowestObserved.y(y);
            // Z
            if (z > highestObserved.z())
                highestObserved.z(z);
            else if (z < lowestObserved.z())
                lowestObserved.z(z);
        }
        positionsBuffer.rewind();
        //
        ByteBuffer normalsBuffer = ByteBuffer.wrap(geometryData.getNormals());
        normalsBuffer.order(ByteOrder.LITTLE_ENDIAN);
        // Create a geometry identification number in the form of: geom-320450
        long oid = ifcProductObject.getOid();
        String id = String.format("geom-%d", oid);
        // If the material doesn't exist in the converted map, add it.
        if (!converted.containsKey(material))
            converted.put(material, new HashSet<IfcProduct>());
        // Add the current IfcProduct to the appropriate entry in the material map.
        converted.get(material).add(ifcProductObject);
        // Name for geometry.
        String name = (ifcProductObject.getGlobalId() == null) ? "[NO_GUID]" : ifcProductObject.getGlobalId();
        // Counts.
        int vertexComponentsTotal = positionsBuffer.capacity() / 4,
                normalComponentsTotal = normalsBuffer.capacity() / 4;
        int verticesCount = positionsBuffer.capacity() / 12, normalsCount = normalsBuffer.capacity() / 12,
                triangleCount = indicesBuffer.capacity() / 12;
        // Vertex scalars as one long string: 4.05 2 1 55.0 34.01 2
        String stringPositionScalars = byteBufferToFloatingPointSpaceDelimitedString(positionsBuffer);
        // Normal scalars as one long string: 4.05 2 1 55.0 34.01 2
        String stringNormalScalars = byteBufferToFloatingPointSpaceDelimitedString(normalsBuffer); //doubleBufferToFloatingPointSpaceDelimitedString(flippedNormalsBuffer);
        // Vertex indices as one long string: 1 0 2 0 3 2 5 4 6
        String stringIndexScalars = listToSpaceDelimitedString(list, intFormat);
        // Write geometry block for this IfcProduct (i.e. IfcRoof, IfcSlab, etc).
        out.println(" <geometry id=\"" + id + "\" name=\"" + name + "\">");
        out.println("  <mesh>");
        out.println("   <source id=\"positions-" + oid + "\" name=\"positions-" + oid + "\">");
        out.println("    <float_array id=\"positions-array-" + oid + "\" count=\"" + vertexComponentsTotal
                + "\">" + stringPositionScalars + "</float_array>");
        out.println("    <technique_common>");
        out.println("     <accessor count=\"" + verticesCount + "\" offset=\"0\" source=\"#positions-array-"
                + oid + "\" stride=\"3\">");
        out.println("      <param name=\"X\" type=\"float\"></param>");
        out.println("      <param name=\"Y\" type=\"float\"></param>");
        out.println("      <param name=\"Z\" type=\"float\"></param>");
        out.println("     </accessor>");
        out.println("    </technique_common>");
        out.println("   </source>");
        out.println("   <source id=\"normals-" + oid + "\" name=\"normals-" + oid + "\">");
        out.println("    <float_array id=\"normals-array-" + oid + "\" count=\"" + normalComponentsTotal + "\">"
                + stringNormalScalars + "</float_array>");
        out.println("    <technique_common>");
        out.println("     <accessor count=\"" + normalsCount + "\" offset=\"0\" source=\"#normals-array-" + oid
                + "\" stride=\"3\">");
        out.println("      <param name=\"X\" type=\"float\"></param>");
        out.println("      <param name=\"Y\" type=\"float\"></param>");
        out.println("      <param name=\"Z\" type=\"float\"></param>");
        out.println("     </accessor>");
        out.println("    </technique_common>");
        out.println("   </source>");
        out.println("   <vertices id=\"vertices-" + oid + "\">");
        out.println("    <input semantic=\"POSITION\" source=\"#positions-" + oid + "\"/>");
        out.println("    <input semantic=\"NORMAL\" source=\"#normals-" + oid + "\"/>");
        out.println("   </vertices>");
        out.println("   <triangles count=\"" + triangleCount + "\" material=\"Material-" + oid + "\">");
        out.println("    <input offset=\"0\" semantic=\"VERTEX\" source=\"#vertices-" + oid + "\"/>");
        out.println("    <p>" + stringIndexScalars + "</p>");
        out.println("   </triangles>");
        out.println("  </mesh>");
        out.println(" </geometry>");
    }
}

From source file:net.kungfoo.grizzly.proxy.impl.ConnectingHandler.java

public void inputReady(final NHttpClientConnection conn, final ContentDecoder decoder) {
    System.out.println(conn + " [proxy<-origin] input ready");

    HttpContext context = conn.getContext();
    ProxyProcessingInfo proxyTask = (ProxyProcessingInfo) context.getAttribute(ProxyProcessingInfo.ATTRIB);

    synchronized (proxyTask) {
        ConnState connState = proxyTask.getOriginState();
        if (connState != ConnState.RESPONSE_RECEIVED && connState != ConnState.RESPONSE_BODY_STREAM) {
            throw new IllegalStateException("Illegal target connection state: " + connState);
        }//from w w  w. ja  va  2 s .  co  m

        final Response response = proxyTask.getResponse();
        try {

            ByteBuffer dst = proxyTask.getOutBuffer();
            int bytesRead = decoder.read(dst);
            if (bytesRead > 0) {
                dst.flip();
                final ByteChunk chunk = new ByteChunk(bytesRead);
                final byte[] buf = new byte[bytesRead];
                dst.get(buf);
                chunk.setBytes(buf, 0, bytesRead);
                dst.compact();
                try {
                    response.doWrite(chunk);
                } catch (ClassCastException e) {
                    System.err.println("gone bad: " + e.getMessage());
                    e.printStackTrace(System.err);
                }
                response.flush();
                System.out.println(conn + " [proxy<-origin] " + bytesRead + " bytes read");
                System.out.println(conn + " [proxy<-origin] " + decoder);
            }
            if (!dst.hasRemaining()) {
                // Output buffer is full. Suspend origin input until
                // the client handler frees up some space in the buffer
                conn.suspendInput();
            }
            /*
                    // If there is some content in the buffer make sure client output
                    // is active
                    if (dst.position() > 0) {
                      proxyTask.getClientIOControl().requestOutput();
                    }
            */

            if (decoder.isCompleted()) {
                System.out.println(conn + " [proxy<-origin] response body received");
                proxyTask.setOriginState(ConnState.RESPONSE_BODY_DONE);
                if (!this.connStrategy.keepAlive(conn.getHttpResponse(), context)) {
                    System.out.println(conn + " [proxy<-origin] close connection");
                    proxyTask.setOriginState(ConnState.CLOSING);
                    conn.close();
                }
                proxyTask.getCompletion().run();
            } else {
                proxyTask.setOriginState(ConnState.RESPONSE_BODY_STREAM);
            }

        } catch (IOException ex) {
            shutdownConnection(conn);
        }
    }
}