Example usage for java.nio ByteBuffer remaining

List of usage examples for java.nio ByteBuffer remaining

Introduction

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

Prototype

public final int remaining() 

Source Link

Document

Returns the number of remaining elements in this buffer, that is limit - position .

Usage

From source file:edu.cmu.cylab.starslinger.transaction.WebEngine.java

private byte[] handleResponseExceptions(byte[] resp, int errMax)
        throws ExchangeException, MessageNotFoundException {

    int firstInt = 0;
    ByteBuffer result = ByteBuffer.wrap(resp);
    if (mCancelable)
        throw new ExchangeException(mCtx.getString(R.string.error_WebCancelledByUser));
    else if (resp == null)
        throw new ExchangeException(mCtx.getString(R.string.error_ServerNotResponding));
    else if (resp.length < 4)
        throw new ExchangeException(mCtx.getString(R.string.error_ServerNotResponding));
    else {//from   w ww.  j a v  a 2 s .  co m
        firstInt = result.getInt();
        byte[] bytes = new byte[result.remaining()];
        result.get(bytes);
        if (firstInt <= errMax) { // error int
            MyLog.e(TAG, "server error code: " + firstInt);

            // first, look for expected error string codes from 3rd-party
            handleMessagingErrorCodes(resp);

            // second, use message directly from server
            throw new ExchangeException(
                    String.format(mCtx.getString(R.string.error_ServerAppMessage), new String(bytes).trim()));
        }
        // else strip off server version
        mLatestServerVersion = firstInt;
        return bytes;
    }
}

From source file:org.apache.usergrid.persistence.Schema.java

public static ByteBuffer encrypt(ByteBuffer clear) {
    if (clear == null || !clear.hasRemaining()) {
        return clear;
    }//from   w  w w  . j av  a  2 s . c o m
    try {
        SecretKeySpec sKeySpec = new SecretKeySpec(getRawKey(encryptionSeed), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, sKeySpec);
        ByteBuffer encrypted = ByteBuffer.allocate(cipher.getOutputSize(clear.remaining()));
        cipher.doFinal(clear, encrypted);
        encrypted.rewind();
        return encrypted;
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.team254.cheezdroid.SelfieModeFragment.java

/**
 * This a callback object for the {@link ImageReader}. "onImageAvailable" will be called when a
 * still image is ready to be saved.// w  w  w.j a v  a  2s.co  m
 */

private byte[] convertYUV420ToN21(Image imgYUV420) {
    byte[] rez = new byte[0];

    ByteBuffer buffer0 = imgYUV420.getPlanes()[0].getBuffer();
    ByteBuffer buffer2 = imgYUV420.getPlanes()[2].getBuffer();
    int buffer0_size = buffer0.remaining();
    int buffer2_size = buffer2.remaining();
    rez = new byte[buffer0_size + buffer2_size];

    buffer0.get(rez, 0, buffer0_size);
    buffer2.get(rez, buffer0_size, buffer2_size);

    return rez;
}

From source file:com.ery.ertc.estorm.util.Bytes.java

private static byte[] readBytes(ByteBuffer buf) {
    byte[] result = new byte[buf.remaining()];
    buf.get(result);//from w  ww.  ja v a  2  s.  c  om
    return result;
}

From source file:org.apache.tajo.master.exec.NonForwardQueryResultSystemScanner.java

@Override
public SerializedResultSet nextRowBlock(int fetchRowNum) throws IOException {
    int rowCount = 0;

    SerializedResultSet.Builder resultSetBuilder = SerializedResultSet.newBuilder();
    resultSetBuilder.setSchema(getLogicalSchema().getProto());
    resultSetBuilder.setRows(rowCount);/*from   w w  w .  j  av a  2s.c  o m*/
    int startRow = currentRow;
    int endRow = startRow + fetchRowNum;

    if (physicalExec == null) {
        return resultSetBuilder.build();
    }

    while (currentRow < endRow) {
        Tuple currentTuple = physicalExec.next();

        if (currentTuple == null) {
            eof = true;
            break;
        } else {
            if (rowBlock == null) {
                rowBlock = new MemoryRowBlock(SchemaUtil.toDataTypes(tableDesc.getLogicalSchema()));
            }

            rowBlock.getWriter().addTuple(currentTuple);
            currentRow++;
            rowCount++;

            if (currentRow >= maxRow) {
                eof = true;
                break;
            }
        }
    }

    if (rowCount > 0) {
        resultSetBuilder.setRows(rowCount);
        MemoryBlock memoryBlock = rowBlock.getMemory();
        ByteBuffer rows = memoryBlock.getBuffer().nioBuffer(0, memoryBlock.readableBytes());

        resultSetBuilder.setDecompressedLength(rows.remaining());
        resultSetBuilder.setSerializedTuples(ByteString.copyFrom(rows));
        rowBlock.clear();
    }

    if (eof) {
        close();
    }
    return resultSetBuilder.build();
}

From source file:org.ojai.json.impl.JsonDocumentBuilder.java

@Override
public JsonDocumentBuilder put(String field, ByteBuffer value) {
    byte[] bytes = new byte[value.remaining()];
    value.slice().get(bytes);/*w  ww.  java2s.c  o  m*/
    return put(field, bytes);
}

From source file:org.ojai.json.impl.JsonDocumentBuilder.java

@Override
public JsonDocumentBuilder add(ByteBuffer value) {
    byte[] bytes = new byte[value.remaining()];
    value.slice().get(bytes);//from  ww w .j  a v a2s . co  m
    return add(bytes);
}

From source file:org.apache.hadoop.hdfs.BlockReaderLocalLegacy.java

/**
 * Reads bytes into a buffer until EOF or the buffer's limit is reached
 *//*from   w  ww .java  2  s .  c  om*/
private int fillBuffer(FileInputStream stream, ByteBuffer buf) throws IOException {
    try (TraceScope ignored = tracer.newScope("BlockReaderLocalLegacy#fillBuffer(" + blockId + ")")) {
        int bytesRead = stream.getChannel().read(buf);
        if (bytesRead < 0) {
            //EOF
            return bytesRead;
        }
        while (buf.remaining() > 0) {
            int n = stream.getChannel().read(buf);
            if (n < 0) {
                //EOF
                return bytesRead;
            }
            bytesRead += n;
        }
        return bytesRead;
    }
}

From source file:org.apache.usergrid.persistence.Schema.java

public static ByteBuffer decrypt(ByteBuffer encrypted) {
    if (encrypted == null || !encrypted.hasRemaining()) {
        return encrypted;
    }/*from w  ww  .j  a  v a 2s.c o m*/
    try {
        SecretKeySpec sKeySpec = new SecretKeySpec(getRawKey(encryptionSeed), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, sKeySpec);
        ByteBuffer decrypted = ByteBuffer.allocate(cipher.getOutputSize(encrypted.remaining()));
        cipher.doFinal(encrypted, decrypted);
        decrypted.rewind();
        return decrypted;
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.apache.spark.sql.execution.datasources.parquet.UnsafeRowParquetRecordReader.java

private void decodeBinaryBatch(int col, int num) throws IOException {
    for (int n = 0; n < num; ++n) {
        if (columnReaders[col].next()) {
            ByteBuffer bytes = columnReaders[col].nextBinary().toByteBuffer();
            int len = bytes.remaining();
            if (originalTypes[col] == OriginalType.UTF8) {
                UTF8String str = UTF8String.fromBytes(bytes.array(), bytes.arrayOffset() + bytes.position(),
                        len);/*from w w w. j  av  a 2s .  co m*/
                rowWriters[n].write(col, str);
            } else {
                rowWriters[n].write(col, bytes.array(), bytes.arrayOffset() + bytes.position(), len);
            }
            rows[n].setNotNullAt(col);
        } else {
            rows[n].setNullAt(col);
        }
    }
}