Java Code Examples for java.nio.ByteBuffer#hasArray()

The following examples show how to use java.nio.ByteBuffer#hasArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: US_ASCII.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(CharBuffer src,
                                 ByteBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
Example 2
Source File: CESU_8.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
protected final CoderResult encodeLoop(CharBuffer src,
                                       ByteBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
Example 3
Source File: EnvelopeSchemaConverter.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/**
 * Get payload field from GenericRecord and convert to byte array
 */
public byte[] getPayload(GenericRecord inputRecord, String payloadFieldName) {
  ByteBuffer bb = (ByteBuffer) inputRecord.get(payloadFieldName);
  byte[] payloadBytes;
  if (bb.hasArray()) {
    payloadBytes = bb.array();
  } else {
    payloadBytes = new byte[bb.remaining()];
    bb.get(payloadBytes);
  }
  String hexString = new String(payloadBytes, StandardCharsets.UTF_8);
  return DatatypeConverter.parseHexBinary(hexString);
}
 
Example 4
Source File: CameraSource.java    From flutter_barcode_scanner with MIT License 5 votes vote down vote up
private byte[] createPreviewBuffer(Size previewSize) {
    int bitsPerPixel = ImageFormat.getBitsPerPixel(ImageFormat.NV21);
    long sizeInBits = previewSize.getHeight() * previewSize.getWidth() * bitsPerPixel;
    int bufferSize = (int) Math.ceil(sizeInBits / 8.0d) + 1;

    byte[] byteArray = new byte[bufferSize];
    ByteBuffer buffer = ByteBuffer.wrap(byteArray);
    if (!buffer.hasArray() || (buffer.array() != byteArray)) {
        throw new IllegalStateException("Failed to create valid buffer for camera source.");
    }

    mBytesToByteBuffer.put(byteArray, buffer);
    return byteArray;
}
 
Example 5
Source File: CodedOutputStream.java    From play-store-api with GNU General Public License v3.0 5 votes vote down vote up
@Override
public final void writeRawBytes(final ByteBuffer value) throws IOException {
  if (value.hasArray()) {
    write(value.array(), value.arrayOffset(), value.capacity());
  } else {
    ByteBuffer duplicated = value.duplicate();
    duplicated.clear();
    write(duplicated);
  }
}
 
Example 6
Source File: ISO2022_JP.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(CharBuffer src,
                                 ByteBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
Example 7
Source File: StaticArrayEntry.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void copy(ByteBuffer data, byte[] dest, int destOffset) {
    if (data.hasArray()) {
        System.arraycopy(data.array(), data.arrayOffset() + data.position(), dest, destOffset, data.remaining());
    } else {
        data.mark();
        data.get(dest, destOffset, data.remaining());
        data.reset();
    }
}
 
Example 8
Source File: EUC_TW.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return decodeArrayLoop(src, dst);
    else
        return decodeBufferLoop(src, dst);
}
 
Example 9
Source File: ISO_8859_1.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(CharBuffer src,
                                 ByteBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
Example 10
Source File: ISCII91.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult decodeLoop(ByteBuffer src,
                                 CharBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return decodeArrayLoop(src, dst);
    else
        return decodeBufferLoop(src, dst);
}
 
Example 11
Source File: Sniffer.java    From ttt with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private static String findXMLEncoding(ByteBuffer bb) {
    String encoding;
    int restore = bb.position();
    if (findFrom(bb, 0, encoding8))
        encoding = parseXMLEncoding(bb, 1, true);
    else {
        byte[] bytes;
        if (bb.hasArray())
            bytes = bb.array();
        else {
            bytes = new byte[4];
            bb.position(0);
            bb.get(bytes);
            bb.position(0);
        }
        if (bytes.length < 4) {
            encoding = null;
        } else if ((bytes[0] == '<') && (bytes[1] == 0) && (bytes[2] == 0) && (bytes[3] == 0)) {
            if (findFrom(bb, 0, encoding32le))
                encoding = parseXMLEncoding(bb, 4, false);
            else
                encoding = null;
        } else if ((bytes[0] == '<') && (bytes[1] == 0)) {
            if (findFrom(bb, 0, encoding16le))
                encoding = parseXMLEncoding(bb, 2, false);
            else
                encoding = null;
        } else if ((bytes[0] == 0) && (bytes[1] == 0) && (bytes[2] == 0) && (bytes[3] == '<')) {
            if (findFrom(bb, 0, encoding32be))
                encoding = parseXMLEncoding(bb, 4, true);
            else
                encoding = null;
        } else if ((bytes[0] == 0) && (bytes[1] == '<')) {
            if (findFrom(bb, 0, encoding16be))
                encoding = parseXMLEncoding(bb, 2, true);
            else
                encoding = null;
        } else
            encoding = null;
    }
    if (encoding == null)
        bb.position(restore);
    return encoding;
}
 
Example 12
Source File: SingleByteDecoder.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
    if (true && src.hasArray() && dst.hasArray())
        return decodeArrayLoop(src, dst);
    else
        return decodeBufferLoop(src, dst);
}
 
Example 13
Source File: SimpleEUCDecoder.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
    if (src.hasArray() && dst.hasArray())
        return decodeArrayLoop(src, dst);
    else
        return decodeBufferLoop(src, dst);
}
 
Example 14
Source File: LinuxUserDefinedFileAttributeView.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int write(String name, ByteBuffer src) throws IOException {
    if (System.getSecurityManager() != null)
        checkAccess(file.getPathForPermissionCheck(), false, true);

    int pos = src.position();
    int lim = src.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);

    NativeBuffer nb;
    long address;
    if (src instanceof sun.nio.ch.DirectBuffer) {
        nb = null;
        address = ((sun.nio.ch.DirectBuffer)src).address() + pos;
    } else {
        // substitute with native buffer
        nb = NativeBuffers.getNativeBuffer(rem);
        address = nb.address();

        if (src.hasArray()) {
            // copy from backing array into buffer
            int off = src.arrayOffset() + pos + Unsafe.ARRAY_BYTE_BASE_OFFSET;
            unsafe.copyMemory(src.array(), off, null, address, rem);
        } else {
            // backing array not accessible so transfer via temporary array
            byte[] tmp = new byte[rem];
            src.get(tmp);
            src.position(pos);  // reset position as write may fail
            unsafe.copyMemory(tmp, Unsafe.ARRAY_BYTE_BASE_OFFSET, null,
                address, rem);
        }
    }

    int fd = file.openForAttributeAccess(followLinks);
    try {
        try {
            fsetxattr(fd, nameAsBytes(file,name), address, rem);
            src.position(pos + rem);
            return rem;
        } catch (UnixException x) {
            throw new FileSystemException(file.getPathForExceptionMessage(),
                null, "Error writing extended attribute '" + name + "': " +
                x.getMessage());
        } finally {
            close(fd);
        }
    } finally {
        if (nb != null)
            nb.release();
    }
}
 
Example 15
Source File: TBaseHelper.java    From incubator-retired-blur with Apache License 2.0 4 votes vote down vote up
public static boolean wrapsFullArray(ByteBuffer byteBuffer) {
  return byteBuffer.hasArray()
    && byteBuffer.position() == 0
    && byteBuffer.arrayOffset() == 0
    && byteBuffer.remaining() == byteBuffer.capacity();
}
 
Example 16
Source File: TBaseHelper.java    From galaxy-sdk-java with Apache License 2.0 4 votes vote down vote up
public static boolean wrapsFullArray(ByteBuffer byteBuffer) {
  return byteBuffer.hasArray()
    && byteBuffer.position() == 0
    && byteBuffer.arrayOffset() == 0
    && byteBuffer.remaining() == byteBuffer.capacity();
}
 
Example 17
Source File: DoubleByteEncoder.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
    if (true && src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
Example 18
Source File: DefaultCredentialManager.java    From triplea with GNU General Public License v3.0 4 votes vote down vote up
private static void scrub(final ByteBuffer bb) {
  if (bb.hasArray()) {
    scrub(bb.array(), bb.arrayOffset(), bb.arrayOffset() + bb.position() + bb.remaining());
  }
}
 
Example 19
Source File: SingleByteEncoder.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
    if (true && src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
Example 20
Source File: SingleByteEncoder.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
    if (true && src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}