Example usage for java.nio ByteBuffer get

List of usage examples for java.nio ByteBuffer get

Introduction

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

Prototype

public abstract byte get(int index);

Source Link

Document

Returns the byte at the specified index and does not change the position.

Usage

From source file:hornet.framework.clamav.service.ClamAVCheckService.java

/**
 * Lecture du fichier et envoi sur la socket.
 *
 * @param resultat//from w  w  w .jav a2  s . c o  m
 *            resultat
 * @param fileForTestSize
 *            fileForTestSize
 * @param channel
 *            channel
 * @param bufFileForTestRead
 *            bufFileForTestRead
 * @throws IOException
 *             IOException
 */
protected void readAndSendFile(final StringBuilder resultat, final long fileForTestSize,
        final SocketChannel channel, final MappedByteBuffer bufFileForTestRead) throws IOException {

    // Envoi de la commande
    final ByteBuffer writeReadBuffer = ByteBuffer.allocate(BUFFER_SIZE);
    writeReadBuffer.put(ClamAVCheckService.COMMANDE.getBytes(UTF_8));
    writeReadBuffer.put(this.intToByteArray((int) fileForTestSize));
    writeReadBuffer.flip();
    channel.write(writeReadBuffer);

    // Envoi du fichier

    long size = fileForTestSize;
    // envoi du fichier
    while (size > 0) {
        size -= channel.write(bufFileForTestRead);
    }
    final ByteBuffer writeBuffer = ByteBuffer.allocate(4);
    writeBuffer.put(new byte[] { 0, 0, 0, 0 });
    writeBuffer.flip();
    channel.write(writeBuffer);

    // lecture de la rponse
    ByteBuffer readBuffer;
    readBuffer = ByteBuffer.allocate(BUFFER_SIZE);
    // lecture de la rponse
    readBuffer.clear();
    boolean readLine = false;
    while (!readLine) {
        final int numReaden = channel.read(readBuffer);
        if (numReaden > 0) {
            readLine = readBuffer.get(numReaden - 1) == '\n';
            resultat.append(new String(readBuffer.array(), 0, numReaden, UTF_8));
            readBuffer.clear();
        } else {
            if (numReaden == -1) {
                readLine = true;
                readBuffer.clear();
            }
        }
    }
}

From source file:com.healthmarketscience.jackcess.impl.ColumnImpl.java

/**
 * Read the extra flags field for a column definition.
 *///from www . j a  v  a  2 s. c  o  m
static byte readExtraFlags(ByteBuffer buffer, int offset, JetFormat format) {
    int extFlagsOffset = format.OFFSET_COLUMN_EXT_FLAGS;
    return ((extFlagsOffset >= 0) ? buffer.get(offset + extFlagsOffset) : 0);
}

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();//  ww  w  .ja  va  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:de.rwhq.btree.LeafNode.java

@Override
public byte[] getLastLeafKeySerialized() {
    final ByteBuffer buffer = rawPage().bufferForReading(getOffsetForKeyPos(getNumberOfEntries() - 1));
    final byte[] buf = new byte[keySerializer.getSerializedLength()];
    buffer.get(buf);
    return buf;//from  w  w w  .  ja  va 2 s  .com
}

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 w w .  ja va  2 s . c  o 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:com.ery.ertc.estorm.util.Bytes.java

private static byte[] readBytes(ByteBuffer buf) {
    byte[] result = new byte[buf.remaining()];
    buf.get(result);
    return result;
}

From source file:ar.com.qbe.siniestros.model.utils.MimeMagic.MagicMatcher.java

/**
 * test the data against the test byte/*from w w w .  j ava 2  s .  co  m*/
 *
 * @param data the data we are testing
 *
 * @return if we have a match
 */
private boolean testByte(ByteBuffer data) {
    log.debug("testByte()");

    String test = new String(match.getTest().array());
    char comparator = match.getComparator();
    long bitmask = match.getBitmask();

    String s = test;
    byte b = data.get(0);
    b = (byte) (b & bitmask);
    log.debug("testByte(): decoding '" + test + "' to byte");

    int tst = Integer.decode(test).byteValue();
    byte t = (byte) (tst & 0xff);
    log.debug("testByte(): applying bitmask '" + bitmask + "' to '" + tst + "', result is '" + t + "'");
    log.debug("testByte(): comparing byte '" + b + "' to '" + t + "'");

    switch (comparator) {
    case '=':
        return t == b;

    case '!':
        return t != b;

    case '>':
        return t > b;

    case '<':
        return t < b;
    }

    return false;
}

From source file:com.linkedin.databus.core.DbusEventV1.java

@Override
public DbusEventInternalReadable reset(ByteBuffer buf, int position) {
    if (buf.get(position) != DbusEventFactory.DBUS_EVENT_V1) {
        verifyByteOrderConsistency(buf, "DbusEventV1.reset()");
        return DbusEventV2Factory.createReadOnlyDbusEventFromBufferUnchecked(buf, position);
    }/*  w  ww.j a v  a  2s  .co  m*/
    resetInternal(buf, position); // could optionally add "where" arg here, too
    return this;
}

From source file:fuse.okuyamafs.OkuyamaFilesystem.java

public int write_bk(String path, Object fh, boolean isWritepage, ByteBuffer buf, long offset)
        throws FuseException {
    byte[] tmpBuf = new byte[buf.limit()];
    buf.get(tmpBuf);

    return 0;//from   w  w  w.jav a  2 s  .  c  om
}

From source file:fuse.okuyamafs.OkuyamaFilesystem.java

public int realWrite(String path, Object fh, boolean isWritepage, ByteBuffer buf, long offset)
        throws FuseException {
    byte[] writeData = new byte[buf.limit()];
    buf.get(writeData);
    return realWrite(path, fh, isWritepage, writeData, offset);
}