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

The following are Jave code examples for showing how to use hasArray() of the java.nio.ByteBuffer class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: DeepImagePreview-Project   File: CameraSource.java   View Source Code Vote up 6 votes
/**
 * Creates one buffer for the camera preview callback.  The size of the buffer is based off of
 * the camera preview size and the format of the camera image.
 *
 * @return a new preview buffer of the appropriate size for the current camera settings
 */
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;

    //
    // NOTICE: This code only works when using play services v. 8.1 or higher.
    //

    // Creating the byte array this way and wrapping it, as opposed to using .allocate(),
    // should guarantee that there will be an array to work with.
    byte[] byteArray = new byte[bufferSize];
    ByteBuffer buffer = ByteBuffer.wrap(byteArray);
    if (!buffer.hasArray() || (buffer.array() != byteArray)) {
        // I don't think that this will ever happen.  But if it does, then we wouldn't be
        // passing the preview content to the underlying detector later.
        throw new IllegalStateException("Failed to create valid buffer for camera source.");
    }

    mBytesToByteBuffer.put(byteArray, buffer);
    return byteArray;
}
 
Example 2
Project: Spark   File: WaveData.java   View Source Code Vote up 6 votes
/**
 * Creates a WaveData container from the specified ByetBuffer. If the buffer
 * is backed by an array, it will be used directly, else the contents of the
 * buffer will be copied using get(byte[]).
 * 
 * @param buffer
 *            ByteBuffer containing sound file
 * @return WaveData containing data, or null if a failure occured
 */
public static WaveData create(ByteBuffer buffer)
{
	try
	{
		byte[] bytes = null;

		if (buffer.hasArray())
		{
			bytes = buffer.array();
		}
		else
		{
			bytes = new byte[buffer.capacity()];
			buffer.get(bytes);
		}
		return create(bytes);
	}
	catch (Exception e)
	{
		e.printStackTrace();
		return null;
	}
}
 
Example 3
Project: mycat-src-1.6.1-RELEASE   File: FastByteOperations.java   View Source Code Vote up 6 votes
public void copy(ByteBuffer srcBuf, int srcPosition, ByteBuffer trgBuf, int trgPosition, int length)
{
    Object src;
    long srcOffset;
    if (srcBuf.hasArray())
    {
        src = srcBuf.array();
        srcOffset = BYTE_ARRAY_BASE_OFFSET + srcBuf.arrayOffset();
    }
    else
    {
        src = null;
        srcOffset = theUnsafe.getLong(srcBuf, DIRECT_BUFFER_ADDRESS_OFFSET);
    }
    copy(src, srcOffset + srcPosition, trgBuf, trgPosition, length);
}
 
Example 4
Project: trust-wallet-android   File: CameraSource.java   View Source Code Vote up 6 votes
/**
 * Creates one buffer for the camera preview callback.  The size of the buffer is based off of
 * the camera preview size and the format of the camera image.
 *
 * @return a new preview buffer of the appropriate size for the current camera settings
 */
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;

    //
    // NOTICE: This code only works when using play services v. 8.1 or higher.
    //

    // Creating the byte array this way and wrapping it, as opposed to using .allocate(),
    // should guarantee that there will be an array to work with.
    byte[] byteArray = new byte[bufferSize];
    ByteBuffer buffer = ByteBuffer.wrap(byteArray);
    if (!buffer.hasArray() || (buffer.array() != byteArray)) {
        // I don't think that this will ever happen.  But if it does, then we wouldn't be
        // passing the preview content to the underlying detector later.
        throw new IllegalStateException("Failed to create valid buffer for camera source.");
    }

    mBytesToByteBuffer.put(byteArray, buffer);
    return byteArray;
}
 
Example 5
Project: jdk8u-jdk   File: Base64.java   View Source Code Vote up 6 votes
/**
 * Decodes all bytes from the input byte buffer using the {@link Base64}
 * encoding scheme, writing the results into a newly-allocated ByteBuffer.
 *
 * <p> Upon return, the source buffer's position will be updated to
 * its limit; its limit will not have been changed. The returned
 * output buffer's position will be zero and its limit will be the
 * number of resulting decoded bytes
 *
 * <p> {@code IllegalArgumentException} is thrown if the input buffer
 * is not in valid Base64 encoding scheme. The position of the input
 * buffer will not be advanced in this case.
 *
 * @param   buffer
 *          the ByteBuffer to decode
 *
 * @return  A newly-allocated byte buffer containing the decoded bytes
 *
 * @throws  IllegalArgumentException
 *          if {@code src} is not in valid Base64 scheme.
 */
public ByteBuffer decode(ByteBuffer buffer) {
    int pos0 = buffer.position();
    try {
        byte[] src;
        int sp, sl;
        if (buffer.hasArray()) {
            src = buffer.array();
            sp = buffer.arrayOffset() + buffer.position();
            sl = buffer.arrayOffset() + buffer.limit();
            buffer.position(buffer.limit());
        } else {
            src = new byte[buffer.remaining()];
            buffer.get(src);
            sp = 0;
            sl = src.length;
        }
        byte[] dst = new byte[outLength(src, sp, sl)];
        return ByteBuffer.wrap(dst, 0, decode0(src, sp, sl, dst));
    } catch (IllegalArgumentException iae) {
        buffer.position(pos0);
        throw iae;
    }
}
 
Example 6
Project: openjdk-jdk10   File: UTF_8.java   View Source Code Vote up 5 votes
protected CoderResult decodeLoop(ByteBuffer src,
                                 CharBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return decodeArrayLoop(src, dst);
    else
        return decodeBufferLoop(src, dst);
}
 
Example 7
Project: sstable-adaptor   File: FastByteOperations.java   View Source Code Vote up 5 votes
public static void copy(Object src, long srcOffset, ByteBuffer trgBuf, int trgPosition, int length)
{
    if (trgBuf.hasArray())
        copy(src, srcOffset, trgBuf.array(), trgBuf.arrayOffset() + trgPosition, length);
    else
        copy(src, srcOffset, null, trgPosition + theUnsafe.getLong(trgBuf, DIRECT_BUFFER_ADDRESS_OFFSET), length);
}
 
Example 8
Project: monarch   File: HeapDataOutputStream.java   View Source Code Vote up 5 votes
/**
 * gets the contents of this stream as a byte[]. The stream should not be written to past this
 * point until it has been reset.
 */
public final byte[] toByteArray() {
  ByteBuffer bb = toByteBuffer();
  if (bb.hasArray() && bb.arrayOffset() == 0 && bb.limit() == bb.capacity()) {
    return bb.array();
  } else {
    // create a new buffer of just the right size and copy the old buffer into it
    ByteBuffer tmp = ByteBuffer.allocate(bb.remaining());
    tmp.put(bb);
    tmp.flip();
    this.buffer = tmp;
    return this.buffer.array();
  }
}
 
Example 9
Project: B4J_Server   File: CompressExtension.java   View Source Code Vote up 5 votes
private static boolean supplyInput(Inflater inflater, ByteBuffer buf)
{
    if (buf.remaining() <= 0)
    {
        if (LOG.isDebugEnabled())
        {
            LOG.debug("No data left left to supply to Inflater");
        }
        return false;
    }

    byte input[];
    int inputOffset;
    int len;

    if (buf.hasArray())
    {
        // no need to create a new byte buffer, just return this one.
        len = buf.remaining();
        input = buf.array();
        inputOffset = buf.position() + buf.arrayOffset();
        buf.position(buf.position() + len);
    }
    else
    {
        // Only create an return byte buffer that is reasonable in size
        len = Math.min(INPUT_MAX_BUFFER_SIZE,buf.remaining());
        input = new byte[len];
        inputOffset = 0;
        buf.get(input,0,len);
    }

    inflater.setInput(input,inputOffset,len);
    if (LOG.isDebugEnabled())
    {
        LOG.debug("Supplied {} input bytes: {}",input.length,toDetail(inflater));
    }
    return true;
}
 
Example 10
Project: jdk8u-jdk   File: ISO2022.java   View Source Code Vote up 5 votes
protected CoderResult encodeLoop(CharBuffer src,
                                 ByteBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
Example 11
Project: jdk8u-jdk   File: CESU_8.java   View Source Code Vote up 5 votes
protected CoderResult decodeLoop(ByteBuffer src,
                                 CharBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return decodeArrayLoop(src, dst);
    else
        return decodeBufferLoop(src, dst);
}
 
Example 12
Project: ditb   File: ByteBufferUtils.java   View Source Code Vote up 5 votes
/**
 * Copy from one buffer to another from given offset. This will be absolute positional copying and
 * won't affect the position of any of the buffers.
 * @param out
 * @param in
 * @param sourceOffset
 * @param destinationOffset
 * @param length
 */
public static void copyFromBufferToBuffer(ByteBuffer out, ByteBuffer in, int sourceOffset,
    int destinationOffset, int length) {
  if (in.hasArray() && out.hasArray()) {
    System.arraycopy(in.array(), sourceOffset + in.arrayOffset(), out.array(), out.arrayOffset()
        + destinationOffset, length);
  } else {
    for (int i = 0; i < length; ++i) {
      out.put((destinationOffset + i), in.get(sourceOffset + i));
    }
  }
}
 
Example 13
Project: flink-connectors   File: FlinkPravegaUtils.java   View Source Code Vote up 5 votes
@Override
@SneakyThrows
public T deserialize(ByteBuffer buffer) {
    byte[] array;
    if (buffer.hasArray() && buffer.arrayOffset() == 0 && buffer.position() == 0 && buffer.limit() == buffer.capacity()) {
        array = buffer.array();
    } else {
        array = new byte[buffer.remaining()];
        buffer.get(array);
    }

    return deserializationSchema.deserialize(array);
}
 
Example 14
Project: jdk8u-jdk   File: UTF_8.java   View Source Code Vote up 5 votes
protected CoderResult decodeLoop(ByteBuffer src,
                                 CharBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return decodeArrayLoop(src, dst);
    else
        return decodeBufferLoop(src, dst);
}
 
Example 15
Project: jdk8u-jdk   File: DBCS_IBM_EBCDIC_Encoder.java   View Source Code Vote up 4 votes
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
    if (true && src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
Example 16
Project: letv   File: ca.java   View Source Code Vote up 4 votes
public static boolean b(ByteBuffer byteBuffer) {
    return byteBuffer.hasArray() && byteBuffer.position() == 0 && byteBuffer.arrayOffset() == 0 && byteBuffer.remaining() == byteBuffer.capacity();
}
 
Example 17
Project: openjdk-jdk10   File: SimpleEUCEncoder.java   View Source Code Vote up 4 votes
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
    if (true && src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
Example 18
Project: OpenJSharp   File: EUC_JP.java   View Source Code Vote up 4 votes
public CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
    if (src.hasArray() && dst.hasArray())
        return decodeArrayLoop(src, dst);
    else
        return decodeBufferLoop(src, dst);
}
 
Example 19
Project: sstable-adaptor   File: DataOutputStreamPlus.java   View Source Code Vote up 4 votes
protected WritableByteChannel newDefaultChannel()
{
    return new WritableByteChannel()
    {

        @Override
        public boolean isOpen()
        {
            return true;
        }

        @Override
        public void close()
        {
        }

        @Override
        public int write(ByteBuffer src) throws IOException
        {
            int toWrite = src.remaining();

            if (src.hasArray())
            {
                DataOutputStreamPlus.this.write(src.array(), src.arrayOffset() + src.position(), src.remaining());
                src.position(src.limit());
                return toWrite;
            }

            if (toWrite < 16)
            {
                int offset = src.position();
                for (int i = 0 ; i < toWrite ; i++)
                    DataOutputStreamPlus.this.write(src.get(i + offset));
                src.position(src.limit());
                return toWrite;
            }

            byte[] buf = retrieveTemporaryBuffer(toWrite);

            int totalWritten = 0;
            while (totalWritten < toWrite)
            {
                int toWriteThisTime = Math.min(buf.length, toWrite - totalWritten);

                ByteBufferUtil.arrayCopy(src, src.position() + totalWritten, buf, 0, toWriteThisTime);

                DataOutputStreamPlus.this.write(buf, 0, toWriteThisTime);

                totalWritten += toWriteThisTime;
            }

            src.position(src.limit());
            return totalWritten;
        }

    };
}
 
Example 20
Project: jdk8u-jdk   File: DoubleByte.java   View Source Code Vote up 4 votes
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
    if (src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}