Example usage for java.nio ByteBuffer put

List of usage examples for java.nio ByteBuffer put

Introduction

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

Prototype

public ByteBuffer put(byte[] src, int off, int len) 

Source Link

Document

Writes bytes in the given byte array, starting from the specified offset, to the current position and increases the position by the number of bytes written.

Usage

From source file:com.amazonaws.http.UrlHttpClient.java

private void write(InputStream is, OutputStream os, CurlBuilder curlBuilder, ByteBuffer curlBuffer)
        throws IOException {
    final byte[] buf = new byte[DEFAULT_BUFFER_SIZE * BUFFER_SIZE_MULTIPLIER];
    int len;/* w  w  w  .  j av  a2s.c  om*/
    while ((len = is.read(buf)) != -1) {
        try {
            if (curlBuffer != null) {
                curlBuffer.put(buf, 0 /* offset */, len);
            }
        } catch (final BufferOverflowException e) {
            curlBuilder.setContentOverflow(true);
        }
        os.write(buf, 0, len);
    }
}

From source file:com.serenegiant.media.TLMediaEncoder.java

/**
 * read raw bit stream from specific intermediate file
 * @param in//from w ww  . j  a va  2  s .co  m
 * @param header
 * @param buffer
 * @param readBuffer
 * @throws IOException
 * @throws BufferOverflowException
 */
/*package*/static ByteBuffer readStream(final DataInputStream in, final TLMediaFrameHeader header,
        ByteBuffer buffer, final byte[] readBuffer) throws IOException {

    readHeader(in, header);
    if ((buffer == null) || header.size > buffer.capacity()) {
        buffer = ByteBuffer.allocateDirect(header.size);
    }
    buffer.clear();
    final int max_bytes = Math.min(readBuffer.length, header.size);
    int read_bytes;
    for (int i = header.size; i > 0; i -= read_bytes) {
        read_bytes = in.read(readBuffer, 0, Math.min(i, max_bytes));
        if (read_bytes <= 0)
            break;
        buffer.put(readBuffer, 0, read_bytes);
    }
    buffer.flip();
    return buffer;
}

From source file:com.smartitengineering.util.simple.io.BufferedInputStream.java

@Override
public int read() throws IOException {
    ByteBuffer buffer = getCurrentBuffer();
    if (available() > 0) {
        return buffer.get();
    }//  w  w  w  . j  a  v  a2 s. co  m
    int remaining = buffer.remaining();
    if (remaining <= 0) {
        if (hasNextBuffer()) {
            currentBuffer = nextBuffer();
            return read();
        } else if (eofReached) {
            return -1;
        } else {
            remaining = initializeNewBuffer();
            buffer = getCurrentBuffer();
        }
    }
    byte[] readBuffer = new byte[remaining];
    int read = wrappedStream.read(readBuffer);
    if (read > 0) {
        int position = buffer.position();
        buffer.put(readBuffer, 0, read);
        buffer.position(position);
        get(buffer).add(read);
        return buffer.get();
    } else {
        eofReached = true;
        return -1;
    }
}

From source file:edu.uci.ics.crawler4j.crawler.fetcher.PageFetcher.java

private boolean loadPage(final Page p, final InputStream in, final int totalsize, final boolean isBinary,
        String encoding) {/* www .j  a v  a  2s . c om*/
    ByteBuffer bBuf;

    if (totalsize > 0) {
        bBuf = ByteBuffer.allocate(totalsize + 1024);
    } else {
        bBuf = ByteBuffer.allocate(maxDownloadSize);
    }
    final byte[] b = new byte[1024];
    int len;
    double finished = 0;
    try {
        while ((len = in.read(b)) != -1) {
            if (finished + b.length > bBuf.capacity()) {
                break;
            }
            bBuf.put(b, 0, len);
            finished += len;
        }
    } catch (final BufferOverflowException boe) {
        System.out.println("Page size exceeds maximum allowed.");
        return false;
    } catch (final Exception e) {
        System.err.println(e.getMessage());
        return false;
    }

    bBuf.flip();
    if (isBinary) {
        byte[] tmp = new byte[bBuf.limit()];
        bBuf.get(tmp);
        p.setBinaryData(tmp);
    } else {
        String html = "";
        if (encoding == null) {
            int pos = bBuf.position();
            html = Charset.forName("US-ASCII").decode(bBuf).toString();
            bBuf.position(pos);
            pos = html.toLowerCase().indexOf("<meta http-equiv=\"content-type\" content=\"");
            if (pos >= 0) {
                int end = html.indexOf("\"", pos + 41);
                if (end >= 0) {
                    String content = html.substring(pos, end);
                    if (content.contains("charset=")) {
                        encoding = content.substring(content.indexOf("charset=") + 8);
                    }
                }
            }
        }
        if (encoding == null || !Charset.isSupported(encoding))
            encoding = "UTF-8";

        if (!encoding.equals("UTF-8")) {
            html = Charset.forName(encoding).decode(bBuf).toString();
        }

        if (html.length() == 0) {
            return false;
        }
        p.setHTML(html);
    }
    return true;
}

From source file:com.mortardata.pig.storage.DynamoDBStorage.java

private WriteRequestWithCapacity getWriteRequestWithCapacity(Tuple tuple) throws IOException {
    ResourceFieldSchema[] fields = this.schema.getFields();
    Map<String, AttributeValue> dynamoItem = new HashMap<String, AttributeValue>();
    int dataSize = 0;
    int dynamoItemSize = 0;
    int tupleSize = tuple.size();
    for (int i = 0; i < tupleSize; i++) {
        Object field = tuple.get(i);
        AttributeValue dynamoValue = null;

        switch (DataType.findType(field)) {

        case DataType.NULL:
            // dynamodb does not support null values
            // simply don't write field
            reportCounter(DYNAMO_COUNTER_NULL_FIELDS_DISCARDED, 1);
            break;
        case DataType.BOOLEAN:
            if (((Boolean) field).booleanValue()) {
                dynamoValue = new AttributeValue().withN("1");
            } else {
                dynamoValue = new AttributeValue().withN("0");
            }//ww w .  ja v  a2 s  . c  om
            dataSize += 1;
            dynamoItemSize += 1;
            break;
        case DataType.INTEGER:
        case DataType.LONG:
        case DataType.FLOAT:
        case DataType.DOUBLE:
            String numAsString = field.toString();
            dynamoValue = new AttributeValue().withN(numAsString);
            dataSize += numAsString.length();
            dynamoItemSize += numAsString.length();
            break;
        case DataType.BYTEARRAY:
            byte[] b = ((DataByteArray) field).get();
            ByteBuffer buffer = ByteBuffer.allocate(b.length);
            buffer.put(b, 0, b.length);
            buffer.position(0);
            dynamoValue = new AttributeValue().withB(buffer);
            dataSize += b.length;
            dynamoItemSize += b.length;
            break;
        case DataType.CHARARRAY:
            String fieldStr = (String) field;
            int fieldLen = fieldStr.length();
            if (fieldLen > 0) {
                dynamoValue = new AttributeValue().withS(fieldStr);
                dataSize += fieldLen;
                dynamoItemSize += fieldLen;
            } else {
                // DynamoDB cannot handle empty strings
                reportCounter(DYNAMO_COUNTER_EMPTY_STRING_FIELDS_DISCARDED, 1);
            }
            break;
        case DataType.BYTE:
            ByteBuffer buf = ByteBuffer.allocate(1);
            buf.put((Byte) field);
            buf.position(0);
            dynamoValue = new AttributeValue().withB(buf);
            dataSize += 1;
            dynamoItemSize += 1;
            break;
        case DataType.MAP:
        case DataType.TUPLE:
        case DataType.BAG:
            throw new RuntimeException("DynamoDBStorage does not support Maps, Tuples or Bags");
        }

        if (dynamoValue != null) {
            ResourceFieldSchema fieldSchema = fields[i];
            String fieldName = fieldSchema.getName();
            if (fieldName == null) {
                throw new IllegalArgumentException(
                        "Cannot write a field with no name (element " + i + " )  FieldSchema: " + fields);
            }
            dynamoItemSize += fieldName.length();
            dynamoItem.put(fieldName, dynamoValue);
        }
    }

    // check for max item size
    if (dynamoItemSize > DYNAMO_MAX_ITEM_SIZE_IN_BYTES) {
        throw new RuntimeException("Item size " + dynamoItemSize + " bytes is larger than max dynamo item size "
                + DYNAMO_MAX_ITEM_SIZE_IN_BYTES + ". Aborting. Item: " + dynamoItem);
    }

    WriteRequest writeRequest = new WriteRequest().withPutRequest(new PutRequest().withItem(dynamoItem));

    return new WriteRequestWithCapacity(writeRequest, dynamoItemSize, dataSize);

}

From source file:com.samsung.px.pig.storage.DynamoDBStorage.java

private WriteRequestWithCapacity getWriteRequestWithCapacity(Tuple tuple) throws IOException {
    ResourceFieldSchema[] fields = this.schema.getFields();
    Map<String, AttributeValue> dynamoItem = new HashMap<String, AttributeValue>();
    int dataSize = 0;
    int dynamoItemSize = 0;
    int tupleSize = tuple.size();
    for (int i = 0; i < tupleSize; i++) {
        Object field = tuple.get(i);
        AttributeValue dynamoValue = null;

        switch (DataType.findType(field)) {

        case DataType.NULL:
            // dynamodb does not support null values
            // simply don't write field
            reportCounter(DYNAMO_COUNTER_NULL_FIELDS_DISCARDED, 1);
            break;
        case DataType.BOOLEAN:
            if (((Boolean) field).booleanValue()) {
                dynamoValue = new AttributeValue().withN("1");
            } else {
                dynamoValue = new AttributeValue().withN("0");
            }/* w w  w  .  j av  a  2s  .  c  om*/
            dataSize += 1;
            dynamoItemSize += 1;
            break;
        case DataType.INTEGER:
        case DataType.LONG:
        case DataType.FLOAT:
        case DataType.DOUBLE:
            String numAsString = field.toString();
            dynamoValue = new AttributeValue().withN(numAsString);
            dataSize += numAsString.length();
            dynamoItemSize += numAsString.length();
            break;
        case DataType.BYTEARRAY:
            byte[] b = ((DataByteArray) field).get();
            ByteBuffer buffer = ByteBuffer.allocate(b.length);
            buffer.put(b, 0, b.length);
            buffer.position(0);
            dynamoValue = new AttributeValue().withB(buffer);
            dataSize += b.length;
            dynamoItemSize += b.length;
            break;
        case DataType.CHARARRAY:
            String fieldStr = (String) field;
            int fieldLen = fieldStr.length();
            if (fieldLen > 0) {
                dynamoValue = new AttributeValue().withS(fieldStr);
                dataSize += fieldLen;
                dynamoItemSize += fieldLen;
            } else {
                // DynamoDB cannot handle empty strings
                reportCounter(DYNAMO_COUNTER_EMPTY_STRING_FIELDS_DISCARDED, 1);
            }
            break;
        case DataType.BYTE:
            ByteBuffer buf = ByteBuffer.allocate(1);
            buf.put((Byte) field);
            buf.position(0);
            dynamoValue = new AttributeValue().withB(buf);
            dataSize += 1;
            dynamoItemSize += 1;
            break;
        case DataType.MAP:
        case DataType.TUPLE:
            Tuple listTuple = (Tuple) field;

            if (listTuple.size() > 0) {

                Collection<String> listOfValues = new ArrayList<String>();
                for (int k = 0; k < listTuple.size(); k++) {
                    String strItem = (String) listTuple.get(k);
                    int itemLen = strItem.length();
                    dataSize += itemLen;
                    dynamoItemSize += itemLen;
                    listOfValues.add(strItem);
                }
                dynamoValue = new AttributeValue().withSS(listOfValues);

            } else {
                // DynamoDB cannot handle empty strings
                reportCounter(DYNAMO_COUNTER_EMPTY_STRING_FIELDS_DISCARDED, 1);
            }
            break;
        case DataType.BAG:
            throw new RuntimeException("DynamoDBStorage does not support Maps, Tuples or Bags");
        }

        if (dynamoValue != null) {
            ResourceFieldSchema fieldSchema = fields[i];
            String fieldName = fieldSchema.getName();
            if (fieldName == null) {
                throw new IllegalArgumentException(
                        "Cannot write a field with no name (element " + i + " )  FieldSchema: " + fields);
            }
            dynamoItemSize += fieldName.length();
            dynamoItem.put(fieldName, dynamoValue);
        }
    }

    // check for max item size
    if (dynamoItemSize > DYNAMO_MAX_ITEM_SIZE_IN_BYTES) {
        throw new RuntimeException("Item size " + dynamoItemSize + " bytes is larger than max dynamo item size "
                + DYNAMO_MAX_ITEM_SIZE_IN_BYTES + ". Aborting. Item: " + dynamoItem);
    }

    WriteRequest writeRequest = new WriteRequest().withPutRequest(new PutRequest().withItem(dynamoItem));

    return new WriteRequestWithCapacity(writeRequest, dynamoItemSize, dataSize);

}

From source file:guru.benson.pinch.Pinch.java

/**
 * Gets the list of files included in the ZIP stored on the HTTP server.
 *
 * @return A list of ZIP entries./*ww w  .j  a  v a 2s .  co m*/
 */
public ArrayList<ExtendedZipEntry> parseCentralDirectory() {
    int contentLength = getHttpFileSize();
    if (contentLength <= 0) {
        return null;
    }

    if (!findCentralDirectory(contentLength)) {
        return null;
    }

    HttpURLConnection conn = null;
    InputStream bis = null;

    long start = centralDirectoryOffset;
    long end = start + centralDirectorySize - 1;

    byte[] data = new byte[2048];
    ByteBuffer buf = ByteBuffer.allocate(centralDirectorySize);

    try {
        conn = openConnection();
        conn.setRequestProperty("Range", "bytes=" + start + "-" + end);
        conn.setInstanceFollowRedirects(true);
        conn.connect();

        int responseCode = conn.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_PARTIAL) {
            throw new IOException("Unexpected HTTP server response: " + responseCode);
        }

        bis = conn.getInputStream();
        int read, bytes = 0;
        while ((read = bis.read(data)) != -1) {
            buf.put(data, 0, read);
            bytes += read;
        }

        log("Central directory is " + bytes + " bytes");

    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        close(bis);
        disconnect(conn);
    }

    return parseHeaders(buf);
}

From source file:io.warp10.continuum.gts.GTSDecoder.java

/**
 * Attempt to read the next measurement and associated metadata (timestamp, location, elevation)
 * @return true if a measurement was successfully read, false if none were left in the buffer.
 *//*from  ww  w .j  av  a 2s .  c o  m*/
public boolean next() {

    //
    // Update position prior to reading the next value, etc so we can 
    //

    this.position = this.buffer.position();

    if (!buffer.hasRemaining()) {
        return false;
    }

    this.nextCalled = true;

    //
    // Read timestamp/type flag
    //

    byte tsTypeFlag = buffer.get();

    //
    // Check if we encountered encrypted data
    //

    if (GTSEncoder.FLAGS_ENCRYPTED == (tsTypeFlag & GTSEncoder.FLAGS_MASK_ENCRYPTED)) {
        //
        // Extract encrypted length
        //

        int enclen = (int) Varint.decodeUnsignedLong(buffer);

        //
        // If there is no decryption key, simply skip the encrypted data
        // and call next recursively.
        //

        if (null == wrappingKey) {
            buffer.position(buffer.position() + enclen);

            // WARNING(hbs): if there are many encrypted chunks this may lead to a stack overflow
            return next();
        }

        byte[] encrypted = new byte[enclen];
        buffer.get(encrypted);

        //
        // Decrypt the encrypted data
        //

        AESWrapEngine engine = new AESWrapEngine();
        CipherParameters params = new KeyParameter(this.wrappingKey);
        engine.init(false, params);

        try {
            byte[] decrypted = engine.unwrap(encrypted, 0, encrypted.length);
            //
            // Unpad the decrypted data
            //

            PKCS7Padding padding = new PKCS7Padding();
            int padcount = padding.padCount(decrypted);

            //
            // Replace the current buffer with a new one containing the
            // decrypted data followed by any remaining data in the original
            // buffer.
            //

            ByteBuffer bb = ByteBuffer.allocate(decrypted.length - padcount + this.buffer.remaining());

            bb.put(decrypted, 0, decrypted.length - padcount);
            bb.put(this.buffer);
            bb.flip();

            this.buffer = bb;
        } catch (InvalidCipherTextException icte) {
            // FIXME(hbs): log this somewhere...
            //
            // Skip the encrypted chunk we failed to decrypt
            //
        }

        //
        // Call next recursively
        //
        // WARNING(hbs): we may hit StackOverflow in some cases

        return next();
    }

    //
    // Read location/elevation flag if needed
    //

    byte locElevFlag = 0x0;

    if (GTSEncoder.FLAGS_CONTINUATION == (tsTypeFlag & GTSEncoder.FLAGS_CONTINUATION)) {
        if (!buffer.hasRemaining()) {
            return false;
        }

        locElevFlag = buffer.get();
    }

    //
    // Read timestamp
    //

    switch (tsTypeFlag & GTSEncoder.FLAGS_MASK_TIMESTAMP) {
    case GTSEncoder.FLAGS_TIMESTAMP_RAW_ABSOLUTE: {
        ByteOrder order = buffer.order();
        buffer.order(ByteOrder.BIG_ENDIAN);
        previousLastTimestamp = lastTimestamp;
        lastTimestamp = buffer.getLong();
        buffer.order(order);
    }
        break;
    //case GTSEncoder.FLAGS_TIMESTAMP_ZIGZAG_ABSOLUTE:
    //  previousLastTimestamp = lastTimestamp;
    //  lastTimestamp = Varint.decodeSignedLong(buffer);
    //  break;
    case GTSEncoder.FLAGS_TIMESTAMP_EQUALS_BASE:
        previousLastTimestamp = lastTimestamp;
        lastTimestamp = baseTimestamp;
        break;
    case GTSEncoder.FLAGS_TIMESTAMP_ZIGZAG_DELTA_BASE: {
        long delta = Varint.decodeSignedLong(buffer);
        previousLastTimestamp = lastTimestamp;
        lastTimestamp = baseTimestamp + delta;
    }
        break;
    case GTSEncoder.FLAGS_TIMESTAMP_ZIGZAG_DELTA_PREVIOUS: {
        long delta = Varint.decodeSignedLong(buffer);
        previousLastTimestamp = lastTimestamp;
        lastTimestamp = lastTimestamp + delta;
    }
        break;
    default:
        throw new RuntimeException("Invalid timestamp format.");
    }

    //
    // Read location/elevation
    //

    if (GTSEncoder.FLAGS_LOCATION == (locElevFlag & GTSEncoder.FLAGS_LOCATION)) {
        if (GTSEncoder.FLAGS_LOCATION_IDENTICAL != (locElevFlag & GTSEncoder.FLAGS_LOCATION_IDENTICAL)) {
            if (GTSEncoder.FLAGS_LOCATION_GEOXPPOINT_ZIGZAG_DELTA == (locElevFlag
                    & GTSEncoder.FLAGS_LOCATION_GEOXPPOINT_ZIGZAG_DELTA)) {
                long delta = Varint.decodeSignedLong(buffer);
                previousLastGeoXPPoint = lastGeoXPPoint;
                lastGeoXPPoint = lastGeoXPPoint + delta;
            } else {
                ByteOrder order = buffer.order();
                buffer.order(ByteOrder.BIG_ENDIAN);
                previousLastGeoXPPoint = lastGeoXPPoint;
                lastGeoXPPoint = buffer.getLong();
                buffer.order(order);
            }
        }
    } else {
        previousLastGeoXPPoint = lastGeoXPPoint;
        lastGeoXPPoint = GeoTimeSerie.NO_LOCATION;
    }

    if (GTSEncoder.FLAGS_ELEVATION == (locElevFlag & GTSEncoder.FLAGS_ELEVATION)) {
        if (GTSEncoder.FLAGS_ELEVATION_IDENTICAL != (locElevFlag & GTSEncoder.FLAGS_ELEVATION_IDENTICAL)) {
            boolean zigzag = GTSEncoder.FLAGS_ELEVATION_ZIGZAG == (locElevFlag
                    & GTSEncoder.FLAGS_ELEVATION_ZIGZAG);

            long encoded;

            if (zigzag) {
                encoded = Varint.decodeSignedLong(buffer);
            } else {
                ByteOrder order = buffer.order();
                buffer.order(ByteOrder.BIG_ENDIAN);
                encoded = buffer.getLong();
                buffer.order(order);
            }

            if (GTSEncoder.FLAGS_ELEVATION_DELTA_PREVIOUS == (locElevFlag
                    & GTSEncoder.FLAGS_ELEVATION_DELTA_PREVIOUS)) {
                previousLastElevation = lastElevation;
                lastElevation = lastElevation + encoded;
            } else {
                previousLastElevation = lastElevation;
                lastElevation = encoded;
            }
        }
    } else {
        previousLastElevation = lastElevation;
        lastElevation = GeoTimeSerie.NO_ELEVATION;
    }

    //
    // Extract value
    //

    switch (tsTypeFlag & GTSEncoder.FLAGS_MASK_TYPE) {
    case GTSEncoder.FLAGS_TYPE_LONG:
        lastType = TYPE.LONG;
        if (GTSEncoder.FLAGS_VALUE_IDENTICAL != (tsTypeFlag & GTSEncoder.FLAGS_VALUE_IDENTICAL)) {
            long encoded;

            if (GTSEncoder.FLAGS_LONG_ZIGZAG == (tsTypeFlag & GTSEncoder.FLAGS_LONG_ZIGZAG)) {
                encoded = Varint.decodeSignedLong(buffer);
            } else {
                ByteOrder order = buffer.order();
                buffer.order(ByteOrder.BIG_ENDIAN);
                encoded = buffer.getLong();
                buffer.order(order);
            }

            if (GTSEncoder.FLAGS_LONG_DELTA_PREVIOUS == (tsTypeFlag & GTSEncoder.FLAGS_LONG_DELTA_PREVIOUS)) {
                previousLastLongValue = lastLongValue;
                lastLongValue = lastLongValue + encoded;
            } else {
                previousLastLongValue = lastLongValue;
                lastLongValue = encoded;
            }
        } else {
            previousLastLongValue = lastLongValue;
        }
        break;

    case GTSEncoder.FLAGS_TYPE_DOUBLE:
        lastType = TYPE.DOUBLE;
        if (GTSEncoder.FLAGS_VALUE_IDENTICAL != (tsTypeFlag & GTSEncoder.FLAGS_VALUE_IDENTICAL)) {
            if (GTSEncoder.FLAGS_DOUBLE_IEEE754 == (tsTypeFlag & GTSEncoder.FLAGS_DOUBLE_IEEE754)) {
                ByteOrder order = buffer.order();
                buffer.order(ByteOrder.BIG_ENDIAN);
                previousLastDoubleValue = lastDoubleValue;
                lastDoubleValue = buffer.getDouble();
                previousLastBDValue = lastBDValue;
                lastBDValue = null;
                buffer.order(order);
            } else {
                int scale = buffer.get();
                long unscaled = Varint.decodeSignedLong(buffer);
                previousLastBDValue = lastBDValue;
                lastBDValue = new BigDecimal(new BigInteger(Long.toString(unscaled)), scale);
            }
        } else {
            previousLastDoubleValue = lastDoubleValue;
            previousLastBDValue = lastBDValue;
        }
        break;

    case GTSEncoder.FLAGS_TYPE_STRING:
        lastType = TYPE.STRING;
        if (GTSEncoder.FLAGS_VALUE_IDENTICAL != (tsTypeFlag & GTSEncoder.FLAGS_VALUE_IDENTICAL)) {
            // Decode String length
            long len = Varint.decodeUnsignedLong(buffer);

            // Prevent excessive allocation
            if (len > buffer.remaining()) {
                throw new RuntimeException("Invalid string length.");
            }

            byte[] utf8 = new byte[(int) len];
            // Read String UTF8 representation
            buffer.get(utf8);
            previousLastStringValue = lastStringValue;
            lastStringValue = new String(utf8, Charsets.UTF_8);
        } else {
            previousLastStringValue = lastStringValue;
        }
        break;

    case GTSEncoder.FLAGS_TYPE_BOOLEAN:
        if (GTSEncoder.FLAGS_DELETE_MARKER == (tsTypeFlag & GTSEncoder.FLAGS_MASK_TYPE_FLAGS)) {
            lastType = TYPE.UNDEFINED;
        } else {
            lastType = TYPE.BOOLEAN;

            if (GTSEncoder.FLAGS_BOOLEAN_VALUE_TRUE == (tsTypeFlag & GTSEncoder.FLAGS_MASK_TYPE_FLAGS)) {
                lastBooleanValue = true;
            } else if (GTSEncoder.FLAGS_BOOLEAN_VALUE_FALSE == (tsTypeFlag
                    & GTSEncoder.FLAGS_MASK_TYPE_FLAGS)) {
                lastBooleanValue = false;
            } else {
                throw new RuntimeException("Invalid boolean value.");
            }
            //lastBooleanValue = GTSEncoder.FLAGS_BOOLEAN_VALUE == (tsTypeFlag & GTSEncoder.FLAGS_BOOLEAN_VALUE);
        }
        break;

    default:
        throw new RuntimeException("Invalid type encountered!");
    }

    return true;
}

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

public void outputReady(final NHttpClientConnection conn, final ContentEncoder encoder) {
    System.out.println(conn + " [proxy->origin] output ready");

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

    synchronized (proxyTask) {
        ConnState connState = proxyTask.getOriginState();
        if (connState != ConnState.REQUEST_SENT && connState != ConnState.REQUEST_BODY_STREAM) {
            throw new IllegalStateException("Illegal target connection state: " + connState);
        }//  www .  j  a v  a 2s  . c  o m

        try {

            // TODO: propper handling of POST
            ByteBuffer src = proxyTask.getInBuffer();
            final int srcSize = src.limit();
            if (src.position() != 0) {
                System.out.println(conn + " [proxy->origin] buff not consumed yet");
                return;
            }
            ByteChunk chunk = new ByteChunk(srcSize);
            Request originalRequest = proxyTask.getOriginalRequest();
            int read;
            int encRead = 0;
            long bytesWritten = 0;
            while ((read = originalRequest.doRead(chunk)) != -1) {
                System.out.println(conn + " [proxy->origin] " + read + " bytes read");
                if (read > srcSize) {
                    src = ByteBuffer.wrap(chunk.getBytes(), chunk.getOffset(), read);
                } else {
                    src.put(chunk.getBytes(), chunk.getOffset(), read);
                }
                src.flip();
                encRead = encoder.write(src);
                bytesWritten += encRead;
                src.compact();
                chunk.reset();
                if (encRead == 0) {
                    System.out.println(conn + " [proxy->origin] encoder refused to consume more");
                    break;
                } else {
                    System.out.println(conn + " [proxy->origin] " + encRead + " consumed by encoder");
                }
            }
            System.out.println(conn + " [proxy->origin] " + bytesWritten + " bytes written");
            System.out.println(conn + " [proxy->origin] " + encoder);
            src.compact();

            if (src.position() == 0 && encRead != 0) {
                encoder.complete();
            }
            // Update connection state
            if (encoder.isCompleted()) {
                System.out.println(conn + " [proxy->origin] request body sent");
                proxyTask.setOriginState(ConnState.REQUEST_BODY_DONE);
            } else {
                proxyTask.setOriginState(ConnState.REQUEST_BODY_STREAM);
            }

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

From source file:com.inclouds.hbase.rowcache.RowCache.java

/**
 * CHECKED 2 Adds the key value (KeyValue) to a buffer for Put/Append.
 * /*from w  w w. j a  v a2 s . c o  m*/
 * @param buf
 *          the buf
 * @param kv
 *          the kv
 * @return the int
 */
private int addKeyValue(ByteBuffer buf, KeyValue kv) {

    // Format:
    // 8 bytes - ts
    // 4 bytes - value length
    // value blob
    int valLen = kv.getValueLength();
    int size = 12 + valLen;
    buf.putLong(kv.getTimestamp());
    buf.putInt(valLen);
    buf.put(kv.getBuffer(), kv.getValueOffset(), valLen);
    return size;
}