Example usage for java.nio ByteBuffer hasArray

List of usage examples for java.nio ByteBuffer hasArray

Introduction

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

Prototype

public final boolean hasArray() 

Source Link

Document

Indicates whether this buffer is based on a byte array and provides read/write access.

Usage

From source file:org.apache.hadoop.hbase.client.coprocessor.AggregationClient.java

byte[] getBytesFromResponse(ByteString response) {
    ByteBuffer bb = response.asReadOnlyByteBuffer();
    bb.rewind();/*ww w .  ja  v a 2 s  .c om*/
    byte[] bytes;
    if (bb.hasArray()) {
        bytes = bb.array();
    } else {
        bytes = response.toByteArray();
    }
    return bytes;
}

From source file:org.apache.hadoop.hbase.io.hfile.bucket.FileMmapEngine.java

/**
 * Transfers data from the given byte buffer to file
 * @param srcBuffer the given byte buffer from which bytes are to be read
 * @param offset The offset in the file where the first byte to be written
 * @throws IOException/*  ww w .jav  a  2  s  . com*/
 */
@Override
public void write(ByteBuffer srcBuffer, long offset) throws IOException {
    assert srcBuffer.hasArray();
    bufferArray.putMultiple(offset, srcBuffer.remaining(), srcBuffer.array(), srcBuffer.arrayOffset());
}

From source file:org.apache.hadoop.hive.serde2.thrift.TBinarySortableProtocol.java

@Override
public void writeBinary(ByteBuffer bin) throws TException {
    if (bin == null) {
        writeRawBytes(nullByte, 0, 1);//from ww  w  .  j  a v  a 2s  .  c  o m
        return;
    }

    int length = bin.limit() - bin.position() - bin.arrayOffset();
    if (bin.hasArray()) {
        writeBinary(bin.array(), bin.arrayOffset() + bin.position(), length);
    } else {
        byte[] copy = new byte[length];
        bin.get(copy);
        writeBinary(copy);
    }
}

From source file:org.apache.http.contrib.logging.Wire.java

public void output(final ByteBuffer b) {
    if (b.hasArray()) {
        output(b.array(), b.arrayOffset() + b.position(), b.remaining());
    } else {/*from  www .  j a  va  2 s. co m*/
        byte[] tmp = new byte[b.remaining()];
        b.get(tmp);
        output(tmp);
    }
}

From source file:org.apache.http.contrib.logging.Wire.java

public void input(final ByteBuffer b) {
    if (b.hasArray()) {
        input(b.array(), b.arrayOffset() + b.position(), b.remaining());
    } else {/*from w w w .  jav a 2s.c o  m*/
        byte[] tmp = new byte[b.remaining()];
        b.get(tmp);
        input(tmp);
    }
}

From source file:org.apache.http.HC4.impl.nio.conn.Wire.java

public void output(final ByteBuffer b) {
    if (b.hasArray()) {
        output(b.array(), b.arrayOffset() + b.position(), b.remaining());
    } else {//from   www . j  a  v a 2 s .c o m
        final byte[] tmp = new byte[b.remaining()];
        b.get(tmp);
        output(tmp);
    }
}

From source file:org.apache.http.HC4.impl.nio.conn.Wire.java

public void input(final ByteBuffer b) {
    if (b.hasArray()) {
        input(b.array(), b.arrayOffset() + b.position(), b.remaining());
    } else {// w  w w  . j  a va  2  s.c om
        final byte[] tmp = new byte[b.remaining()];
        b.get(tmp);
        input(tmp);
    }
}

From source file:org.apache.james.protocols.api.handler.CommandDispatcher.java

/**
 * Parse the line into a {@link Request}
 * // w w  w  .  j  ava 2 s  . c o m
 * @param session
 * @param line
 * @return request
 * @throws Exception
 */
protected Request parseRequest(Session session, ByteBuffer buffer) throws Exception {
    String curCommandName = null;
    String curCommandArgument = null;
    byte[] line;
    if (buffer.hasArray()) {
        line = buffer.array();
    } else {
        line = new byte[buffer.remaining()];
        buffer.get(line);
    }
    // This should be changed once we move to java6
    String cmdString = new String(line, session.getCharset().name()).trim();
    int spaceIndex = cmdString.indexOf(" ");
    if (spaceIndex > 0) {
        curCommandName = cmdString.substring(0, spaceIndex);
        curCommandArgument = cmdString.substring(spaceIndex + 1);
    } else {
        curCommandName = cmdString;
    }
    curCommandName = curCommandName.toUpperCase(Locale.US);

    return new BaseRequest(curCommandName, curCommandArgument);

}

From source file:org.apache.james.protocols.smtp.core.DataLineMessageHookHandler.java

private byte[] readBytes(ByteBuffer line) {
    line.rewind();//from w  w  w .  j  a va 2  s .co m
    byte[] bline;
    if (line.hasArray()) {
        bline = line.array();
    } else {
        bline = new byte[line.remaining()];
        line.get(bline);
    }
    return bline;
}

From source file:org.apache.maven.plugin.surefire.report.Utf8RecodingDeferredFileOutputStream.java

public synchronized void write(byte[] buf, int off, int len) throws IOException {
    if (closed) {
        return;/*from   w  ww.ja  v  a 2  s.c o  m*/
    }

    if (!Charset.defaultCharset().equals(UTF8)) {
        CharBuffer decodedFromDefaultCharset = Charset.defaultCharset().decode(ByteBuffer.wrap(buf, off, len));
        ByteBuffer utf8Encoded = UTF8.encode(decodedFromDefaultCharset);

        if (utf8Encoded.hasArray()) {
            byte[] convertedBytes = utf8Encoded.array();

            deferredFileOutputStream.write(convertedBytes, utf8Encoded.position(), utf8Encoded.remaining());
        } else {
            byte[] convertedBytes = new byte[utf8Encoded.remaining()];
            utf8Encoded.get(convertedBytes, 0, utf8Encoded.remaining());

            deferredFileOutputStream.write(convertedBytes, 0, convertedBytes.length);
        }
    } else {
        deferredFileOutputStream.write(buf, off, len);
    }
}